Skip to content

Commit

Permalink
fix more bugs, add file mode/owner to file "names", cleanup, no-home …
Browse files Browse the repository at this point in the history
…moved to .tmp/<test>/home
  • Loading branch information
verdverm committed Jun 16, 2020
1 parent be96668 commit ea2e246
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 173 deletions.
11 changes: 10 additions & 1 deletion script/hls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import (
"github.com/hofstadter-io/hof/script/runtime"
)

func TestHLS(t *testing.T) {
func TestScriptBrowser(t *testing.T) {
runtime.Run(t, runtime.Params{
Dir: "tests/browser",
Glob: "*.hls",
})
}

func TestScriptCmds(t *testing.T) {
runtime.Run(t, runtime.Params{
Dir: "tests/cmds",
Glob: "*.hls",
})
}

func TestScriptHTTP(t *testing.T) {
runtime.Run(t, runtime.Params{
Dir: "tests/http",
Glob: "*.hls",
Expand Down
43 changes: 43 additions & 0 deletions script/runtime/cmd_checks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package runtime

import (
goruntime "runtime"
"strconv"
"strings"
"testing"

"github.com/hofstadter-io/hof/lib/gotils/imports"
"github.com/hofstadter-io/hof/lib/gotils/intern/os/execpath"
"github.com/hofstadter-io/hof/lib/gotils/par"
"github.com/hofstadter-io/hof/lib/gotils/testenv"
)

// status checks the exit or status code from the last exec or http call
Expand Down Expand Up @@ -32,3 +40,38 @@ func (ts *Script) CmdStatus(neg int, args []string) {

}

var execCache par.Cache

// condition reports whether the given condition is satisfied.
func (ts *Script) condition(cond string) (bool, error) {
switch cond {
case "short":
return testing.Short(), nil
case "net":
return testenv.HasExternalNetwork(), nil
case "link":
return testenv.HasLink(), nil
case "symlink":
return testenv.HasSymlink(), nil
case goruntime.GOOS, goruntime.GOARCH:
return true, nil
default:
if imports.KnownArch[cond] || imports.KnownOS[cond] {
return false, nil
}
if strings.HasPrefix(cond, "exec:") {
prog := cond[len("exec:"):]
ok := execCache.Do(prog, func() interface{} {
_, err := execpath.Look(prog, ts.Getenv)
return err == nil
}).(bool)
return ok, nil
}
if ts.params.Condition != nil {
return ts.params.Condition(cond)
}
ts.Fatalf("unknown condition %q", cond)
panic("unreachable")
}
}

58 changes: 58 additions & 0 deletions script/runtime/cmd_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@ package runtime

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)

// expand applies environment variable expansion to the string s.
func (ts *Script) expand(s string) string {
return os.Expand(s, func(key string) string {
if key1 := strings.TrimSuffix(key, "@R"); len(key1) != len(key) {
return regexp.QuoteMeta(ts.Getenv(key1))
}
return ts.Getenv(key)
})
}

// Setenv sets the value of the environment variable named by the key.
func (ts *Script) Setenv(key, value string) {
ts.env = append(ts.env, key+"="+value)
Expand Down Expand Up @@ -122,3 +135,48 @@ func (ts *Script) CmdEnvsub(neg int, args []string) {
}
}

// MkAbs interprets file relative to the test script's current directory
// and returns the corresponding absolute path.
func (ts *Script) MkAbs(file string) string {
if filepath.IsAbs(file) {
return file
}
return filepath.Join(ts.cd, file)
}

// ReadFile returns the contents of the file with the
// given name, intepreted relative to the test script's
// current directory. It interprets "stdout" and "stderr" to
// mean the standard output or standard error from
// the most recent exec or wait command respectively.
//
// If the file cannot be read, the script fails.
func (ts *Script) ReadFile(file string) string {
switch file {
case "stdout":
return ts.stdout
case "stderr":
return ts.stderr
default:
file = ts.MkAbs(file)
data, err := ioutil.ReadFile(file)
ts.Check(err)
return string(data)
}
}

func removeAll(dir string) error {
// module cache has 0444 directories;
// make them writable in order to remove content.
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // ignore errors walking in file system
}
if info.IsDir() {
os.Chmod(path, 0777)
}
return nil
})
return os.RemoveAll(dir)
}

2 changes: 0 additions & 2 deletions script/runtime/cmd_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func (ts *Script) http(args []string) (string, string, int, error) {
return "", "", 0, nil
}

fmt.Println("args", args)

req, err := ts.reqFromArgs(args)
ts.Check(err)

Expand Down
98 changes: 0 additions & 98 deletions script/runtime/script_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,12 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
goruntime "runtime"
"strings"
"testing"

"github.com/hofstadter-io/hof/lib/gotils/imports"
"github.com/hofstadter-io/hof/lib/gotils/intern/os/execpath"
"github.com/hofstadter-io/hof/lib/gotils/par"
"github.com/hofstadter-io/hof/lib/gotils/testenv"
"github.com/hofstadter-io/hof/lib/gotils/txtar"
)

var execCache par.Cache

// If -testwork is specified, the test prints the name of the temp directory
// and does not remove it when done, so that a programmer can
// poke at the test file tree afterward.
Expand Down Expand Up @@ -67,39 +57,6 @@ func (ts *Script) applyScriptUpdates() {
ts.Logf("%s updated", ts.file)
}

// condition reports whether the given condition is satisfied.
func (ts *Script) condition(cond string) (bool, error) {
switch cond {
case "short":
return testing.Short(), nil
case "net":
return testenv.HasExternalNetwork(), nil
case "link":
return testenv.HasLink(), nil
case "symlink":
return testenv.HasSymlink(), nil
case goruntime.GOOS, goruntime.GOARCH:
return true, nil
default:
if imports.KnownArch[cond] || imports.KnownOS[cond] {
return false, nil
}
if strings.HasPrefix(cond, "exec:") {
prog := cond[len("exec:"):]
ok := execCache.Do(prog, func() interface{} {
_, err := execpath.Look(prog, ts.Getenv)
return err == nil
}).(bool)
return ok, nil
}
if ts.params.Condition != nil {
return ts.params.Condition(cond)
}
ts.Fatalf("unknown condition %q", cond)
panic("unreachable")
}
}

// Helpers for command implementations.

// abbrev abbreviates the actual work directory in the string s to the literal string "$WORK".
Expand All @@ -116,61 +73,6 @@ func (ts *Script) abbrev(s string) string {
return s
}

// expand applies environment variable expansion to the string s.
func (ts *Script) expand(s string) string {
return os.Expand(s, func(key string) string {
if key1 := strings.TrimSuffix(key, "@R"); len(key1) != len(key) {
return regexp.QuoteMeta(ts.Getenv(key1))
}
return ts.Getenv(key)
})
}

// MkAbs interprets file relative to the test script's current directory
// and returns the corresponding absolute path.
func (ts *Script) MkAbs(file string) string {
if filepath.IsAbs(file) {
return file
}
return filepath.Join(ts.cd, file)
}

// ReadFile returns the contents of the file with the
// given name, intepreted relative to the test script's
// current directory. It interprets "stdout" and "stderr" to
// mean the standard output or standard error from
// the most recent exec or wait command respectively.
//
// If the file cannot be read, the script fails.
func (ts *Script) ReadFile(file string) string {
switch file {
case "stdout":
return ts.stdout
case "stderr":
return ts.stderr
default:
file = ts.MkAbs(file)
data, err := ioutil.ReadFile(file)
ts.Check(err)
return string(data)
}
}

func removeAll(dir string) error {
// module cache has 0444 directories;
// make them writable in order to remove content.
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil // ignore errors walking in file system
}
if info.IsDir() {
os.Chmod(path, 0777)
}
return nil
})
return os.RemoveAll(dir)
}

func homeEnvName() string {
switch goruntime.GOOS {
case "windows":
Expand Down
5 changes: 5 additions & 0 deletions script/runtime/script_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

// run runs the test script.
func (ts *Script) run() {
ts.setupZap()

defer func() {
ts.cleanup()
Expand Down Expand Up @@ -217,4 +218,8 @@ func (ts *Script) cleanup() {
// Flush testScript log to testing.T log.
ts.t.Log("\n" + ts.abbrev(ts.log.String()))
ts.deferred()

if ts.Logger != nil {
ts.Logger.Sync()
}
}
Loading

0 comments on commit ea2e246

Please sign in to comment.