You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +02:00 
			
		
		
		
	test: use t.Setenv to set env vars (#4140)
				
					
				
			We have been using `t.Setenv` is most of our tests. This PR replaces the remaining `os.Setenv` with `t.Setenv`. Reference: https://pkg.go.dev/testing#T.Setenv ```go func TestFoo(t *testing.T) { // before os.Setenv(key, "new value") defer os.Unsetenv(key) // after t.Setenv(key, "new value") } ``` --------- Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
		| @@ -1,7 +1,6 @@ | ||||
| package cmd | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"runtime" | ||||
| 	"testing" | ||||
|  | ||||
| @@ -181,12 +180,8 @@ func TestBuildFlags(t *testing.T) { | ||||
| 		}) | ||||
|  | ||||
| 		t.Run("from env", func(t *testing.T) { | ||||
| 			os.Setenv("GOOS", "linux") | ||||
| 			os.Setenv("GOARCH", "arm64") | ||||
| 			t.Cleanup(func() { | ||||
| 				os.Unsetenv("GOOS") | ||||
| 				os.Unsetenv("GOARCH") | ||||
| 			}) | ||||
| 			t.Setenv("GOOS", "linux") | ||||
| 			t.Setenv("GOARCH", "arm64") | ||||
| 			result := setup(opts) | ||||
| 			require.Equal(t, []string{"linux"}, result.Config.Builds[0].Goos) | ||||
| 			require.Equal(t, []string{"arm64"}, result.Config.Builds[0].Goarch) | ||||
|   | ||||
| @@ -281,12 +281,9 @@ func createFakeGoBinaryWithVersion(tb testing.TB, name, version string) { | ||||
| 	)) | ||||
|  | ||||
| 	currentPath := os.Getenv("PATH") | ||||
| 	tb.Cleanup(func() { | ||||
| 		require.NoError(tb, os.Setenv("PATH", currentPath)) | ||||
| 	}) | ||||
|  | ||||
| 	path := fmt.Sprintf("%s%c%s", d, os.PathListSeparator, currentPath) | ||||
| 	require.NoError(tb, os.Setenv("PATH", path)) | ||||
| 	tb.Setenv("PATH", path) | ||||
| } | ||||
|  | ||||
| func TestInvalidTargets(t *testing.T) { | ||||
|   | ||||
| @@ -168,7 +168,7 @@ func TestMinioUploadCustomBucketID(t *testing.T) { | ||||
| 	require.NoError(t, os.WriteFile(tgzpath, []byte("fake\ntargz"), 0o744)) | ||||
| 	require.NoError(t, os.WriteFile(debpath, []byte("fake\ndeb"), 0o744)) | ||||
| 	// Set custom BUCKET_ID env variable. | ||||
| 	require.NoError(t, os.Setenv("BUCKET_ID", name)) | ||||
| 	t.Setenv("BUCKET_ID", name) | ||||
| 	ctx := testctx.NewWithCfg(config.Project{ | ||||
| 		Dist:        folder, | ||||
| 		ProjectName: "testupload", | ||||
|   | ||||
| @@ -246,7 +246,7 @@ func TestDefaultFail(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestDefaultExpandEnv(t *testing.T) { | ||||
| 	require.NoError(t, os.Setenv("XBAR", "FOOBAR")) | ||||
| 	t.Setenv("XBAR", "FOOBAR") | ||||
| 	ctx := testctx.NewWithCfg(config.Project{ | ||||
| 		Builds: []config.Build{ | ||||
| 			{ | ||||
|   | ||||
| @@ -283,11 +283,7 @@ func TestSnapshotDirty(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestGitNotInPath(t *testing.T) { | ||||
| 	path := os.Getenv("PATH") | ||||
| 	defer func() { | ||||
| 		require.NoError(t, os.Setenv("PATH", path)) | ||||
| 	}() | ||||
| 	require.NoError(t, os.Setenv("PATH", "")) | ||||
| 	t.Setenv("PATH", "") | ||||
| 	require.EqualError(t, Pipe{}.Run(testctx.New()), ErrNoGit.Error()) | ||||
| } | ||||
|  | ||||
| @@ -310,16 +306,12 @@ func TestTagFromCI(t *testing.T) { | ||||
| 		}, | ||||
| 	} { | ||||
| 		for name, value := range tc.envs { | ||||
| 			require.NoError(t, os.Setenv(name, value)) | ||||
| 			t.Setenv(name, value) | ||||
| 		} | ||||
|  | ||||
| 		ctx := testctx.New() | ||||
| 		require.NoError(t, Pipe{}.Run(ctx)) | ||||
| 		require.Equal(t, tc.expected, ctx.Git.CurrentTag) | ||||
|  | ||||
| 		for name := range tc.envs { | ||||
| 			require.NoError(t, os.Setenv(name, "")) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @@ -357,16 +349,12 @@ func TestPreviousTagFromCI(t *testing.T) { | ||||
| 	} { | ||||
| 		t.Run(tc.expected, func(t *testing.T) { | ||||
| 			for name, value := range tc.envs { | ||||
| 				require.NoError(t, os.Setenv(name, value)) | ||||
| 				t.Setenv(name, value) | ||||
| 			} | ||||
|  | ||||
| 			ctx := testctx.New() | ||||
| 			require.NoError(t, Pipe{}.Run(ctx)) | ||||
| 			require.Equal(t, tc.expected, ctx.Git.PreviousTag) | ||||
|  | ||||
| 			for name := range tc.envs { | ||||
| 				require.NoError(t, os.Setenv(name, "")) | ||||
| 			} | ||||
| 		}) | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -2,7 +2,6 @@ package slack | ||||
|  | ||||
| import ( | ||||
| 	"bytes" | ||||
| 	"os" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/goreleaser/goreleaser/internal/testctx" | ||||
| @@ -98,8 +97,7 @@ func TestParseRichText(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestRichText(t *testing.T) { | ||||
| 	t.Parallel() | ||||
| 	os.Setenv("SLACK_WEBHOOK", slackTestHook()) | ||||
| 	t.Setenv("SLACK_WEBHOOK", slackTestHook()) | ||||
|  | ||||
| 	t.Run("e2e - full slack config with blocks and attachments", func(t *testing.T) { | ||||
| 		t.SkipNow() // requires a valid webhook for integration testing | ||||
|   | ||||
| @@ -319,11 +319,7 @@ func TestRunPipeMetadata(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestNoSnapcraftInPath(t *testing.T) { | ||||
| 	path := os.Getenv("PATH") | ||||
| 	defer func() { | ||||
| 		require.NoError(t, os.Setenv("PATH", path)) | ||||
| 	}() | ||||
| 	require.NoError(t, os.Setenv("PATH", "")) | ||||
| 	t.Setenv("PATH", "") | ||||
| 	ctx := testctx.NewWithCfg(config.Project{ | ||||
| 		Snapcrafts: []config.Snapcraft{ | ||||
| 			{ | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package testlib | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"testing" | ||||
|  | ||||
| 	"github.com/stretchr/testify/require" | ||||
| @@ -15,17 +14,8 @@ func TestCheckPath(t *testing.T) { | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	setupEnv := func(tb testing.TB, value string) { | ||||
| 		tb.Helper() | ||||
| 		previous := os.Getenv("CI") | ||||
| 		require.NoError(tb, os.Setenv("CI", value)) | ||||
| 		tb.Cleanup(func() { | ||||
| 			require.NoError(tb, os.Setenv("CI", previous)) | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	t.Run("local", func(t *testing.T) { | ||||
| 		setupEnv(t, "false") | ||||
| 		t.Setenv("CI", "false") | ||||
|  | ||||
| 		t.Run("in path", func(t *testing.T) { | ||||
| 			requireSkipped(t, false) | ||||
| @@ -39,7 +29,7 @@ func TestCheckPath(t *testing.T) { | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("CI", func(t *testing.T) { | ||||
| 		setupEnv(t, "true") | ||||
| 		t.Setenv("CI", "true") | ||||
|  | ||||
| 		t.Run("in path on CI", func(t *testing.T) { | ||||
| 			requireSkipped(t, false) | ||||
|   | ||||
| @@ -1,7 +1,6 @@ | ||||
| package context | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
| 	"runtime" | ||||
| 	"testing" | ||||
| 	"time" | ||||
| @@ -11,8 +10,8 @@ import ( | ||||
| ) | ||||
|  | ||||
| func TestNew(t *testing.T) { | ||||
| 	require.NoError(t, os.Setenv("FOO", "NOT BAR")) | ||||
| 	require.NoError(t, os.Setenv("BAR", "1")) | ||||
| 	t.Setenv("FOO", "NOT BAR") | ||||
| 	t.Setenv("BAR", "1") | ||||
| 	ctx := New(config.Project{ | ||||
| 		Env: []string{ | ||||
| 			"FOO=BAR", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user