diff --git a/caddyfile/lexer.go b/caddyfile/lexer.go index 2b38627b..f928772e 100644 --- a/caddyfile/lexer.go +++ b/caddyfile/lexer.go @@ -48,6 +48,9 @@ func (l *lexer) load(input io.Reader) error { // discard byte order mark, if present firstCh, _, err := l.reader.ReadRune() if err != nil { + if err == io.EOF { + return nil + } return err } if firstCh != 0xFEFF { diff --git a/caddyfile/parse_test.go b/caddyfile/parse_test.go index 10beb5b5..2b140919 100644 --- a/caddyfile/parse_test.go +++ b/caddyfile/parse_test.go @@ -23,21 +23,47 @@ import ( ) func TestAllTokens(t *testing.T) { - input := strings.NewReader("a b c\nd e") - expected := []string{"a", "b", "c", "d", "e"} - tokens, err := allTokens(input) - - if err != nil { - t.Fatalf("Expected no error, got %v", err) - } - if len(tokens) != len(expected) { - t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens)) + tests := []struct { + name string + input string + expected []string + }{ + { + name: "not-empty", + input: "a b c\nd e", + expected: []string{"a", "b", "c", "d", "e"}, + }, { + name: "empty", + input: "", + }, { + name: "newline", + input: "\n", + }, { + name: "space", + input: " ", + }, { + name: "tab and newline", + input: "\t\n", + }, } - for i, val := range expected { - if tokens[i].Text != val { - t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text) - } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tokens, err := allTokens(strings.NewReader(tt.input)) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if len(tokens) != len(tt.expected) { + t.Fatalf("Expected %d tokens, got %d", len(tt.expected), len(tokens)) + } + + for i, val := range tt.expected { + if tokens[i].Text != val { + t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text) + } + } + }) } }