1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-07-15 01:34:38 +02:00

test: use T.TempDir to create temporary test directory (#3721)

This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.

Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}
is also tedious, but `t.TempDir` handles this for us nicely.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
This commit is contained in:
Eng Zer Jun
2022-07-12 21:19:12 +08:00
committed by GitHub
parent b7c0831b7f
commit 0f4e30e9db
47 changed files with 245 additions and 723 deletions

View File

@ -613,11 +613,9 @@ func TestReportGolangTestCoverage(t *testing.T) {
func TestPrepareLdflags(t *testing.T) {
t.Parallel()
dir, err := ioutil.TempDir("", "")
defer os.RemoveAll(dir) // clean up
assert.NoError(t, err, "Error when creating temp dir")
dir := t.TempDir()
err = os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0777)
err := os.Mkdir(filepath.Join(dir, "commonPipelineEnvironment"), 0777)
assert.NoError(t, err, "Error when creating folder structure")
err = ioutil.WriteFile(filepath.Join(dir, "commonPipelineEnvironment", "artifactVersion"), []byte("1.2.3"), 0666)