Skip to content

Commit 236f6ad

Browse files
authored
Merge pull request #357 from nix-community/mkDerivation-for-tests
Fix: Use mkDerivation with src instead of runCommand for test derivation
2 parents a6ca1e5 + e16026b commit 236f6ad

File tree

5 files changed

+168
-223
lines changed

5 files changed

+168
-223
lines changed

crate2nix/Cargo.nix

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,52 +3248,41 @@ rec {
32483248
testPostRun
32493249
]);
32503250
in
3251-
pkgs.runCommand "run-tests-${testCrate.name}"
3252-
{
3253-
inherit testCrateFlags;
3254-
buildInputs = testInputs;
3255-
} ''
3256-
set -e
3257-
3258-
export RUST_BACKTRACE=1
3259-
3260-
# recreate a file hierarchy as when running tests with cargo
3261-
3262-
# the source for test data
3263-
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
3264-
# instead of $NIX_BUILD_TOP/
3265-
# because we compiled those test binaries in the former and not the latter.
3266-
# So all paths will expect source tree to be there and not in the build top directly.
3267-
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
3268-
# NOTE: There could be edge cases if `crate.sourceRoot` does exist but
3269-
# it's very hard to reason about them.
3270-
# Open a bug if you run into this!
3271-
mkdir -p source/
3272-
cd source/
3273-
3274-
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
3275-
3276-
# build outputs
3277-
testRoot=target/debug
3278-
mkdir -p $testRoot
3279-
3280-
# executables of the crate
3281-
# we copy to prevent std::env::current_exe() to resolve to a store location
3282-
for i in ${crate}/bin/*; do
3283-
cp "$i" "$testRoot"
3284-
done
3285-
chmod +w -R .
3286-
3287-
# test harness executables are suffixed with a hash, like cargo does
3288-
# this allows to prevent name collision with the main
3289-
# executables of the crate
3290-
hash=$(basename $out)
3291-
for file in ${drv}/tests/*; do
3292-
f=$testRoot/$(basename $file)-$hash
3293-
cp $file $f
3294-
${testCommand}
3295-
done
3296-
'';
3251+
pkgs.stdenvNoCC.mkDerivation {
3252+
name = "run-tests-${testCrate.name}";
3253+
3254+
inherit (crate) src;
3255+
3256+
inherit testCrateFlags;
3257+
3258+
buildInputs = testInputs;
3259+
3260+
buildPhase = ''
3261+
set -e
3262+
export RUST_BACKTRACE=1
3263+
3264+
# build outputs
3265+
testRoot=target/debug
3266+
mkdir -p $testRoot
3267+
3268+
# executables of the crate
3269+
# we copy to prevent std::env::current_exe() to resolve to a store location
3270+
for i in ${crate}/bin/*; do
3271+
cp "$i" "$testRoot"
3272+
done
3273+
chmod +w -R .
3274+
3275+
# test harness executables are suffixed with a hash, like cargo does
3276+
# this allows to prevent name collision with the main
3277+
# executables of the crate
3278+
hash=$(basename $out)
3279+
for file in ${drv}/tests/*; do
3280+
f=$testRoot/$(basename $file)-$hash
3281+
cp $file $f
3282+
${testCommand}
3283+
done
3284+
'';
3285+
};
32973286
in
32983287
pkgs.runCommand "${crate.name}-linked"
32993288
{

crate2nix/templates/nix/crate2nix/default.nix

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -120,52 +120,41 @@ rec {
120120
testPostRun
121121
]);
122122
in
123-
pkgs.runCommand "run-tests-${testCrate.name}"
124-
{
125-
inherit testCrateFlags;
126-
buildInputs = testInputs;
127-
} ''
128-
set -e
129-
130-
export RUST_BACKTRACE=1
131-
132-
# recreate a file hierarchy as when running tests with cargo
133-
134-
# the source for test data
135-
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
136-
# instead of $NIX_BUILD_TOP/
137-
# because we compiled those test binaries in the former and not the latter.
138-
# So all paths will expect source tree to be there and not in the build top directly.
139-
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
140-
# NOTE: There could be edge cases if `crate.sourceRoot` does exist but
141-
# it's very hard to reason about them.
142-
# Open a bug if you run into this!
143-
mkdir -p source/
144-
cd source/
145-
146-
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
147-
148-
# build outputs
149-
testRoot=target/debug
150-
mkdir -p $testRoot
151-
152-
# executables of the crate
153-
# we copy to prevent std::env::current_exe() to resolve to a store location
154-
for i in ${crate}/bin/*; do
155-
cp "$i" "$testRoot"
156-
done
157-
chmod +w -R .
158-
159-
# test harness executables are suffixed with a hash, like cargo does
160-
# this allows to prevent name collision with the main
161-
# executables of the crate
162-
hash=$(basename $out)
163-
for file in ${drv}/tests/*; do
164-
f=$testRoot/$(basename $file)-$hash
165-
cp $file $f
166-
${testCommand}
167-
done
168-
'';
123+
pkgs.stdenvNoCC.mkDerivation {
124+
name = "run-tests-${testCrate.name}";
125+
126+
inherit (crate) src;
127+
128+
inherit testCrateFlags;
129+
130+
buildInputs = testInputs;
131+
132+
buildPhase = ''
133+
set -e
134+
export RUST_BACKTRACE=1
135+
136+
# build outputs
137+
testRoot=target/debug
138+
mkdir -p $testRoot
139+
140+
# executables of the crate
141+
# we copy to prevent std::env::current_exe() to resolve to a store location
142+
for i in ${crate}/bin/*; do
143+
cp "$i" "$testRoot"
144+
done
145+
chmod +w -R .
146+
147+
# test harness executables are suffixed with a hash, like cargo does
148+
# this allows to prevent name collision with the main
149+
# executables of the crate
150+
hash=$(basename $out)
151+
for file in ${drv}/tests/*; do
152+
f=$testRoot/$(basename $file)-$hash
153+
cp $file $f
154+
${testCommand}
155+
done
156+
'';
157+
};
169158
in
170159
pkgs.runCommand "${crate.name}-linked"
171160
{

sample_projects/bin_with_git_submodule_dep/Cargo.nix

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,52 +1466,41 @@ rec {
14661466
testPostRun
14671467
]);
14681468
in
1469-
pkgs.runCommand "run-tests-${testCrate.name}"
1470-
{
1471-
inherit testCrateFlags;
1472-
buildInputs = testInputs;
1473-
} ''
1474-
set -e
1475-
1476-
export RUST_BACKTRACE=1
1477-
1478-
# recreate a file hierarchy as when running tests with cargo
1479-
1480-
# the source for test data
1481-
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
1482-
# instead of $NIX_BUILD_TOP/
1483-
# because we compiled those test binaries in the former and not the latter.
1484-
# So all paths will expect source tree to be there and not in the build top directly.
1485-
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
1486-
# NOTE: There could be edge cases if `crate.sourceRoot` does exist but
1487-
# it's very hard to reason about them.
1488-
# Open a bug if you run into this!
1489-
mkdir -p source/
1490-
cd source/
1491-
1492-
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
1493-
1494-
# build outputs
1495-
testRoot=target/debug
1496-
mkdir -p $testRoot
1497-
1498-
# executables of the crate
1499-
# we copy to prevent std::env::current_exe() to resolve to a store location
1500-
for i in ${crate}/bin/*; do
1501-
cp "$i" "$testRoot"
1502-
done
1503-
chmod +w -R .
1504-
1505-
# test harness executables are suffixed with a hash, like cargo does
1506-
# this allows to prevent name collision with the main
1507-
# executables of the crate
1508-
hash=$(basename $out)
1509-
for file in ${drv}/tests/*; do
1510-
f=$testRoot/$(basename $file)-$hash
1511-
cp $file $f
1512-
${testCommand}
1513-
done
1514-
'';
1469+
pkgs.stdenvNoCC.mkDerivation {
1470+
name = "run-tests-${testCrate.name}";
1471+
1472+
inherit (crate) src;
1473+
1474+
inherit testCrateFlags;
1475+
1476+
buildInputs = testInputs;
1477+
1478+
buildPhase = ''
1479+
set -e
1480+
export RUST_BACKTRACE=1
1481+
1482+
# build outputs
1483+
testRoot=target/debug
1484+
mkdir -p $testRoot
1485+
1486+
# executables of the crate
1487+
# we copy to prevent std::env::current_exe() to resolve to a store location
1488+
for i in ${crate}/bin/*; do
1489+
cp "$i" "$testRoot"
1490+
done
1491+
chmod +w -R .
1492+
1493+
# test harness executables are suffixed with a hash, like cargo does
1494+
# this allows to prevent name collision with the main
1495+
# executables of the crate
1496+
hash=$(basename $out)
1497+
for file in ${drv}/tests/*; do
1498+
f=$testRoot/$(basename $file)-$hash
1499+
cp $file $f
1500+
${testCommand}
1501+
done
1502+
'';
1503+
};
15151504
in
15161505
pkgs.runCommand "${crate.name}-linked"
15171506
{

sample_projects/codegen/Cargo.nix

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -641,52 +641,41 @@ rec {
641641
testPostRun
642642
]);
643643
in
644-
pkgs.runCommand "run-tests-${testCrate.name}"
645-
{
646-
inherit testCrateFlags;
647-
buildInputs = testInputs;
648-
} ''
649-
set -e
644+
pkgs.stdenvNoCC.mkDerivation {
645+
name = "run-tests-${testCrate.name}";
650646

651-
export RUST_BACKTRACE=1
647+
inherit (crate) src;
652648

653-
# recreate a file hierarchy as when running tests with cargo
649+
inherit testCrateFlags;
654650

655-
# the source for test data
656-
# It's necessary to locate the source in $NIX_BUILD_TOP/source/
657-
# instead of $NIX_BUILD_TOP/
658-
# because we compiled those test binaries in the former and not the latter.
659-
# So all paths will expect source tree to be there and not in the build top directly.
660-
# For example: $NIX_BUILD_TOP := /build in general, if you ask yourself.
661-
# NOTE: There could be edge cases if `crate.sourceRoot` does exist but
662-
# it's very hard to reason about them.
663-
# Open a bug if you run into this!
664-
mkdir -p source/
665-
cd source/
651+
buildInputs = testInputs;
666652

667-
${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
653+
buildPhase = ''
654+
set -e
655+
export RUST_BACKTRACE=1
668656
669-
# build outputs
670-
testRoot=target/debug
671-
mkdir -p $testRoot
657+
# build outputs
658+
testRoot=target/debug
659+
mkdir -p $testRoot
672660
673-
# executables of the crate
674-
# we copy to prevent std::env::current_exe() to resolve to a store location
675-
for i in ${crate}/bin/*; do
676-
cp "$i" "$testRoot"
677-
done
678-
chmod +w -R .
661+
# executables of the crate
662+
# we copy to prevent std::env::current_exe() to resolve to a store location
663+
for i in ${crate}/bin/*; do
664+
cp "$i" "$testRoot"
665+
done
666+
chmod +w -R .
679667
680-
# test harness executables are suffixed with a hash, like cargo does
681-
# this allows to prevent name collision with the main
682-
# executables of the crate
683-
hash=$(basename $out)
684-
for file in ${drv}/tests/*; do
685-
f=$testRoot/$(basename $file)-$hash
686-
cp $file $f
687-
${testCommand}
688-
done
689-
'';
668+
# test harness executables are suffixed with a hash, like cargo does
669+
# this allows to prevent name collision with the main
670+
# executables of the crate
671+
hash=$(basename $out)
672+
for file in ${drv}/tests/*; do
673+
f=$testRoot/$(basename $file)-$hash
674+
cp $file $f
675+
${testCommand}
676+
done
677+
'';
678+
};
690679
in
691680
pkgs.runCommand "${crate.name}-linked"
692681
{

0 commit comments

Comments
 (0)