Skip to content

Commit

Permalink
testscript: allow multiple args to chmod
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne authored and mvdan committed Aug 30, 2020
1 parent 76dc4b3 commit bb90167
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
28 changes: 13 additions & 15 deletions testscript/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,23 @@ func (ts *TestScript) cmdCd(neg bool, args []string) {
}

func (ts *TestScript) cmdChmod(neg bool, args []string) {
if len(args) != 2 {
ts.Fatalf("usage: chmod mode file")
if neg {
ts.Fatalf("unsupported: ! chmod")
}
mode, err := strconv.ParseInt(args[0], 8, 32)
if err != nil {
ts.Fatalf("bad file mode %q: %v", args[0], err)
if len(args) != 2 {
ts.Fatalf("usage: chmod perm paths...")
}
if mode > 0777 {
ts.Fatalf("unsupported file mode %.3o", mode)
perm, err := strconv.ParseUint(args[0], 8, 32)
if err != nil || perm&uint64(os.ModePerm) != perm {
ts.Fatalf("invalid mode: %s", args[0])
}
err = os.Chmod(ts.MkAbs(args[1]), os.FileMode(mode))
if neg {
if err == nil {
ts.Fatalf("unexpected chmod success")
for _, arg := range args[1:] {
path := arg
if !filepath.IsAbs(path) {
path = filepath.Join(ts.cd, arg)
}
return
}
if err != nil {
ts.Fatalf("unexpected chmod failure: %v", err)
err := os.Chmod(path, os.FileMode(perm))
ts.Check(err)
}
}

Expand Down
6 changes: 3 additions & 3 deletions testscript/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ The predefined commands are:
- cd dir
Change to the given directory for future commands.
- chmod mode file
Change the permissions of file or directory to the given octal mode (000 to 777).
- chmod perm path...
Change the permissions of the files or directories named by the path arguments
to the given octal mode (000 to 777).
- cmp file1 file2
Check that the named files have the same content.
Expand Down

0 comments on commit bb90167

Please sign in to comment.