approvals/ApprovalTests.Java

Question: Possibility to use different approval files depending on OS platform

DGuhr opened this issue · 3 comments

DGuhr commented

Hey :)

I hope you could help me with the following question: Is it possible to point the library to different approval files depending on the operating system? If so, how?

context:
We're using approvalTests to check CLI outputs in our new keycloak testsuite for the quarkus cli. See here [1] [2] for an approvalTest, and here for the folder containing the approvals.

Everything is fine as long as we're on a unix system, but we're also cross-platform, so windows is a thing and there we use a kc.bat entrypoint instead of kc.sh on unix, and this is included in the approvals. So now I am searching a way to tell the tests "if on windows, use this folder containing the windows approvals with kc.bat inside, and if on unix (macos/linux) use that folder with the corresponding approvals inside." - The detection of the OS is not the problem, but for the "point to different approval files based on that condition" part i can't find something, so hope you could help me :)

Thanks in advance!

hello, first of all, thats a really cool use-case of using approval tests.

I've 2 thoughts about this:

  1. you could split it into 2 tests and use conditional annotations from junit5

  2. If you wrap the call to Approvals.verify the following way, I think it will add the OS into the approved file name, like for example here: src/test/java/org/approvaltests/machine_specific_tests/ApprovalsTest.testApproveComponent.Mac_OS_X.approved.png.

    try (NamedEnvironment env = NamerFactory.asOsSpecificTest())
    {
      Approvals.verify(getOutput());
    }
DGuhr commented

thanks for the fast response! You are right, conditionals may be helpful. I think 2nd approach is not usable as is, because it's more coarse grained for us, its either windows or "all others", as e.g. linux and osx behave the same when it comes to sh files. But will look into the NamerFactory later on :)

DGuhr commented

just created a little approvaltest extension to have a KcNamerFactory that has a WindowsOrUnixOsEnvironmentLabeller inside that returns either "windows" when os.name (to lower) contains "windows", or "unix" otherwise. Works for me (on macos) at least , so thanks again.

Have to give it a few tries and spin up the windows vm later on to see if it also works there, but if it works and if you want i can contribute back for sure. :)