-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test fwd_step_log_open #2388
Test fwd_step_log_open #2388
Conversation
7e95357
to
868395e
Compare
Unfortunately no, the way to ensure that class TmpLog
{
public:
string log_file_path;
fwd_step_log_type *fwd_step_log
TmpLog()
: log_file_path( "tmp_fwd_step_log_open/test.txt" )
{
fwd_step_log = fwd_step_log_alloc();
fwd_step_log_set_log_file(fwd_step_log, log_file_path);
fwd_step_log_open(fwd_step_log);
}
~TmpLog()
{
fwd_step_log_free(fwd_step_log);
std::filesystem::remove_all(log_file_path);
}
}; I believe that is the correct solution, but perhaps @dotfloat has some interesting more modern c++ thoughts on this. |
868395e
to
8e48eff
Compare
Thanks! |
df806ac
to
4024f1c
Compare
I certainly like it, but perhaps it would be best to ask @dotfloat for a review aswell. |
Ah, I did manage to remember something. This is still not 100% foolproof. Things can go south and the object destructor never called so the folder never removed but it would have to be really catastrophic (like pulling the cable). So I suggest you use tmpfile or tmpnam so that a new temporary file (or directory) is always created. |
So, technically, you're not using RAII (at least not directly). You're using catch2's test fixture functionality. If you were to RAII, you'd write: TEST_CASE("..", "[foo]") {
TmpLog tmp_log;
// ...
} This is probably okay. But to answer @eivindjahren , modern C++ goes full speed ahead wrt. RAII, so they are definitely the way to go. |
Good to know that I am not completely out of the loop to suggest RAII here. I think we can call this RAII pattern even though we use it as a fixture. As you noted when we talked about this. Even calling util (which this code is fond of) will break all our efforts of trying to clean up unless we put it into a abort signal handler (lets not do that). So I suggest adding that |
I agree wrt. creating a new directory. I would suggest stealing the |
Could be, as the original author of that I hesitated to suggest it since it might be overkill and it has a few weaknesses. Since it changes the entry point to the tests the compile time goes up a bit because catch2 is not optimized for that usage. Also we might want to resolve equinor/resdata#821 first. |
4024f1c
to
0a9e6ff
Compare
0a9e6ff
to
7de47e0
Compare
Did not think of RAII as it's new to me. I read up on it a bit but can't see what kind of problems we might end up having by using what we have now. What can go wrong? |
RAII will handle things correctly for the normal error handling stuff, however that still leaves signals and catastrophic failures. If the program is given So the cases we are most worried about is So the solution proposed is to instead be robust to what happens when we are unable to clean up. That involves (1) writing our temporary file to pytest has a pretty cool fixture for doing this which creates a directory |
Generating random names is a good idea, thanks. clang complains about ...warning: 'tmpnam' is deprecated: This function is provided for compatibility reasons only. Due to security concerns inherent in the design of tmpnam(3), it is highly recommended that you use mkstemp(3) instead. [-Wdeprecated-declarations]
Could perhaps create a "unique" filename by appending a timestamp or something similar, but I don't like that. Ideas? |
mkstemp has its own problem that it is posix standard only. You can get the name of the file by using If we include boost we could do
|
Seems a bit overkill to include |
I don't know, we might want to use boost more in any case, and if we use |
Should be noted that the reason not to use That is one reason why pytest uses the folder structure it does for its |
This function has been deleted, but the work can be used in testing other functions that write to disk. |
Issue
Resolves #2387
Will all code under
WHEN
always run even if something crashes?I would like to always run the following,
but am not sure if this is the right way to go about it.