How to setup a build pipeline when unit test executes in a different context?

Consider this c# code:

        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
            Console.WriteLine("Running on Windows");
        } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
            Console.WriteLine("Running on Linux");
        } else  {
            Console.WriteLine("Running on an unsupported OS");
        }

If I want to achieve 100% code coverage I need to run this unit test in 3 different contexts. How do I need to setup my pipelines for this?

  1. Build
    1. sonarprepare
    2. dotnet build
  2. testing
    1. test on windows
    2. test on linux
    3. test on mac
  3. Running SonarAnalyze?
    But what files should I collect from previous steps to make this work?
    Since this step is a different context, the paths set in prepare no longer match, the build agent gives me a slightly different directory

Is it even possible to have a 100% code coverage in this example with sonar?

Hi

I don’t know what build system you are using but if it was github actions then you could try something like this which is broadly similar to a requirement we have, all in one workflow:

  • A build job running on ubuntu-latest that:
    • Checkout the repo again (Depth 0)
    • Installs Sonar
    • Does the sonar BEGIN
    • Builds the code
    • Pushes the `.sonarqube folder contents into a github artifact
    • Pushes the test assemblies into another artifact
  • Now do a matrix job run-on: {{ matrix.os }} where the matrix is the os-specific github runners you want to use. Inside that matrix:
    • Downloads the test assemblies
    • Runs tests + coverage
    • Pushes the test and coverage output into another github artifact which has an os-specific part in the name to ensure we have different artifacts per OS and name them so you can easily wild-card later - e.g. test-artifacts-windows, coverage-artifacts-ubuntu etc
  • Now you want another job running again on ubuntu-latest (or whatever OS you ran the build on - it MUST be the same OS) that:
    • Checkout the repo again (Depth 0)
    • Downloads all of the artifacts from the matrix above - so test-artifacts- and coverage-artifacts-
    • Installs Sonar
    • Runs the Sonar END step and points it to the test and coverage artifacts folders you downloaded to

I think that should give you what you want but note:

  1. It is important that the BEGIN and END run on the same type of agent or the coverage won’t be recorded correctly
  2. This is 1000% an unsupported mechanism - it is possible in the future Sonar may change the folder name, may require other folders, may do other things that mean this would not work.

anyway hope this gives you some idea of what’s involved.