Skip to content

Commit

Permalink
testscript: make sure work dir isn't removed when -testwork is used (#80
Browse files Browse the repository at this point in the history
)

When passing -testwork to go test, the work directory shouldn't
be removed.
  • Loading branch information
leitzler authored and myitcv committed Sep 7, 2019
1 parent 9181448 commit 18d060b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions testscript/testdata/nothing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally blank file, used to test that -testwork doesn't remove the work directory
2 changes: 1 addition & 1 deletion testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func RunT(t T, p Params) {
deferred: func() {},
}
defer func() {
if p.TestWork {
if p.TestWork || *testWork {
return
}
removeAll(ts.workdir)
Expand Down
28 changes: 28 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"regexp"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -121,6 +123,32 @@ func TestScripts(t *testing.T) {
// TODO check that the temp directory has been removed.
}

// TestTestwork tests that using the flag -testwork will make sure the work dir isn't removed
// after the test is done. It uses an empty testscript file that doesn't do anything.
func TestTestwork(t *testing.T) {
out, err := exec.Command("go", "test", ".", "-testwork", "-v", "-run", "TestScripts/^nothing$").CombinedOutput()
if err != nil {
t.Fatal(err)
}

re := regexp.MustCompile(`\s+WORK=(\S+)`)
match := re.FindAllStringSubmatch(string(out), -1)

// Ensure that there is only one line with one match
if len(match) != 1 || len(match[0]) != 2 {
t.Fatalf("failed to extract WORK directory")
}

var fi os.FileInfo
if fi, err = os.Stat(match[0][1]); err != nil {
t.Fatalf("failed to stat expected work directory %v: %v", match[0][1], err)
}

if !fi.IsDir() {
t.Fatalf("expected persisted workdir is not a directory: %v", match[0][1])
}
}

func setSpecialVal(ts *TestScript, neg bool, args []string) {
ts.Setenv("SPECIALVAL", "42")
}
Expand Down

0 comments on commit 18d060b

Please sign in to comment.