Add tests for using cargoExtraArgs

* In this case, we set up a crate with various non-default features and
  ensure that we can correctly influence the cargo build by passing in
  the appropriate `--feature` flag
This commit is contained in:
Ivan Petkov 2022-01-04 09:28:58 -08:00
parent 9b09599c5c
commit 359cb53441
No known key found for this signature in database
GPG Key ID: BB6F9EFC065832B6
8 changed files with 133 additions and 82 deletions

View File

@ -3,10 +3,10 @@
, jq , jq
}: }:
src: expected: src: expected: args:
let let
runCargoAndCheckFreshness = cmd: '' runCargoAndCheckFreshness = cmd: ''
cargo ${cmd} --workspace --release --message-format json-diagnostic-short >${cmd}out cargo ${cmd} --workspace --release --message-format json-diagnostic-short ${args.cargoExtraArgs or ""} >${cmd}out
filter='select(.reason == "compiler-artifact" and .fresh != true) | .target.name' filter='select(.reason == "compiler-artifact" and .fresh != true) | .target.name'
builtTargets="$(jq -r "$filter" <${cmd}out | sort -u)" builtTargets="$(jq -r "$filter" <${cmd}out | sort -u)"
@ -19,13 +19,13 @@ let
fi fi
''; '';
in in
buildWithCargo { buildWithCargo (args // {
inherit src; inherit src;
doCopyTargetToOutput = false; doCopyTargetToOutput = false;
# NB: explicit call here so that the buildDepsOnly call # NB: explicit call here so that the buildDepsOnly call
# doesn't inherit our build commands # doesn't inherit our build commands
cargoArtifacts = buildDepsOnly { inherit src; }; cargoArtifacts = buildDepsOnly (args // { inherit src; });
nativeBuildInputs = [ jq ]; nativeBuildInputs = [ jq ];
@ -49,4 +49,4 @@ buildWithCargo {
installPhase = '' installPhase = ''
touch $out touch $out
''; '';
} })

View File

@ -17,7 +17,7 @@ pkgs.lib.makeScope myLib.newScope (self:
cmpCleanCargoTomlComplex = self.cmpCleanCargoToml ./cleanCargoToml/complex; cmpCleanCargoTomlComplex = self.cmpCleanCargoToml ./cleanCargoToml/complex;
compilesFresh = callPackage ./compilesFresh.nix { }; compilesFresh = callPackage ./compilesFresh.nix { };
compilesFreshSimple = self.compilesFresh ./simple "simple"; compilesFreshSimple = self.compilesFresh ./simple "simple" { };
compilesFreshOverlappingTargets = self.compilesFresh compilesFreshOverlappingTargets = self.compilesFresh
./overlapping-targets ./overlapping-targets
(builtins.concatStringsSep "\n" [ (builtins.concatStringsSep "\n" [
@ -25,7 +25,8 @@ pkgs.lib.makeScope myLib.newScope (self:
"baz" "baz"
"foo" "foo"
"overlapping-targets" "overlapping-targets"
]); ])
{ };
customCargoTargetDirectory = customCargoTargetDirectory =
let let
@ -45,65 +46,7 @@ pkgs.lib.makeScope myLib.newScope (self:
src = ./various-targets; src = ./various-targets;
}; };
featuresDefault = features = callPackage ./features { };
let
crate = myLib.buildPackage {
src = ./features;
};
in
pkgs.runCommand "featuresDefault" { } ''
set -x
[[ "hello" == "$(${crate}/bin/features)" ]]
touch $out
'';
featuresFoo =
let
crate = myLib.buildPackage {
src = ./features;
cargoExtraArgs = "--features foo";
};
in
pkgs.runCommand "featuresFoo" { } ''
[[ "$(echo -e 'hello\nfoo')" == "$(${crate}/bin/features)" ]]
touch $out
'';
featuresBar =
let
crate = myLib.buildPackage {
src = ./features;
cargoExtraArgs = "--features bar";
};
in
pkgs.runCommand "featuresBar" { } ''
[[ "$(echo -e 'hello\nbar')" == "$(${crate}/bin/features)" ]]
touch $out
'';
featuresFooBar =
let
crate = myLib.buildPackage {
src = ./features;
cargoExtraArgs = "--features 'foo bar'";
};
in
pkgs.runCommand "featuresFoo" { } ''
[[ "$(echo -e 'hello\nfoo\nbar')" == "$(${crate}/bin/features)" ]]
touch $out
'';
featuresAll =
let
crate = myLib.buildPackage {
src = ./features;
cargoExtraArgs = "--all-features";
};
in
pkgs.runCommand "featuresAll" { } ''
[[ "$(echo -e 'hello\nfoo\nbar')" == "$(${crate}/bin/features)" ]]
touch $out
'';
manyLibs = myLib.buildPackage { manyLibs = myLib.buildPackage {
src = ./with-libs; src = ./with-libs;

View File

@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "features"
version = "0.1.0"

View File

@ -1,9 +0,0 @@
[package]
name = "features"
version = "0.1.0"
edition = "2021"
[features]
default = []
foo = []
bar = []

View File

@ -0,0 +1,62 @@
{ buildPackage
, compilesFresh
, lib
, linkFarmFromDrvs
, runCommand
}:
let
mkTests = { name, cargoExtraArgs, runResult }:
let
crate = buildPackage {
inherit cargoExtraArgs name;
src = ./features;
};
in
[
crate
(compilesFresh ./features "features" {
inherit cargoExtraArgs;
name = "${name}CompilesFresh";
})
(runCommand "${name}Run" { } ''
[[ "hello${runResult}" == "$(${crate}/bin/features)" ]]
touch $out
'')
];
tests = [
(mkTests {
name = "default";
cargoExtraArgs = "";
runResult = "";
})
(mkTests {
name = "foo";
cargoExtraArgs = "--features foo";
runResult = "\nfoo";
})
(mkTests {
name = "bar";
cargoExtraArgs = "--features bar";
runResult = "\nbar";
})
(mkTests {
name = "fooBar";
cargoExtraArgs = "--features 'foo bar'";
runResult = "\nfoo\nbar";
})
(mkTests {
name = "all";
cargoExtraArgs = "--all-features";
runResult = "\nfoo\nbar";
})
];
in
linkFarmFromDrvs "features" (lib.flatten tests)

49
tests/features/features/Cargo.lock generated Normal file
View File

@ -0,0 +1,49 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "features"
version = "0.1.0"
dependencies = [
"libc",
"regex",
]
[[package]]
name = "libc"
version = "0.2.112"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125"
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"

View File

@ -0,0 +1,13 @@
[package]
name = "features"
version = "0.1.0"
edition = "2021"
[features]
default = []
foo = ["libc"]
bar = ["regex"]
[dependencies]
libc = { version = "*", optional = true }
regex = { version = "*", optional = true }