Skip to content

Commit

Permalink
Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 15, 2017
1 parent 6a3a853 commit d6d6929
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
38 changes: 17 additions & 21 deletions cmd/liquid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprint(stderr, err) // nolint: gas
exit(1)
os.Exit(1)
}
}

Expand All @@ -41,42 +41,38 @@ func run(args []string) error {
if _, err := io.Copy(buf, stdin); err != nil {
return err
}
render(buf.Bytes(), "")
return render(buf.Bytes(), "")
case args[0] == "-h" || args[0] == "--help":
usage(false)
usage()
case strings.HasPrefix(args[0], "-"):
usage(true)
usage()
exit(1)
case len(args) == 1:
s, err := ioutil.ReadFile(args[0])
if err != nil {
return err
}
render(s, args[0])
return render(s, args[0])
default:
usage(true)
usage()
exit(1)
}
return nil
}

func exitIfErr(err error) {
func render(b []byte, filename string) (err error) {
tpl, err := liquid.NewEngine().ParseTemplate(b)
if err != nil {
fmt.Fprint(stdout, err) // nolint: gas
exit(1)
return err
}
}

func render(b []byte, filename string) {
tpl, err := liquid.NewEngine().ParseTemplate(b)
exitIfErr(err)
out, err := tpl.Render(map[string]interface{}{})
exitIfErr(err)
stdout.Write(out) // nolint: gas, errcheck
if err != nil {
return err
}
_, err = stdout.Write(out)
return err
}

func usage(error bool) {
fmt.Printf("usage: %s [FILE]\n", os.Args[0])
if error {
exit(1)
}
func usage() {
fmt.Fprintf(stdout, "usage: %s [FILE]\n", os.Args[0]) // nolint: gas
}
12 changes: 12 additions & 0 deletions cmd/liquid/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func TestMain(t *testing.T) {
exit = func(n int) { t.Fatalf("exit called") }

src := `{{ "Hello World" | downcase | split: " " | first | append: "!"}}`
buf := new(bytes.Buffer)
stdin = bytes.NewBufferString(src)
Expand All @@ -20,4 +22,14 @@ func TestMain(t *testing.T) {
stdout = buf
require.NoError(t, run([]string{"testdata/source.txt"}))
require.Contains(t, buf.String(), "file system")

buf = new(bytes.Buffer)
stdout = buf
require.NoError(t, run([]string{"--help"}))
require.Contains(t, buf.String(), "usage:")

exitCode := 0
exit = func(n int) { exitCode = n }
require.NoError(t, run([]string{"--unknown-flag"}))
require.Equal(t, 1, exitCode)
}
8 changes: 7 additions & 1 deletion expressions/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var evaluatorTestBindings = (map[string]interface{}{
"hash_with_size_key": map[string]interface{}{"size": "key_value"},
})

func TestEvaluator(t *testing.T) {
func TestEvaluateString(t *testing.T) {
cfg := NewConfig()
cfg.AddFilter("length", strings.Count)
ctx := NewContext(evaluatorTestBindings, cfg)
Expand All @@ -128,6 +128,12 @@ func TestEvaluator(t *testing.T) {
require.Equalf(t, test.expected, val, test.in)
})
}

_, err := EvaluateString("syntax error", ctx)
require.Error(t, err)

_, err = EvaluateString("1 | undefined_filter", ctx)
require.Error(t, err)
}

func TestClosure(t *testing.T) {
Expand Down

0 comments on commit d6d6929

Please sign in to comment.