From 3347d40b927ea1738df60985617b92ab2af8cb65 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 5 Aug 2026 14:57:45 +0000 Subject: [PATCH] Avoid leaking subprocess test temp directories Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> --- cmd/datdump/main_test.go | 8 ++++---- main_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/datdump/main_test.go b/cmd/datdump/main_test.go index b6258c23..fc9dae1a 100644 --- a/cmd/datdump/main_test.go +++ b/cmd/datdump/main_test.go @@ -12,7 +12,7 @@ import ( "google.golang.org/protobuf/proto" ) -// mainExitEnv marks the re-executed test binary that has to run main(). +// mainExitEnv stores the missing input path for the re-executed test binary. const mainExitEnv = "DATDUMP_TEST_MAIN_EXIT" func TestLoadGeosite(t *testing.T) { @@ -244,13 +244,13 @@ func TestMainExportsLists(t *testing.T) { // TestMainExitsOnError re-executes the test binary, because main() terminates // the process when the export fails. func TestMainExitsOnError(t *testing.T) { - if os.Getenv(mainExitEnv) == "1" { - os.Args = []string{"datdump", "--inputdata=" + filepath.Join(t.TempDir(), "missing.dat")} + if missingPath := os.Getenv(mainExitEnv); missingPath != "" { + os.Args = []string{"datdump", "--inputdata=" + missingPath} main() return } cmd := exec.Command(os.Args[0], "-test.run=^TestMainExitsOnError$") - cmd.Env = append(os.Environ(), mainExitEnv+"=1") + cmd.Env = append(os.Environ(), mainExitEnv+"="+filepath.Join(t.TempDir(), "missing.dat")) out, err := cmd.CombinedOutput() var exitErr *exec.ExitError if !errors.As(err, &exitErr) || exitErr.ExitCode() != 1 { diff --git a/main_test.go b/main_test.go index b9e075f6..a71833cc 100644 --- a/main_test.go +++ b/main_test.go @@ -14,7 +14,7 @@ import ( "google.golang.org/protobuf/proto" ) -// mainExitEnv marks the re-executed test binary that has to run main(). +// mainExitEnv stores the missing data path for the re-executed test binary. const mainExitEnv = "DLC_TEST_MAIN_EXIT" // datList is a list and its rules read back from a generated dat file. @@ -667,13 +667,13 @@ func TestMainGeneratesDat(t *testing.T) { // TestMainExitsOnError re-executes the test binary, because main() terminates // the process when the generation fails. func TestMainExitsOnError(t *testing.T) { - if os.Getenv(mainExitEnv) == "1" { - os.Args = []string{"domain-list-community", "--datapath=" + filepath.Join(t.TempDir(), "missing")} + if missingPath := os.Getenv(mainExitEnv); missingPath != "" { + os.Args = []string{"domain-list-community", "--datapath=" + missingPath} main() return } cmd := exec.Command(os.Args[0], "-test.run=^TestMainExitsOnError$") - cmd.Env = append(os.Environ(), mainExitEnv+"=1") + cmd.Env = append(os.Environ(), mainExitEnv+"="+filepath.Join(t.TempDir(), "missing")) out, err := cmd.CombinedOutput() var exitErr *exec.ExitError if !errors.As(err, &exitErr) || exitErr.ExitCode() != 1 {