Skip to content

Commit

Permalink
testscript: add unix2dos command
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 16, 2020
1 parent 1115b6a commit 85830ee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
79 changes: 60 additions & 19 deletions testscript/cmd.go
Expand Up @@ -5,6 +5,8 @@
package testscript

import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
Expand All @@ -24,25 +26,26 @@ import (
// NOTE: If you make changes here, update doc.go.
//
var scriptCmds = map[string]func(*TestScript, bool, []string){
"cd": (*TestScript).cmdCd,
"chmod": (*TestScript).cmdChmod,
"cmp": (*TestScript).cmdCmp,
"cmpenv": (*TestScript).cmdCmpenv,
"cp": (*TestScript).cmdCp,
"env": (*TestScript).cmdEnv,
"exec": (*TestScript).cmdExec,
"exists": (*TestScript).cmdExists,
"grep": (*TestScript).cmdGrep,
"mkdir": (*TestScript).cmdMkdir,
"rm": (*TestScript).cmdRm,
"unquote": (*TestScript).cmdUnquote,
"skip": (*TestScript).cmdSkip,
"stdin": (*TestScript).cmdStdin,
"stderr": (*TestScript).cmdStderr,
"stdout": (*TestScript).cmdStdout,
"stop": (*TestScript).cmdStop,
"symlink": (*TestScript).cmdSymlink,
"wait": (*TestScript).cmdWait,
"cd": (*TestScript).cmdCd,
"chmod": (*TestScript).cmdChmod,
"cmp": (*TestScript).cmdCmp,
"cmpenv": (*TestScript).cmdCmpenv,
"cp": (*TestScript).cmdCp,
"env": (*TestScript).cmdEnv,
"exec": (*TestScript).cmdExec,
"exists": (*TestScript).cmdExists,
"grep": (*TestScript).cmdGrep,
"mkdir": (*TestScript).cmdMkdir,
"rm": (*TestScript).cmdRm,
"unquote": (*TestScript).cmdUnquote,
"skip": (*TestScript).cmdSkip,
"stdin": (*TestScript).cmdStdin,
"stderr": (*TestScript).cmdStderr,
"stdout": (*TestScript).cmdStdout,
"stop": (*TestScript).cmdStop,
"symlink": (*TestScript).cmdSymlink,
"unix2dos": (*TestScript).cmdUNIX2DOS,
"wait": (*TestScript).cmdWait,
}

// cd changes to a different directory.
Expand Down Expand Up @@ -409,6 +412,26 @@ func (ts *TestScript) cmdSymlink(neg bool, args []string) {
ts.Check(os.Symlink(args[2], ts.MkAbs(args[0])))
}

// cmdUNIX2DOS converts files from UNIX line endings to DOS line endings.
func (ts *TestScript) cmdUNIX2DOS(neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! unix2dos")
}
if len(args) < 1 {
ts.Fatalf("usage: unix2dos paths...")
}
for _, arg := range args {
filename := ts.MkAbs(arg)
data, err := ioutil.ReadFile(filename)
ts.Check(err)
dosData, err := unix2DOS(data)
ts.Check(err)
if err := ioutil.WriteFile(filename, dosData, 0666); err != nil {
ts.Fatalf("%s: %v", filename, err)
}
}
}

// Tait waits for background commands to exit, setting stderr and stdout to their result.
func (ts *TestScript) cmdWait(neg bool, args []string) {
if neg {
Expand Down Expand Up @@ -520,3 +543,21 @@ func scriptMatch(ts *TestScript, neg bool, args []string, text, name string) {
}
}
}

// unix2DOS returns data with UNIX line endings converted to DOS line endings.
func unix2DOS(data []byte) ([]byte, error) {
sb := &strings.Builder{}
s := bufio.NewScanner(bytes.NewReader(data))
for s.Scan() {
if _, err := sb.Write(s.Bytes()); err != nil {
return nil, err
}
if _, err := sb.WriteString("\r\n"); err != nil {
return nil, err
}
}
if err := s.Err(); err != nil {
return nil, err
}
return []byte(sb.String()), nil
}
19 changes: 19 additions & 0 deletions testscript/testscript_test.go
Expand Up @@ -5,6 +5,7 @@
package testscript

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -307,6 +308,24 @@ func TestBadDir(t *testing.T) {
}
}

func TestUNIX2DOS(t *testing.T) {
for data, want := range map[string]string{
"": "", // Preserve empty files.
"\n": "\r\n", // Convert LF to CRLF in a file containing a single empty line.
"\r\n": "\r\n", // Preserve CRLF in a single line file.
"a": "a\r\n", // Append CRLF to a single line file with no line terminator.
"a\n": "a\r\n", // Convert LF to CRLF in a file containing a single non-empty line.
"a\r\n": "a\r\n", // Preserve CRLF in a file containing a single non-empty line.
"a\nb\n": "a\r\nb\r\n", // Convert LF to CRLF in multiline UNIX file.
"a\r\nb\n": "a\r\nb\r\n", // Convert LF to CRLF in a file containing a mix of UNIX and DOS lines.
"a\nb\r\n": "a\r\nb\r\n", // Convert LF to CRLF in a file containing a mix of UNIX and DOS lines.
} {
if got, err := unix2DOS([]byte(data)); err != nil || !bytes.Equal(got, []byte(want)) {
t.Errorf("unix2DOS(%q) == %q, %v, want %q, nil", data, got, err, want)
}
}
}

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

0 comments on commit 85830ee

Please sign in to comment.