buildGoModule: add buildTestBinaries option, add tests (#427334)

This commit is contained in:
Paul Meyer
2025-08-12 10:14:14 +02:00
committed by GitHub
8 changed files with 83 additions and 3 deletions

View File

@@ -192,6 +192,12 @@ Specifies the contents of the `go.sum` file and triggers rebuilds when it change
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}

View File

@@ -613,6 +613,9 @@
"typst-package-scope-and-usage": [
"index.html#typst-package-scope-and-usage"
],
"var-go-buildTestBinaries": [
"index.html#var-go-buildTestBinaries"
],
"var-meta-teams": [
"index.html#var-meta-teams"
],

View File

@@ -64,6 +64,12 @@ lib.extendMkDerivation {
# Go build flags.
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:
{
@@ -338,8 +344,18 @@ lib.extendMkDerivation {
export NIX_BUILD_CORES=1
fi
for pkg in $(getGoDirs ""); do
${
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
''
+ lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
@@ -359,7 +375,7 @@ lib.extendMkDerivation {
''
);
doCheck = args.doCheck or true;
doCheck = args.doCheck or (!buildTestBinaries);
checkPhase =
args.checkPhase or ''
runHook preCheck

View File

@@ -0,0 +1,9 @@
{
lib,
callPackage,
}:
lib.packagesFromDirectoryRecursive {
inherit callPackage;
directory = ./tests;
}

View File

@@ -0,0 +1,3 @@
module build-test-binaries
go 1.24

View File

@@ -0,0 +1,7 @@
package main
import "testing"
func TestHelloFromTest(t *testing.T) {
t.Log("Hello from test")
}

View 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"
''

View File

@@ -149,6 +149,8 @@ with pkgs;
php = recurseIntoAttrs (callPackages ./php { });
go = recurseIntoAttrs (callPackage ../build-support/go/tests.nix { });
pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { }) // {
__recurseIntoDerivationForReleaseJobs = true;
};