buildGoModule: add buildTestBinaries option, add tests (#427334)
This commit is contained in:
@@ -192,6 +192,12 @@ Specifies the contents of the `go.sum` file and triggers rebuilds when it change
|
|||||||
|
|
||||||
Defaults to `null`
|
Defaults to `null`
|
||||||
|
|
||||||
|
### `buildTestBinaries` {#var-go-buildTestBinaries}
|
||||||
|
|
||||||
|
This option allows to compile test binaries instead of the usual binaries produced by a package.
|
||||||
|
Go can [compile test into binaries](https://pkg.go.dev/cmd/go#hdr-Test_packages) using the `go test -c` command.
|
||||||
|
These binaries can then be executed at a later point (outside the Nix sandbox) to run the tests.
|
||||||
|
This is mostly useful for downstream consumers to run integration or end-to-end tests that won't work in the Nix sandbox, for example because they require network access.
|
||||||
|
|
||||||
## Versioned toolchains and builders {#ssec-go-toolchain-versions}
|
## Versioned toolchains and builders {#ssec-go-toolchain-versions}
|
||||||
|
|
||||||
|
|||||||
@@ -613,6 +613,9 @@
|
|||||||
"typst-package-scope-and-usage": [
|
"typst-package-scope-and-usage": [
|
||||||
"index.html#typst-package-scope-and-usage"
|
"index.html#typst-package-scope-and-usage"
|
||||||
],
|
],
|
||||||
|
"var-go-buildTestBinaries": [
|
||||||
|
"index.html#var-go-buildTestBinaries"
|
||||||
|
],
|
||||||
"var-meta-teams": [
|
"var-meta-teams": [
|
||||||
"index.html#var-meta-teams"
|
"index.html#var-meta-teams"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -64,6 +64,12 @@ lib.extendMkDerivation {
|
|||||||
# Go build flags.
|
# Go build flags.
|
||||||
GOFLAGS ? [ ],
|
GOFLAGS ? [ ],
|
||||||
|
|
||||||
|
# Instead of building binary targets with 'go install', build test binaries with 'go test'.
|
||||||
|
# The binaries found in $out/bin can be executed as go tests outside of the sandbox.
|
||||||
|
# This is mostly useful outside of nixpkgs, for example to build integration/e2e tests
|
||||||
|
# that won't run within the sandbox.
|
||||||
|
buildTestBinaries ? false,
|
||||||
|
|
||||||
...
|
...
|
||||||
}@args:
|
}@args:
|
||||||
{
|
{
|
||||||
@@ -338,8 +344,18 @@ lib.extendMkDerivation {
|
|||||||
export NIX_BUILD_CORES=1
|
export NIX_BUILD_CORES=1
|
||||||
fi
|
fi
|
||||||
for pkg in $(getGoDirs ""); do
|
for pkg in $(getGoDirs ""); do
|
||||||
echo "Building subPackage $pkg"
|
${
|
||||||
buildGoDir install "$pkg"
|
if buildTestBinaries then
|
||||||
|
''
|
||||||
|
echo "Building test binary for $pkg"
|
||||||
|
buildGoDir "test -c -o $GOPATH/bin/" "$pkg"
|
||||||
|
''
|
||||||
|
else
|
||||||
|
''
|
||||||
|
echo "Building subPackage $pkg"
|
||||||
|
buildGoDir install "$pkg"
|
||||||
|
''
|
||||||
|
}
|
||||||
done
|
done
|
||||||
''
|
''
|
||||||
+ lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
|
+ lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
|
||||||
@@ -359,7 +375,7 @@ lib.extendMkDerivation {
|
|||||||
''
|
''
|
||||||
);
|
);
|
||||||
|
|
||||||
doCheck = args.doCheck or true;
|
doCheck = args.doCheck or (!buildTestBinaries);
|
||||||
checkPhase =
|
checkPhase =
|
||||||
args.checkPhase or ''
|
args.checkPhase or ''
|
||||||
runHook preCheck
|
runHook preCheck
|
||||||
|
|||||||
9
pkgs/build-support/go/tests.nix
Normal file
9
pkgs/build-support/go/tests.nix
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
lib,
|
||||||
|
callPackage,
|
||||||
|
}:
|
||||||
|
|
||||||
|
lib.packagesFromDirectoryRecursive {
|
||||||
|
inherit callPackage;
|
||||||
|
directory = ./tests;
|
||||||
|
}
|
||||||
3
pkgs/build-support/go/tests/build-test-binaries/go.mod
Normal file
3
pkgs/build-support/go/tests/build-test-binaries/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module build-test-binaries
|
||||||
|
|
||||||
|
go 1.24
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestHelloFromTest(t *testing.T) {
|
||||||
|
t.Log("Hello from test")
|
||||||
|
}
|
||||||
34
pkgs/build-support/go/tests/build-test-binaries/package.nix
Normal file
34
pkgs/build-support/go/tests/build-test-binaries/package.nix
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
{
|
||||||
|
buildGoModule,
|
||||||
|
runCommandCC,
|
||||||
|
}:
|
||||||
|
|
||||||
|
let
|
||||||
|
testPackage = buildGoModule {
|
||||||
|
name = "build-test-binaries";
|
||||||
|
src = ./.;
|
||||||
|
vendorHash = null;
|
||||||
|
buildTestBinaries = true;
|
||||||
|
};
|
||||||
|
in
|
||||||
|
|
||||||
|
runCommandCC "build-test-binaries-check"
|
||||||
|
{
|
||||||
|
nativeBuildInputs = [ testPackage ];
|
||||||
|
passthru = { inherit testPackage; };
|
||||||
|
}
|
||||||
|
''
|
||||||
|
fail() {
|
||||||
|
echo "Test failed: $1" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
command -v build-test-binaries.test ||
|
||||||
|
fail "build-test-binaries.test not found in PATH"
|
||||||
|
|
||||||
|
build-test-binaries.test -test.v | tee $out ||
|
||||||
|
fail "build-test-binaries.test failed"
|
||||||
|
|
||||||
|
grep -q "Hello from test" $out ||
|
||||||
|
fail "Output does not contain expected string"
|
||||||
|
''
|
||||||
@@ -149,6 +149,8 @@ with pkgs;
|
|||||||
|
|
||||||
php = recurseIntoAttrs (callPackages ./php { });
|
php = recurseIntoAttrs (callPackages ./php { });
|
||||||
|
|
||||||
|
go = recurseIntoAttrs (callPackage ../build-support/go/tests.nix { });
|
||||||
|
|
||||||
pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { }) // {
|
pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { }) // {
|
||||||
__recurseIntoDerivationForReleaseJobs = true;
|
__recurseIntoDerivationForReleaseJobs = true;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user