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

Changed files
+83 -3
doc
languages-frameworks
pkgs
build-support
go
tests
build-test-binaries
test
+6
doc/languages-frameworks/go.section.md
···
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}
+3
doc/redirects.json
···
"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"
],
+19 -3
pkgs/build-support/go/module.nix
···
# 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:
{
···
export NIX_BUILD_CORES=1
fi
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
''
+ lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
···
''
);
-
doCheck = args.doCheck or true;
+
doCheck = args.doCheck or (!buildTestBinaries);
checkPhase =
args.checkPhase or ''
runHook preCheck
+9
pkgs/build-support/go/tests.nix
···
+
{
+
lib,
+
callPackage,
+
}:
+
+
lib.packagesFromDirectoryRecursive {
+
inherit callPackage;
+
directory = ./tests;
+
}
+3
pkgs/build-support/go/tests/build-test-binaries/go.mod
···
+
module build-test-binaries
+
+
go 1.24
+7
pkgs/build-support/go/tests/build-test-binaries/main_test.go
···
+
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
···
+
{
+
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"
+
''
+2
pkgs/test/default.nix
···
php = recurseIntoAttrs (callPackages ./php { });
+
go = recurseIntoAttrs (callPackage ../build-support/go/tests.nix { });
+
pkg-config = recurseIntoAttrs (callPackage ../top-level/pkg-config/tests.nix { }) // {
__recurseIntoDerivationForReleaseJobs = true;
};