Skip to content

Commit c7913da

Browse files
committed
fixed working dir issue between tests
1 parent b164fb6 commit c7913da

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

cmd/sql_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,27 @@ func TestTxtar(t *testing.T) {
8282
testPrefix = "test: "
8383
wantPrefix = "want: "
8484
)
85+
var (
86+
join func(name string) (tempPath string)
87+
chTmpDir func()
88+
)
89+
{
90+
tmpPath := t.TempDir()
91+
join = func(name string) (tempPath string) {
92+
return filepath.Join(tmpPath, name)
93+
}
94+
chTmpDir = func() { os.Chdir(tmpPath) }
8595

86-
tmpPath := t.TempDir()
87-
join := func(name string) (tempPath string) {
88-
return filepath.Join(tmpPath, name)
96+
cwd, err := os.Getwd()
97+
if err != nil {
98+
t.Fatalf("could not get working dir: %v", err)
99+
}
100+
t.Cleanup(func() {
101+
err := os.Chdir(cwd)
102+
if err != nil {
103+
t.Fatalf("could not change back to orignal working dir %s: %v", cwd, err)
104+
}
105+
})
89106
}
90107

91108
a, err := txtar.ParseFile("./testdata/sql.txt")
@@ -112,6 +129,8 @@ func TestTxtar(t *testing.T) {
112129
}
113130
}
114131

132+
chTmpDir()
133+
115134
for i := 0; i < len(testPairs); i += 2 {
116135
testFile := testPairs[i]
117136
wantFile := testPairs[i+1]
@@ -144,8 +163,6 @@ func TestTxtar(t *testing.T) {
144163
t.Fatalf("could not parse args: %v", err)
145164
}
146165

147-
os.Chdir(tmpPath)
148-
149166
buf := bytes.Buffer{}
150167

151168
inputCsvs := GetInputCsvsOrPanic(fs.Args(), -1)

0 commit comments

Comments
 (0)