Skip to content

Commit

Permalink
Merge pull request #8 from dtcaciuc/multiline
Browse files Browse the repository at this point in the history
Allow multiline commands without terminating backslashes.
  • Loading branch information
dtcaciuc authored Feb 24, 2022
2 parents 935877a + 6b01276 commit 0a4429c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
9 changes: 9 additions & 0 deletions examples/test.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ Simple commands:
baz

Multi-line command:

$ echo foo\
> bar\
> baz
foobarbaz

Multi-line command w/ here-document:

$ cat <<EOF
> Hello, \
> world
> EOF
Hello, world

TODO Persistent bash variables
# $ foo() {
# > echo bar
Expand Down
46 changes: 18 additions & 28 deletions internal/grill/grill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ package grill
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
)

var ErrNoCommand = errors.New("couldn't read any commands")

// Test is a single grill test. It is comprised of documentation, commands, and
// expected test results.
type Test struct {
doc [][]byte
command []byte
command [][]byte
expResults [][]byte
obsResults [][]byte
diff DiffData
Expand All @@ -26,7 +23,7 @@ func (t Test) Doc() string {
}

func (t Test) Command() io.Reader {
return bytes.NewReader(t.command)
return bytes.NewReader(bytes.Join(t.command, []byte("\n")))
}

func byteSlicesToString(slice [][]byte) string {
Expand Down Expand Up @@ -110,8 +107,16 @@ func (suite TestSuite) WriteErr() error {
return fmt.Errorf("couldn't write %s: %s", tErr, err)
}
}
if _, err := fmt.Fprintf(f, " $ %s\n", string(t.command)); err != nil {
return fmt.Errorf("couldn't write %s: %s", tErr, err)
for i, line := range t.command {
var format string
if i == 0 {
format = " $ %s\n"
} else {
format = " > %s\n"
}
if _, err := fmt.Fprintf(f, format, line); err != nil {
return fmt.Errorf("couldn't write %s: %s", tErr, err)
}
}
for _, e := range t.obsResults {
if _, err := fmt.Fprintf(f, " %s\n", e); err != nil {
Expand Down Expand Up @@ -252,31 +257,16 @@ func (t *testReader) Read(test *Test) error {
if len(line) < 5 {
return synErr(i, "line too short")
}
if bytes.HasSuffix(line, []byte("\\")) {
t.state = stateCmdCont
} else {
t.state = stateExp
}
test.command = append(test.command, bytes.Trim(line[4:], "\n")...)
// Assume next line is continuation; next state will
// unread and go straight to exp state if necessary.
t.state = stateCmdCont
test.command = append(test.command, line[4:])
case stateCmdCont:
if !bytes.HasPrefix(line, []byte(" > ")) {
return synErr(i, "truncated command")
}
if len(line) < 5 {
return synErr(i, "line too short")
}
trimmed := line[4:]
if bytes.HasSuffix(line, []byte("\\")) {
trimmed = trimmed[:len(trimmed)-1]
} else {
t.state = stateExp
continue
}
args := bytes.Split(trimmed, []byte(" "))
for _, a := range args {
if len(a) > 0 {
test.command = append(test.command, bytes.Trim(a, "\n")...)
}
}
test.command = append(test.command, line[4:])
case stateExp:
if bytes.HasPrefix(line, []byte(" $ ")) {
t.state = stateCmdStart
Expand Down
8 changes: 4 additions & 4 deletions internal/grill/grill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ func makeSpecs() []spec {
return []spec{
{
doc: "Run grill examples:\n",
command: []byte("grill -q examples examples/fail.t"),
command: [][]byte{[]byte("grill -q examples examples/fail.t")},
results: ".s.!.s.\n# Ran 7 tests, 2 skipped, 1 failed.\n[1]",
},
{
command: []byte("md5 examples/fail.t examples/fail.t.err"),
command: [][]byte{[]byte("md5 examples/fail.t examples/fail.t.err")},
results: ".*\\b0f598c2b7b8ca5bcb8880e492ff6b452\\b.* (re)\n.*\\b7a23dfa85773c77648f619ad0f9df554\\b.* (re)",
},
{
command: []byte("rm examples/fail.t.err"),
command: [][]byte{[]byte("rm examples/fail.t.err")},
},
}
}

type spec struct {
doc string
command []byte
command [][]byte
results string
}

Expand Down
2 changes: 1 addition & 1 deletion internal/grill/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestRunTest(t *testing.T) {
doc: [][]byte{
[]byte("This is a test"),
},
command: []byte("echo foobar"),
command: [][]byte{[]byte("echo foobar")},
expResults: [][]byte{
[]byte("foobar"),
},
Expand Down

0 comments on commit 0a4429c

Please sign in to comment.