Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test file manipulation

I hear that accessing to database is wrong in testing.

But what about file manipulation? Things like, cp, mv, rm and touch methods in FileUtils.

If I write a test and actually run the commands (moving files, renaming files, making directories and so on), I can test them. But I need to "undo" every command I ran before running a test again. So I decided to write all the code to "undo", but it seems a waste of time because I don't really need to "undo".

I really want to see how others do. How would you test, for example, when you generate a lot of static files?

like image 781
TK. Avatar asked Jan 22 '23 21:01

TK.


1 Answers

In your case accessing the files is totally legit, if you are writing file manipulation code it should be tested on files. The one thing you have to be careful about is that a failed test means that you code is wrong and not that somebody deleted a file that is needed for the test or something like that. I would put the directory and the files you need for the tests in a separate folder that is only used for the test. Then in the build up of the test copy the whole folder to a temporary place do all the testing and then after the test delete the temporary files. In that way each test has a clean copy of the files that are saved for the test.

like image 83
Janusz Avatar answered Jan 25 '23 10:01

Janusz