Skip to content

Commit

Permalink
testscript: add RequireUniqueNames parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Jan 16, 2023
1 parent 9957a52 commit d0a44e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions testscript/testdata/testscript_duplicate_name.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Check that RequireUniqueNames works;
# it should reject txtar archives with duplicate names as defined by the host system.

unquote scripts-normalized/testscript.txt

testscript scripts-normalized
! testscript -unique-names scripts-normalized

-- scripts-normalized/testscript.txt --
>-- file --
>-- dir/../file --
15 changes: 15 additions & 0 deletions testscript/testscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"flag"
"fmt"
"go/build"
"io/fs"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -174,6 +175,10 @@ type Params struct {
// executions explicit.
RequireExplicitExec bool

// RequireUniqueNames requires that names in the txtar archive are unique.
// By default, later entries silently overwrite earlier ones.
RequireUniqueNames bool

// ContinueOnError causes a testscript to try to continue in
// the face of errors. Once an error has occurred, the script
// will continue as if in verbose mode.
Expand Down Expand Up @@ -426,6 +431,16 @@ func (ts *TestScript) setup() string {
ts.archive = a
for _, f := range a.Files {
name := ts.MkAbs(ts.expand(f.Name))
if ts.params.RequireUniqueNames {
switch _, err := os.Lstat(name); {
case err == nil:
ts.Fatalf("'%s' would overwrite '%s' (because RequireUniqueNames is enabled)", f.Name, name)
case errors.Is(err, fs.ErrNotExist):
// OK.
default:
ts.Fatalf("%s: %v", f.Name, err)
}
}
ts.scriptFiles[name] = f.Name
ts.Check(os.MkdirAll(filepath.Dir(name), 0o777))
ts.Check(ioutil.WriteFile(name, f.Data, 0o666))
Expand Down
2 changes: 2 additions & 0 deletions testscript/testscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ func TestScripts(t *testing.T) {
fset := flag.NewFlagSet("testscript", flag.ContinueOnError)
fUpdate := fset.Bool("update", false, "update scripts when cmp fails")
fExplicitExec := fset.Bool("explicit-exec", false, "require explicit use of exec for commands")
fUniqueNames := fset.Bool("unique-names", false, "require unique names in txtar archive")
fVerbose := fset.Bool("v", false, "be verbose with output")
fContinue := fset.Bool("continue", false, "continue on error")
if err := fset.Parse(args); err != nil {
Expand All @@ -210,6 +211,7 @@ func TestScripts(t *testing.T) {
Dir: ts.MkAbs(dir),
UpdateScripts: *fUpdate,
RequireExplicitExec: *fExplicitExec,
RequireUniqueNames: *fUniqueNames,
Cmds: map[string]func(ts *TestScript, neg bool, args []string){
"some-param-cmd": func(ts *TestScript, neg bool, args []string) {
},
Expand Down

0 comments on commit d0a44e3

Please sign in to comment.