Skip to content

Commit

Permalink
split script/runtime into more files, add exec fallback to system pat…
Browse files Browse the repository at this point in the history
…h lookup
  • Loading branch information
verdverm committed Jun 15, 2020
1 parent 269adf4 commit 6f88d9b
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 436 deletions.
293 changes: 25 additions & 268 deletions script/runtime/cmds.go

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions script/runtime/cmds_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package runtime

// scriptCmds are the script command implementations.
// Keep list and the implementations below sorted by name.
//
// NOTE: If you make changes here, update doc.go.
//
var scriptCmds = map[string]func(*Script, int, []string){
"call": (*Script).CmdCall,
"cd": (*Script).CmdCd,
"chmod": (*Script).CmdChmod,
"cmp": (*Script).CmdCmp,
"cmpenv": (*Script).CmdCmpenv,
"cp": (*Script).CmdCp,
"env": (*Script).CmdEnv,
"exec": (*Script).CmdExec,
"exists": (*Script).CmdExists,
"grep": (*Script).CmdGrep,
"http": (*Script).CmdHttp,
"mkdir": (*Script).CmdMkdir,
"regexp": (*Script).CmdRegexp,
"rm": (*Script).CmdRm,
"unquote": (*Script).CmdUnquote,
"sed": (*Script).CmdSed,
"skip": (*Script).CmdSkip,
"stdin": (*Script).CmdStdin,
"stderr": (*Script).CmdStderr,
"stdout": (*Script).CmdStdout,
"status": (*Script).CmdStatus,
"stop": (*Script).CmdStop,
"symlink": (*Script).CmdSymlink,
"wait": (*Script).CmdWait,
}


2 changes: 1 addition & 1 deletion script/runtime/exe.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) {
"TESTSCRIPT_COMMAND="+name,
"TESTSCRIPT_COVERPROFILE="+cprof,
)
ts.cmdExec(neg, append([]string{path}, args...))
ts.CmdExec(neg, append([]string{path}, args...))
ts.env = ts.env[0:oldEnvLen]
if cprof == "" {
return
Expand Down
157 changes: 157 additions & 0 deletions script/runtime/script_match.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package runtime

import (
"io/ioutil"
"regexp"
"strconv"
"strings"
)

// scriptMatch implements both stdout and stderr.
func scriptMatch(ts *Script, neg int, args []string, text, name string) {
n := 0
if len(args) >= 1 && strings.HasPrefix(args[0], "-count=") {
if neg != 0 {
ts.Fatalf("cannot use -count= with negated match")
}
var err error
n, err = strconv.Atoi(args[0][len("-count="):])
if err != nil {
ts.Fatalf("bad -count=: %v", err)
}
if n < 1 {
ts.Fatalf("bad -count=: must be at least 1")
}
args = args[1:]
}

isRegexp := name == "regexp"
isGrep := name == "grep"
isSed := name == "sed"

extraUsage := ""
want := 1
if isRegexp || isGrep {
extraUsage = " file"
want = 2
}
if isSed {
extraUsage = " replace file"
want = 3
}
if len(args) != want {
ts.Fatalf("usage: %s [-count=N] 'pattern'%s", name, extraUsage)
}

pattern := args[0]
switch pattern {
case "stdout":
pattern = ts.stdout
case "stderr":
pattern = ts.stderr

default:
if pattern[0] == '@' {
fname := pattern[1:] // for error messages
data, err := ioutil.ReadFile(ts.MkAbs(fname))
ts.Check(err)
pattern = string(data)
}
}
re, err := regexp.Compile(`(?m)` + pattern)
ts.Check(err)


if isRegexp || isGrep {
content := args[1]
switch content {
case "stdout", "$WORK/stdout":
text = ts.stdout
case "stderr", "$WORK/stderr":
text = ts.stderr

default:
name = args[1] // for error messages
data, err := ioutil.ReadFile(ts.MkAbs(args[1]))
ts.Check(err)
text = string(data)
}
}
replace := ""
if isSed {
replace = args[1]
switch replace {
case "stdout", "$WORK/stdout":
text = ts.stdout
case "stderr", "$WORK/stderr":
text = ts.stderr

default:
if replace[0] == '@' {
fname := replace[1:] // for error messages
data, err := ioutil.ReadFile(ts.MkAbs(fname))
ts.Check(err)
replace = string(data)
}
}
content := args[2]
switch content {
case "stdout", "$WORK/stdout":
text = ts.stdout
case "stderr", "$WORK/stderr":
text = ts.stderr

default:
if content[0] == '@' {
fname := content[1:] // for error messages
data, err := ioutil.ReadFile(ts.MkAbs(fname))
ts.Check(err)
content = string(data)
}
}
}

if neg > 0 {
if re.MatchString(text) {
if isGrep {
ts.Logf("[%s]\n%s\n", name, text)
}
ts.Fatalf("unexpected match for %#q found in %s: %s", pattern, name, re.FindString(text))
}

if isGrep {
c := -1
if n > 0 {
c = n
}
matches := re.FindAllString(text, c)
if c > 0 && len(matches) > c {
matches = matches[:c]
}
ts.stdout = strings.Join(matches, "\n")
}
if isSed {
ts.stdout = re.ReplaceAllString(text, replace)
}
} else {
if isGrep || isSed {
ts.Fatalf("%s does not support status checking", name)
}
if !re.MatchString(text) {
if isGrep {
ts.Logf("[%s]\n%s\n", name, text)
}
ts.Fatalf("no match for %#q found in %s", pattern, name)
}
if n > 0 {
count := len(re.FindAllString(text, -1))
if count != n {
if isGrep {
ts.Logf("[%s]\n%s\n", name, text)
}
ts.Fatalf("have %d matches for %#q, want %d", count, pattern, n)
}
}
}
}

Loading

0 comments on commit 6f88d9b

Please sign in to comment.