Skip to content

Commit

Permalink
nix: separate derivations for build, check, test
Browse files Browse the repository at this point in the history
  • Loading branch information
hraban committed May 5, 2023
1 parent 738ad2e commit f3eb40d
Showing 1 changed file with 33 additions and 41 deletions.
74 changes: 33 additions & 41 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
pname = "tomono";
version = "2.0.0";
src = ./.;
outputs = [ "out" "doc" ];
outputs = [ "out" ];
buildPhase = ''
# Remove the stale VCS copy
rm tomono
Expand Down Expand Up @@ -59,45 +59,37 @@
outputs = [ "out" ];
});
};
# Build a whole new test derivation and use the final, baked, built
# derivation as input so we really fully test the actual final
# released program. Rather than overriding attrs, which would rebuild
# a copy of the program, and might test something different
# (particularly with differing phases or hooks that accidentally do or
# dont get called). This is a more reliable way of testing in Nix,
# imo. Unfortunately, the test code itself is also built by the
# derivation, so while it looks like overriding here, I’m only doing
# that to build the /test/ code, not the actual binary that will be
# tested. But if e.g. the test script was checked into git, I’d just
# create a completely separate derivation and call that.
checks.default = self.packages.${system}.default.overrideAttrs (_: {
pname = "tomono-test";
version = "1.0";
env = {
GIT_AUTHOR_NAME = "Test";
GIT_AUTHOR_EMAIL = "test@test.com";
GIT_COMMITTER_NAME = "Test";
GIT_COMMITTER_EMAIL = "test@test.com";
};
outputs = [ "out" ];
nativeBuildInputs = [
# The actual code being tested
self.packages.${system}.default
pkgs.makeWrapper
];
doCheck = true;
checkPhase = ''
# Ensure we're testing the pre-built tomono derivation, not the new one
rm -f tomono
# I could add pkgs.git to nativeBuildInputs but I'd be polluting
# the PATH for the tomono script itself as well, which means I
# could miss a misconfiguration if that derivation doesn't
# properly supply its own git path somehow.
wrapProgram test --suffix PATH : "${pkgs.git}/bin"
./test
'';
installPhase = "echo done > $out";
dontFixup = true;
});
checks.default =
let
# Create an entirely separate derivation for the test script
# alone. This can reuse the git path baking and the shebang
# patching of the main derivation, so I can just immediately call
# it.
tomono-test = self.packages.${system}.default.overrideAttrs (_: {
pname = "tomono-test";
installPhase = "mkdir -p $out/bin; cp test $out/bin/tomono-test";
});
in
pkgs.stdenv.mkDerivation (_: {
pname = "tomono-check";
version = "1.0";
env = {
GIT_AUTHOR_NAME = "Test";
GIT_AUTHOR_EMAIL = "test@test.com";
GIT_COMMITTER_NAME = "Test";
GIT_COMMITTER_EMAIL = "test@test.com";
};
nativeBuildInputs = [
# The actual code being tested. Must be in PATH.
self.packages.${system}.default
pkgs.makeWrapper
];
dontUnpack = true;
buildPhase = ''
${tomono-test}/bin/tomono-test
'';
# To keep Nix happy
installPhase = "echo done > $out";
});
});
}

0 comments on commit f3eb40d

Please sign in to comment.