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

Fix: Use mkDerivation with src instead of runCommand for test derivation #357

Merged
merged 1 commit into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 35 additions & 46 deletions crate2nix/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3248,52 +3248,41 @@ rec {
testPostRun
]);
in
pkgs.runCommand "run-tests-${testCrate.name}"
{
inherit testCrateFlags;
buildInputs = testInputs;
} ''
set -e

export RUST_BACKTRACE=1

# recreate a file hierarchy as when running tests with cargo

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

${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
pkgs.stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;

inherit testCrateFlags;

buildInputs = testInputs;

buildPhase = ''
set -e
export RUST_BACKTRACE=1

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
};
in
pkgs.runCommand "${crate.name}-linked"
{
Expand Down
81 changes: 35 additions & 46 deletions crate2nix/templates/nix/crate2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -120,52 +120,41 @@ rec {
testPostRun
]);
in
pkgs.runCommand "run-tests-${testCrate.name}"
{
inherit testCrateFlags;
buildInputs = testInputs;
} ''
set -e

export RUST_BACKTRACE=1

# recreate a file hierarchy as when running tests with cargo

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

${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
pkgs.stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;

inherit testCrateFlags;

buildInputs = testInputs;

buildPhase = ''
set -e
export RUST_BACKTRACE=1

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
};
in
pkgs.runCommand "${crate.name}-linked"
{
Expand Down
81 changes: 35 additions & 46 deletions sample_projects/bin_with_git_submodule_dep/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -1466,52 +1466,41 @@ rec {
testPostRun
]);
in
pkgs.runCommand "run-tests-${testCrate.name}"
{
inherit testCrateFlags;
buildInputs = testInputs;
} ''
set -e

export RUST_BACKTRACE=1

# recreate a file hierarchy as when running tests with cargo

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

${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
pkgs.stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

inherit (crate) src;

inherit testCrateFlags;

buildInputs = testInputs;

buildPhase = ''
set -e
export RUST_BACKTRACE=1

# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
};
in
pkgs.runCommand "${crate.name}-linked"
{
Expand Down
67 changes: 28 additions & 39 deletions sample_projects/codegen/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -641,52 +641,41 @@ rec {
testPostRun
]);
in
pkgs.runCommand "run-tests-${testCrate.name}"
{
inherit testCrateFlags;
buildInputs = testInputs;
} ''
set -e
pkgs.stdenvNoCC.mkDerivation {
name = "run-tests-${testCrate.name}";

export RUST_BACKTRACE=1
inherit (crate) src;

# recreate a file hierarchy as when running tests with cargo
inherit testCrateFlags;

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

${pkgs.buildPackages.xorg.lndir}/bin/lndir ${crate.src}
buildPhase = ''
set -e
export RUST_BACKTRACE=1

# build outputs
testRoot=target/debug
mkdir -p $testRoot
# build outputs
testRoot=target/debug
mkdir -p $testRoot

# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .
# executables of the crate
# we copy to prevent std::env::current_exe() to resolve to a store location
for i in ${crate}/bin/*; do
cp "$i" "$testRoot"
done
chmod +w -R .

# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
# test harness executables are suffixed with a hash, like cargo does
# this allows to prevent name collision with the main
# executables of the crate
hash=$(basename $out)
for file in ${drv}/tests/*; do
f=$testRoot/$(basename $file)-$hash
cp $file $f
${testCommand}
done
'';
};
in
pkgs.runCommand "${crate.name}-linked"
{
Expand Down
Loading