Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nextest: produce output in result even when the command fails #305

Open
phip1611 opened this issue Apr 18, 2023 · 5 comments
Open

nextest: produce output in result even when the command fails #305

phip1611 opened this issue Apr 18, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@phip1611
Copy link

phip1611 commented Apr 18, 2023

I use cargo-nextest to produce a JUnit-style XML report. If all tests succeed inside Nix with crane via cargo-nextest, result/target/nextest/ci/rust-junit.xml contains the result file. The problem is: This file is also needed, if cargo-nextest fails. I tried nix-build --keep-failed --keep-going release.nix -A test but there is still no result folder.

A working workaround is adding || true:

  cargoNextest = crane.cargoNextest (commonArgs // {
    inherit cargoArtifacts;
    # working workaround
    cargoNextestExtraArgs = "--profile ci || true";
  });

but it is inconvenient, as I still need the test command to fail.

I'd like to:

  • ask for advice and
  • an update of the documentation. (I can submit an PR, once the solution is clarified) :)
@phip1611 phip1611 changed the title nextest: produce output even when the command fails nextest: produce output in result even when the command fails Apr 18, 2023
@phip1611
Copy link
Author

I've experimented with the following code:

  nextestReportWrapped = stdenv.mkDerivation {
    name = "nextest-report-wrapped";
    dontCheck = true;
    dontInstall = true;
    broken = true;
    inherit src;
    outputs = [ "out" ];
    buildPhase = ''
      mkdir -p $out/target/nextest/ci
      cp ${cargoNextest}/target/nextest/ci/rust-junit.xml $out/target/nextest/ci
      if grep -q 'failures="[1-9][0-9]*"' $out/target/nextest/ci/rust-junit.xml;
      then
        echo "FAILURE!"
        exit 1
      else
        echo "NO FAILURE!"
      fi
    '';
  };

But, no matter what I've tried, the result folder was never created.

@phip1611
Copy link
Author

Okay, so far the best approach I could find is the following:

  • two Nix attributes that execute cargo nextest, but one with || true
  • in CI, execute the following:
    # never fails but produces the junit.xml file
    nix-build nix/release.nix -A particle.test-ci
    # always fails if a test fails, but has no output
    nix-build --no-out-link nix/release.nix -A particle.test >/dev/null 2>/dev/null

@ipetkov
Copy link
Owner

ipetkov commented Apr 18, 2023

It's worth noting that nix build --keep-going will continue trying to build unrelated derivations; if a derivation's build script fails, Nix won't install anything (and any other derivations which depend on it will not be built either).

You may want to try adding an additional postInstall hook which installs the nextest files (outside of the compressed tarball of target) in addition to the || true short circuit

  craneLib.cargoNextest (commonArgs // {
    inherit cargoArtifacts;
    # working workaround
    cargoNextestExtraArgs = "--profile ci || true";
    postInstall = ''
      cp ./target/nextest/ci/rust-junit.xml $out/rust-junit.xml
    '';
  });

I suppose it may be worth exposing an allowFailure flag on cargoNextest which automatically does the short circuit when set...

@szlend
Copy link
Contributor

szlend commented Oct 19, 2023

Another thing we could do is make the package runnable by including a bin/$pname script in the test derivation that returns with the exit code of cargo test/cargo nextest. Maybe print out the path to test artifacts or copy them into cwd as well. This way you could simply nix run .#tests in CI and easily access test reports regardless of whether the tests failed or not.

@phip1611
Copy link
Author

That would be nice! That's a good idea.

@ipetkov ipetkov added the enhancement New feature or request label Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants