mirror of
https://github.com/v2fly/domain-list-community.git
synced 2026-09-27 22:30:15 +03:00
Avoid leaking subprocess test temp directories
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
This commit is contained in:
parent
99bc33355c
commit
3347d40b92
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user