approvals / ApprovalTests.cpp

Native ApprovalTests for C++ on Linux, Mac and Windows
https://approvaltestscpp.readthedocs.io/en/latest/
Apache License 2.0
318 stars 51 forks source link

Allow setting the root path from where Approval Tests searches for approved files #128

Closed Corristo closed 3 years ago

Corristo commented 4 years ago

I work on a project that is running on a 64-bit ARM board with Linux (among other platforms) and I'd like to use Approval Tests to introduce tests for some of our currently untested code. Currently the path where ApprovalTests.cpp searches for approved files is always relative to the source file of the test and hard-coded into the binary at compile time. However, for that ARM board we don't compile the sources on the board itself but cross-compile on a much more powerful build machine, so the paths that are compiled into the test binary don't exist on the ARM board.

There are two possible ways how support for such a scenario could be added:

Both alternatives would also provide another way to work around the Ninja issue when building in a subdirectory of the source dir.

claremacrae commented 4 years ago

Thanks for this great suggestion! We've got as much as we can handle in flight at the moment, so will pick this up later ... Current thinking is that it would be something that it would be a default that would be configured in code.

Corristo commented 4 years ago

I've looked into this a little bit today as this is a blocker for me, but I also don't want to reinvent the wheel here. Am I correct in assuming that I could write a custom Namer that doesn't use getDirectory but a constructor parameter to determine the root directory? If so I'll try that and create a PR later this week.

claremacrae commented 4 years ago

Ah right. Thanks for letting us know your time-frame...

Am I correct in assuming that I could write a custom Namer that doesn't use getDirectory but a constructor parameter to determine the root directory? If so I'll try that and create a PR later this week.

It's worth a try, as it's a quick experiment to do.

Let us know how you get on - and if it doesn't work, then let us know which test framework you are using, as I have a slightly horrible alternative suggestion....

Corristo commented 4 years ago

It turned out I needed to do two things: Create the custom namer and, because I use GoogleTest, a new GoogleTestListener that doesn't try to determine whether the test fixture name and the source file name are equivalent.

However, I'm not really satisfied with my solution, as it requires users to basically implement whatever functionality other namers provide again when they need to use a different root directory, it simply isn't composable with pre-existing namers.

I've opened a PR anyway (marked as work-in-progress) so we have a basis for further ideas and discussions.

claremacrae commented 3 years ago

This has been implemented, via the templated namer.

The docs are here: https://github.com/approvals/ApprovalTests.cpp/blob/f88bab127d59bd6a41fcb82b6336c28104a9a8f7/doc/Namers.md#templatedcustomnamer

And they link to an example called "out_of_source" that shows how to use them to say where to search for approved files.