buildRustCrate: add buildTests flag to tell rustc to build tests instead of binaries

This helps us instruct rustc to build tests instead of binaries. The
actual build will then ONLY produce test executables. This is a first
step towards having rust crate tests within nixpkgs.

We default back to only a single output in test cases since that is the
only reasonable thing to do here.

Producing libraries or binaries in addition to tests would theoretically
be feasible but usually generates different dependency trees. It is very
common to have some libraries in `[dev-depdendencies]` within Cargo.toml
just for your tests. To not start mixing things up going with a
dedicated derivation for the test build sounds like the best choice for
now.

To use this you must provide a proper test dependency chain to
`buildRustCrate` (as you would usually do with your non-test inputs).
And then set the `buildTests` attribute to `true`. The derivation will
then contain all tests that were built in `$out/tests`. All common test
patterns and directories should be supported and tested by this change.

Below is an example how you would run a single test from the derivation.
This commit contains some more examples in the `buildRustCrateTests`
attribute set that might be helpful.

```
let
  drv = buildRustCrate {
     …
     buildTests true;
  };
in runCommand "test-my-crate" {} ''
  touch $out
  exec ${drv}/tests/my-test
''
```
This commit is contained in:
Andreas Rammhold
2020-01-07 11:57:34 +01:00
parent 2c4c7c929c
commit a3a51763f9
5 changed files with 196 additions and 27 deletions
@@ -39,12 +39,12 @@ let
inherit lib stdenv echo_build_heading noisily mkRustcDepArgs rust;
};
installCrate = import ./install-crate.nix;
installCrate = import ./install-crate.nix { inherit stdenv; };
in
crate_: lib.makeOverridable ({ rust, release, verbose, features, buildInputs, crateOverrides,
dependencies, buildDependencies, crateRenames,
extraRustcOpts,
extraRustcOpts, buildTests,
preUnpack, postUnpack, prePatch, patches, postPatch,
preConfigure, postConfigure, preBuild, postBuild, preInstall, postInstall }:
@@ -59,6 +59,7 @@ let crate = crate_ // (lib.attrByPath [ crate_.crateName ] (attr: {}) crateOverr
extraDerivationAttrs = lib.filterAttrs (n: v: ! lib.elem n processedAttrs) crate;
buildInputs_ = buildInputs;
extraRustcOpts_ = extraRustcOpts;
buildTests_ = buildTests;
# take a list of crates that we depend on and override them to fit our overrides, rustc, release, …
makeDependencies = map (dep: lib.getLib (dep.override { inherit release verbose crateOverrides; }));
@@ -78,7 +79,7 @@ stdenv.mkDerivation (rec {
crate.src
else
fetchCrate { inherit (crate) crateName version sha256; };
name = "rust_${crate.crateName}-${crate.version}";
name = "rust_${crate.crateName}-${crate.version}${lib.optionalString buildTests "-test"}";
depsBuildBuild = [ rust stdenv.cc ];
buildInputs = (crate.buildInputs or []) ++ buildInputs_;
dependencies = makeDependencies dependencies_;
@@ -122,6 +123,8 @@ stdenv.mkDerivation (rec {
++ extraRustcOpts_
++ (lib.optional (edition != null) "--edition ${edition}");
buildTests = buildTests_;
configurePhase = configureCrate {
inherit crateName buildDependencies completeDeps completeBuildDeps crateDescription
crateFeatures crateRenames libName build workspace_member release libPath crateVersion
@@ -132,12 +135,14 @@ stdenv.mkDerivation (rec {
inherit crateName dependencies
crateFeatures crateRenames libName release libPath crateType
metadata hasCrateBin crateBin verbose colors
extraRustcOpts;
extraRustcOpts buildTests;
};
installPhase = installCrate crateName metadata;
installPhase = installCrate crateName metadata buildTests;
outputs = [ "out" "lib" ];
outputDev = [ "lib" ];
# depending on the test setting we are either producing something with bins
# and libs or just test binaries
outputs = if buildTests then [ "out" ] else [ "out" "lib" ];
outputDev = if buildTests then [ "out" ] else [ "lib" ];
} // extraDerivationAttrs
)) {
@@ -162,4 +167,5 @@ stdenv.mkDerivation (rec {
dependencies = crate_.dependencies or [];
buildDependencies = crate_.buildDependencies or [];
crateRenames = crate_.crateRenames or {};
buildTests = crate_.buildTests or false;
}