Skip to content

Commit 18d060b

Browse files
leitzlermyitcv
authored andcommitted
testscript: make sure work dir isn't removed when -testwork is used (#80)
When passing -testwork to go test, the work directory shouldn't be removed.
1 parent 9181448 commit 18d060b

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

testscript/testdata/nothing.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally blank file, used to test that -testwork doesn't remove the work directory

testscript/testscript.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func RunT(t T, p Params) {
174174
deferred: func() {},
175175
}
176176
defer func() {
177-
if p.TestWork {
177+
if p.TestWork || *testWork {
178178
return
179179
}
180180
removeAll(ts.workdir)

testscript/testscript_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"fmt"
99
"io/ioutil"
1010
"os"
11+
"os/exec"
1112
"os/signal"
1213
"path/filepath"
1314
"reflect"
15+
"regexp"
1416
"strconv"
1517
"testing"
1618
"time"
@@ -121,6 +123,32 @@ func TestScripts(t *testing.T) {
121123
// TODO check that the temp directory has been removed.
122124
}
123125

126+
// TestTestwork tests that using the flag -testwork will make sure the work dir isn't removed
127+
// after the test is done. It uses an empty testscript file that doesn't do anything.
128+
func TestTestwork(t *testing.T) {
129+
out, err := exec.Command("go", "test", ".", "-testwork", "-v", "-run", "TestScripts/^nothing$").CombinedOutput()
130+
if err != nil {
131+
t.Fatal(err)
132+
}
133+
134+
re := regexp.MustCompile(`\s+WORK=(\S+)`)
135+
match := re.FindAllStringSubmatch(string(out), -1)
136+
137+
// Ensure that there is only one line with one match
138+
if len(match) != 1 || len(match[0]) != 2 {
139+
t.Fatalf("failed to extract WORK directory")
140+
}
141+
142+
var fi os.FileInfo
143+
if fi, err = os.Stat(match[0][1]); err != nil {
144+
t.Fatalf("failed to stat expected work directory %v: %v", match[0][1], err)
145+
}
146+
147+
if !fi.IsDir() {
148+
t.Fatalf("expected persisted workdir is not a directory: %v", match[0][1])
149+
}
150+
}
151+
124152
func setSpecialVal(ts *TestScript, neg bool, args []string) {
125153
ts.Setenv("SPECIALVAL", "42")
126154
}

0 commit comments

Comments
 (0)