News for hackers. Stuff that matters.
Re:C++ Filesystem Technical Specification approved by ISO by rikkus on Saturday January 03, 2015 @10:09AM (#8830889)
I like to be able to inject a 'filesystem' object into code for testing, so as not to have to use a real filesystem. This doesn't look like it supports that, which is a shame.

My other tactic is to create a temp directory at the start of a test and do _everything_ within it, then clean it up at the end.

Any other ideas on how to write testable code which uses this filesystem API?

Re:C++ Filesystem Technical Specification approved by ISO by euid on Saturday January 03, 2015 @04:22PM (#8832037)
You probably want something like pyfakefs[1], a reimplementation of Python's (old) filesystem modules for the purpose of testing.

My guess is that either you will have to write it, or you will have to convince a C++ developer that it is worth writing cppfakefs.

Otherwise, you can build a test_root_dir RAII object that creates a directory in /tmp and does the cleanup automatically in its destructor.

[1]: https://code.google.com/p/pyfakefs/

Re:C++ Filesystem Technical Specification approved by ISO by stinos on Saturday January 03, 2015 @12:06PM (#8831223)
Any other ideas on how to write testable code which uses this filesystem API?

not nice ones (or I must be missing something). Could either make all code take the API as a template parameter, or wrap the API in your own set of classes. Now often the logic you really want to (unit) test isn't the one directly accessing the filesystem but rather a layer higher? And in case of integration/system tests (or whatever it's called these days) wouldn't you rather use the actual filesystem, like with the temp directory you talk about? Can you provide a concrete example of what would be hard to test with this?

Re:C++ Filesystem Technical Specification approved by ISO by rikkus on Saturday January 03, 2015 @02:03PM (#8831633)
It's third party code I worry about, where it hasn't been written to expect a root directory. I can chroot on *NIX, I suppose.

Perhaps I should go and have a look at how code using boost normally deals with dependency injection for testing. Maybe I'm missing something.

Re:C++ Filesystem Technical Specification approved by ISO by tedunangst on Saturday January 03, 2015 @12:28PM (#8831307)

    namespace fs = test::filesystem;
Re:C++ Filesystem Technical Specification approved by ISO by blt on Saturday January 03, 2015 @01:00PM (#8831418)
templates. or write your own wrapper using virtual methods and take the hit in runtime performance. just like you would mock up any other C++ code that's not already behind a virtual interface.