Skip to content

Commit

Permalink
Merge pull request #4 from dtcaciuc/esc
Browse files Browse the repository at this point in the history
Support esc keyword & keywords appearing verbatim in command output.
  • Loading branch information
dtcaciuc authored Feb 18, 2022
2 parents be412d8 + 98a6ad8 commit 3b3b6c1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TODO:
* Support cram's environment variables.
* Gradually add support for cram's command-line flags.
* Flesh out the tests for the test.t reader.
* Persist new shell variables between commands.

WONTFIX:
* PCRE is not supported. Instead, Go's regexp language is.
Expand Down
2 changes: 1 addition & 1 deletion examples/env.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ Check environment variables:
bare.t
env.t
missingeol.t
test.t

# TODO re-enable when files are added back
# empty.t
# fail.t
# skip.t
# test.t

$ echo "$TESTFILE"
env.t
Expand Down
81 changes: 81 additions & 0 deletions examples/test.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Simple commands:

$ echo foo
foo
$ printf 'bar\nbaz\n' | cat
bar
baz

Multi-line command:
$ echo foo\
> bar\
> baz
foobarbaz

TODO Persistent bash variables
# $ foo() {
# > echo bar
# > }
# $ foo
# bar

Regular expression:

$ echo foobarbaz
foobar.* (re)

Glob:

$ printf '* \\foobarbaz {10}\n'
\* \\fo?bar* {10} (glob)

Literal match ending in (re) and (glob):

$ echo 'foo\Z\Z\Z bar (re)'
foo\Z\Z\Z bar (re)
$ echo 'baz??? quux (glob)'
baz??? quux (glob)

Exit code:

$ (exit 1)
[1]

Write to stderr:

$ echo foo >&2
foo

No newline:

$ printf foo
foo (no-eol)
$ printf 'foo\nbar'
foo
bar (no-eol)
$ printf ' '
(no-eol)
$ printf ' \n '

(no-eol)
$ echo foo
foo
$ printf foo
foo (no-eol)

Escaped output:

$ printf '\00\01\02\03\04\05\06\07\010\011\013\014\016\017\020\021\022\n'
\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\x0b\x0c\x0e\x0f\x10\x11\x12 (esc)
$ printf '\023\024\025\026\027\030\031\032\033\034\035\036\037\040\047\n'
\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f ' (esc)
$ echo hi
\x68\x69 (esc)
$ echo '(esc) in output (esc)'
(esc) in output (esc)
$ echo '(esc) in output (esc)'
(esc) in output \x28esc\x29 (esc)
Command that closes a pipe:
$ cat /dev/urandom | head -1 > /dev/null
24 changes: 21 additions & 3 deletions internal/grill/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"regexp"
"strconv"

"github.com/mb0/diff"
"github.com/mb0/glob"
Expand All @@ -23,13 +24,21 @@ func (f DiffData) Equal(i, j int) bool {
a := f.a[i]
b := f.b[j]

var v bool

if bytes.HasSuffix(a, []byte(" (re)")) {
return matchRegexp(a[:len(a)-5], b)
v = matchRegexp(a[:len(a)-5], b)
}
if bytes.HasSuffix(a, []byte(" (glob)")) {
return matchGlob(a[:len(a)-7], b)
v = matchGlob(a[:len(a)-7], b)
}
if bytes.HasSuffix(a, []byte(" (esc)")) {
v = matchEsc(a[:len(a)-6], b)
}
return bytes.Equal(a, b)

// All of the keywords may appear verbatim in command
// output, so check for direct equality every time.
return v || bytes.Equal(a, b)
}

func matchRegexp(a, b []byte) bool {
Expand All @@ -48,6 +57,15 @@ func matchGlob(a, b []byte) bool {
return match
}

func matchEsc(a, b []byte) bool {
// TODO there's probably a cleaner way to do it
s, err := strconv.Unquote(`"` + string(a) + `"`)
if err != nil {
return false
}
return s == string(b)
}

// NewDiff computes difference between two slices of text lines.
func NewDiff(a, b []byte) DiffData {
alines := bytes.Split(a, []byte("\n"))
Expand Down

0 comments on commit 3b3b6c1

Please sign in to comment.