Skip to content

Commit

Permalink
proc,service: simplify tests with T.Setenv (#3503)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Sep 19, 2023
1 parent e072a64 commit 2b9c3d5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/dlv/dlv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func getDlvBin(t *testing.T) string {
// from getDlvBinEBPF lets clear it here so
// we can ensure we don't get build errors
// depending on the test ordering.
os.Setenv("CGO_LDFLAGS", ldFlags)
t.Setenv("CGO_LDFLAGS", ldFlags)
var tags string
if runtime.GOOS == "windows" && runtime.GOARCH == "arm64" {
tags = "-tags=exp.winarm64"
Expand Down
11 changes: 3 additions & 8 deletions pkg/proc/proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2981,7 +2981,7 @@ func TestPackageWithPathVar(t *testing.T) {

func TestEnvironment(t *testing.T) {
protest.AllowRecording(t)
os.Setenv("SOMEVAR", "bah")
t.Setenv("SOMEVAR", "bah")
withTestProcess("testenv", t, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
assertNoError(grp.Continue(), t, "Continue()")
v := evalVariable(p, t, "x")
Expand Down Expand Up @@ -3047,7 +3047,7 @@ func TestIssue877(t *testing.T) {
t.Skip("broken")
}
const envval = "/usr/local/lib"
os.Setenv("DYLD_LIBRARY_PATH", envval)
t.Setenv("DYLD_LIBRARY_PATH", envval)
withTestProcess("issue877", t, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
assertNoError(grp.Continue(), t, "Continue()")
v := evalVariable(p, t, "dyldenv")
Expand Down Expand Up @@ -4479,12 +4479,7 @@ func TestListImages(t *testing.T) {
}

func TestAncestors(t *testing.T) {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 11) {
t.Skip("not supported on Go <= 1.10")
}
savedGodebug := os.Getenv("GODEBUG")
os.Setenv("GODEBUG", "tracebackancestors=100")
defer os.Setenv("GODEBUG", savedGodebug)
t.Setenv("GODEBUG", "tracebackancestors=100")
withTestProcess("testnextprog", t, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
setFunctionBreakpoint(p, t, "main.testgoroutine")
assertNoError(grp.Continue(), t, "Continue()")
Expand Down
2 changes: 1 addition & 1 deletion pkg/terminal/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func withTestTerminalBuildFlags(name string, t testing.TB, buildFlags test.Build
if testBackend == "rr" {
test.MustHaveRecordingAllowed(t)
}
os.Setenv("TERM", "dumb")
t.Setenv("TERM", "dumb")
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("couldn't start listener: %s\n", err)
Expand Down
9 changes: 1 addition & 8 deletions service/dap/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5719,16 +5719,9 @@ func TestLaunchRequestWithEnv(t *testing.T) {
} {

t.Run(tc.name, func(t *testing.T) {
// cleanup
defer func() {
os.Unsetenv("SOMEVAR")
os.Unsetenv("SOMEVAR2")
os.Unsetenv("SOMEVAR3")
}()

for k, v := range tc.initEnv {
if v != nil {
os.Setenv(k, *v)
t.Setenv(k, *v)
}
}

Expand Down
13 changes: 3 additions & 10 deletions service/debugger/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ func TestDebugger_LaunchNoMain(t *testing.T) {
}

func TestDebugger_LaunchInvalidFormat(t *testing.T) {
goos := os.Getenv("GOOS")
goarch := os.Getenv("GOARCH")
defer func() {
// restore environment values
os.Setenv("GOOS", goos)
os.Setenv("GOARCH", goarch)
}()
fixturesDir := protest.FindFixturesDir()
buildtestdir := filepath.Join(fixturesDir, "buildtest")
debugname := "debug"
Expand All @@ -51,12 +44,12 @@ func TestDebugger_LaunchInvalidFormat(t *testing.T) {
"linux": "windows",
}
if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" {
os.Setenv("GOARCH", "amd64")
t.Setenv("GOARCH", "amd64")
}
if runtime.GOARCH == "ppc64le" && runtime.GOOS == "linux" {
os.Setenv("GOARCH", "amd64")
t.Setenv("GOARCH", "amd64")
}
os.Setenv("GOOS", switchOS[runtime.GOOS])
t.Setenv("GOOS", switchOS[runtime.GOOS])
exepath := filepath.Join(buildtestdir, debugname)
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
t.Fatalf("go build error %v", err)
Expand Down
14 changes: 5 additions & 9 deletions service/debugger/debugger_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ import (
)

func TestDebugger_LaunchNoExecutablePerm(t *testing.T) {
defer func() {
os.Setenv("GOOS", runtime.GOOS)
os.Setenv("GOARCH", runtime.GOARCH)
}()
fixturesDir := protest.FindFixturesDir()
buildtestdir := filepath.Join(fixturesDir, "buildtest")
debugname := "debug"
Expand All @@ -34,12 +30,12 @@ func TestDebugger_LaunchNoExecutablePerm(t *testing.T) {
"linux": "windows",
}
if runtime.GOARCH == "arm64" && runtime.GOOS == "linux" {
os.Setenv("GOARCH", "amd64")
t.Setenv("GOARCH", "amd64")
}
if runtime.GOARCH == "ppc64le" && runtime.GOOS == "linux" {
os.Setenv("GOARCH", "amd64")
t.Setenv("GOARCH", "amd64")
}
os.Setenv("GOOS", switchOS[runtime.GOOS])
t.Setenv("GOOS", switchOS[runtime.GOOS])
exepath := filepath.Join(buildtestdir, debugname)
defer os.Remove(exepath)
if err := gobuild.GoBuild(debugname, []string{buildtestdir}, fmt.Sprintf("-o %s", exepath)); err != nil {
Expand All @@ -65,8 +61,8 @@ func TestDebugger_LaunchWithTTY(t *testing.T) {
}
}
// Ensure no env meddling is leftover from previous tests.
os.Setenv("GOOS", runtime.GOOS)
os.Setenv("GOARCH", runtime.GOARCH)
t.Setenv("GOOS", runtime.GOOS)
t.Setenv("GOARCH", runtime.GOARCH)

p, tty, err := pty.Open()
if err != nil {
Expand Down
8 changes: 3 additions & 5 deletions service/test/integration2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func main() {

func TestRestart_rebuild(t *testing.T) {
// In the original fixture file the env var tested for is SOMEVAR.
os.Setenv("SOMEVAR", "bah")
t.Setenv("SOMEVAR", "bah")

withTestClient2Extended("testenv", t, 0, [3]string{}, nil, func(c service.Client, f protest.Fixture) {
<-c.Continue()
Expand All @@ -258,7 +258,7 @@ func TestRestart_rebuild(t *testing.T) {

// First set our new env var and ensure later that the
// modified source code picks it up.
os.Setenv("SOMEMODIFIEDVAR", "foobar")
t.Setenv("SOMEMODIFIEDVAR", "foobar")

// Restart the program, rebuilding from source.
_, err = c.Restart(true)
Expand Down Expand Up @@ -2194,9 +2194,7 @@ func TestAncestors(t *testing.T) {
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 11) {
t.Skip("not supported on Go <= 1.10")
}
savedGodebug := os.Getenv("GODEBUG")
os.Setenv("GODEBUG", "tracebackancestors=100")
defer os.Setenv("GODEBUG", savedGodebug)
t.Setenv("GODEBUG", "tracebackancestors=100")
withTestClient2("testnextprog", t, func(c service.Client) {
_, err := c.CreateBreakpoint(&api.Breakpoint{FunctionName: "main.testgoroutine", Line: -1})
assertNoError(err, t, "CreateBreakpoint")
Expand Down

0 comments on commit 2b9c3d5

Please sign in to comment.