Skip to content

Commit 6634a00

Browse files
committed
all: make use of t.TempDir and t.Setenv
They were added in Go 1.15 and 1.17, respectively. Now that we require Go 1.17 or later, use them. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Id114de6283c1fbadf7f30472f662a238d8a24305 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/537852 Unity-Result: CUEcueckoo <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 1ed6195 commit 6634a00

File tree

7 files changed

+11
-23
lines changed

7 files changed

+11
-23
lines changed

cmd/cue/cmd/script_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ func TestX(t *testing.T) {
131131
}
132132
}
133133

134-
tmpdir, err := ioutil.TempDir("", "cue-script")
135-
check(err)
136-
defer os.Remove(tmpdir)
134+
tmpdir := t.TempDir()
137135

138136
a, err := txtar.ParseFile(filepath.FromSlash(path))
139137
check(err)

cue/context_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cue_test
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"testing"
2120

2221
"cuelang.org/go/cue"
@@ -99,7 +98,7 @@ bar: [
9998
`
10099

101100
a := txtar.Parse([]byte(in))
102-
dir, _ := ioutil.TempDir("", "*")
101+
dir := t.TempDir()
103102
instance := cuetxtar.Load(a, dir)[0]
104103
if instance.Err != nil {
105104
t.Fatal(instance.Err)
@@ -125,7 +124,7 @@ bar: [
125124
`
126125

127126
a := txtar.Parse([]byte(in))
128-
dir, _ := ioutil.TempDir("", "*")
127+
dir := t.TempDir()
129128
instance := cuetxtar.Load(a, dir)[0]
130129

131130
// Normally, this should be checked, however, this is explicitly

cue/load/tags_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package load
1616

1717
import (
1818
"bytes"
19-
"io/ioutil"
20-
"os"
2119
"path/filepath"
2220
"testing"
2321

@@ -43,8 +41,7 @@ func stringVar(s string) TagVar {
4341
}
4442

4543
func TestTags(t *testing.T) {
46-
dir, _ := ioutil.TempDir("", "")
47-
defer os.RemoveAll(dir)
44+
dir := t.TempDir()
4845

4946
testCases := []struct {
5047
in string

cue/query_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package cue_test
1616

1717
import (
1818
"bytes"
19-
"io/ioutil"
2019
"testing"
2120

2221
"cuelang.org/go/cue"
@@ -163,7 +162,7 @@ _d: 3
163162
`
164163

165164
a := txtar.Parse([]byte(in))
166-
dir, _ := ioutil.TempDir("", "*")
165+
dir := t.TempDir()
167166
instance := cuetxtar.Load(a, dir)[0]
168167
if instance.Err != nil {
169168
t.Fatal(instance.Err)

internal/cuetest/cuetest_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cuetest
22

33
import (
4-
"os"
54
"testing"
65
)
76

@@ -30,7 +29,7 @@ func TestCondition(t *testing.T) {
3029

3130
for _, c := range cases {
3231
t.Run(c.name, func(t *testing.T) {
33-
os.Setenv(envNonIssues, c.env)
32+
t.Setenv(envNonIssues, c.env)
3433
got, err := Condition(c.con)
3534
if got != c.want {
3635
t.Errorf("expected %v; got %v", c.want, got)
@@ -85,7 +84,7 @@ func TestCheckIssueCondition(t *testing.T) {
8584
}
8685
for _, c := range cases {
8786
t.Run(c.name, func(t *testing.T) {
88-
os.Setenv(envNonIssues, c.env)
87+
t.Setenv(envNonIssues, c.env)
8988
isIssue, nonIssue, err := checkIssueCondition(c.con)
9089
if isIssue != c.isIssue {
9190
t.Errorf("expected isIssue %v; got %v", c.isIssue, isIssue)

pkg/path/path_windows_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
4848
perm = 0700
4949
)
5050

51-
tmp, err := ioutil.TempDir("", "testWinSplitListTestIsValid")
52-
if err != nil {
53-
t.Fatalf("TempDir failed: %v", err)
54-
}
55-
defer goos.RemoveAll(tmp)
51+
tmp := t.TempDir()
5652

5753
for i, d := range tt.result {
5854
if d == "" {
@@ -68,12 +64,12 @@ func testWinSplitListTestIsValid(t *testing.T, ti int, tt SplitListTest,
6864
t.Errorf("%d,%d: %#q already exists", ti, i, d)
6965
return
7066
}
71-
if err = goos.MkdirAll(dd, perm); err != nil {
67+
if err := goos.MkdirAll(dd, perm); err != nil {
7268
t.Errorf("%d,%d: MkdirAll(%#q) failed: %v", ti, i, dd, err)
7369
return
7470
}
7571
fn, data := Join([]string{dd, cmdfile}, Windows), []byte("@echo "+d+"\r\n")
76-
if err = ioutil.WriteFile(fn, data, perm); err != nil {
72+
if err := ioutil.WriteFile(fn, data, perm); err != nil {
7773
t.Errorf("%d,%d: WriteFile(%#q) failed: %v", ti, i, fn, err)
7874
return
7975
}

pkg/tool/os/env_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestGetenv(t *testing.T) {
4141
{"CUEOSTESTNUMD", "not a num"},
4242
{"CUEOSTESTMULTI", "10"},
4343
} {
44-
os.Setenv(p[0], p[1])
44+
t.Setenv(p[0], p[1])
4545
}
4646

4747
config := `{

0 commit comments

Comments
 (0)