diff --git a/build_defs/go.build_defs b/build_defs/go.build_defs index 8de215fd..e79bafed 100644 --- a/build_defs/go.build_defs +++ b/build_defs/go.build_defs @@ -850,7 +850,7 @@ def go_test(name:str, srcs:list, resources:list=None, data:list|dict=None, deps: cmds, tools = _go_binary_cmds(name, static=static, definitions=definitions, gcov=cgo, test=True) - test_cmd = f'$TEST {flags} 2>&1 | tee $TMP_DIR/test.results' + test_cmd = f'$TEST {flags} 2>&1 | tee -- "$TMP_DIR/test.results"' worker_cmd = f'$(worker {worker})' if worker else "" if worker_cmd: test_cmd = f'{worker_cmd} && {test_cmd} ' @@ -1747,7 +1747,12 @@ def _go_binary_cmds(name, static=False, ldflags='', pkg_config='', definitions=N elif isinstance(definitions, dict): linkerdefs = [k if v is None else f'{k}={v}' for k, v in sorted(definitions.items())] if test: - linkerdefs += ["testing.testBinary=1"] + # testing.testBinary and testing.testing are required by the Go test runtime. + # Match go test: testing/testing.go:692 (Go 1.25.3) relies on testing.testing=true when linking tests. + if "testing.testBinary=1" not in linkerdefs: + linkerdefs += ["testing.testBinary=1"] + if "testing.testing=true" not in linkerdefs: + linkerdefs += ["testing.testing=true"] defs = ' '.join([f'-X "{linkerdef}"' for linkerdef in linkerdefs]) diff --git a/test/build_defs/e2e.build_defs b/test/build_defs/e2e.build_defs index a68ed0f9..e43ae9d9 100644 --- a/test/build_defs/e2e.build_defs +++ b/test/build_defs/e2e.build_defs @@ -2,11 +2,22 @@ subinclude("///e2e//build_defs:e2e") _please_repo_e2e_test = please_repo_e2e_test -def please_repo_e2e_test(name, expected_output=None, plz_command, repo, labels=[]): +def please_repo_e2e_test(name, expected_output=None, plz_command, repo, labels=[], expect_stdout_contains=None, expect_stderr_contains=None): + plz_cmd = plz_command.replace("plz ", "plz -o please.PluginRepo:file://$TMP_DIR/$DATA_PLUGIN_REPO -o plugin.go.gotool:$TOOLS_GO -o plugin.go.pleasegotool:$TOOLS_PLEASE_GO ") + # Tee stdout and stderr to separate files while preserving the exit code. + wrapped_plz_command = f"( {plz_cmd} > >(tee stdout.log) 2> >(tee stderr.log >&2) ); STATUS=$?; exit $STATUS" + + expect_output_contains = {} + if expect_stdout_contains: + expect_output_contains["stdout.log"] = expect_stdout_contains + if expect_stderr_contains: + expect_output_contains["stderr.log"] = expect_stderr_contains + return _please_repo_e2e_test( name = name, expected_output = expected_output, - plz_command = plz_command.replace("plz ", "plz -o please.PluginRepo:file://$TMP_DIR/$DATA_PLUGIN_REPO -o plugin.go.gotool:$TOOLS_GO -o plugin.go.pleasegotool:$TOOLS_PLEASE_GO "), + expect_output_contains = expect_output_contains if expect_output_contains else None, + plz_command = wrapped_plz_command, repo = repo, data = { "PLUGIN_REPO": ["//test:export"], diff --git a/test/testing_definition/BUILD b/test/testing_definition/BUILD index 4bdd6436..2ea4549f 100644 --- a/test/testing_definition/BUILD +++ b/test/testing_definition/BUILD @@ -1,4 +1,5 @@ subinclude("//build_defs:go") +subinclude("//test/build_defs:e2e") go_test( name = "testing_definition_test", @@ -7,3 +8,10 @@ go_test( "///third_party/go/github.com_stretchr_testify//assert", ], ) + +please_repo_e2e_test( + name = "testing_definition_integration_test", + plz_command = "plz test --show_all_output //sample:testing_flag_test -- -test.v", + repo = "test_repo", + expect_stdout_contains = "testing.Testing() = true", +) diff --git a/test/testing_definition/test_repo/.plzconfig b/test/testing_definition/test_repo/.plzconfig new file mode 100644 index 00000000..349fc964 --- /dev/null +++ b/test/testing_definition/test_repo/.plzconfig @@ -0,0 +1,7 @@ +[Parse] +BuildFileName = BUILD_FILE +PreloadSubincludes = ///go//build_defs:go + +[Plugin "go"] +Target = //plugins:go +Stdlib = //third_party/go:std diff --git a/test/testing_definition/test_repo/plugins/BUILD_FILE b/test/testing_definition/test_repo/plugins/BUILD_FILE new file mode 100644 index 00000000..763befc7 --- /dev/null +++ b/test/testing_definition/test_repo/plugins/BUILD_FILE @@ -0,0 +1,4 @@ +plugin_repo( + name = "go", + revision = "master", +) diff --git a/test/testing_definition/test_repo/sample/BUILD_FILE b/test/testing_definition/test_repo/sample/BUILD_FILE new file mode 100644 index 00000000..ea5b4980 --- /dev/null +++ b/test/testing_definition/test_repo/sample/BUILD_FILE @@ -0,0 +1,4 @@ +go_test( + name = "testing_flag_test", + srcs = ["testing_flag_test.go"], +) diff --git a/test/testing_definition/test_repo/sample/testing_flag_test.go b/test/testing_definition/test_repo/sample/testing_flag_test.go new file mode 100644 index 00000000..45afddfc --- /dev/null +++ b/test/testing_definition/test_repo/sample/testing_flag_test.go @@ -0,0 +1,13 @@ +package sample + +import ( + "fmt" + "testing" +) + +func TestTestingFlag(t *testing.T) { + fmt.Printf("testing.Testing() = %v\n", testing.Testing()) + if !testing.Testing() { + t.Fatalf("expected testing.Testing() to be true") + } +} diff --git a/test/testing_definition/test_repo/third_party/go/BUILD_FILE b/test/testing_definition/test_repo/third_party/go/BUILD_FILE new file mode 100644 index 00000000..f6d6b9a1 --- /dev/null +++ b/test/testing_definition/test_repo/third_party/go/BUILD_FILE @@ -0,0 +1,3 @@ +go_stdlib( + name = "std", +)