diff options
author | Billie Cleek <[email protected]> | 2020-05-16 10:14:03 -0700 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-16 11:14:03 -0600 |
commit | 240de5a5dd20caba4560b9463d532dc629aaab4c (patch) | |
tree | 1ceb42f09ef248b3698d0f2094386699a09601d0 | |
parent | 3b4f7a3c817d57e9faf3e4e70768c3c556ebd771 (diff) | |
download | caddy-v1.tar.gz caddy-v1.zip |
v1: Allow empty files to be imported (#3421)v1
* add tests for an empty file
* return nil from lexer.load when the file is empty
-rw-r--r-- | caddyfile/lexer.go | 3 | ||||
-rw-r--r-- | caddyfile/parse_test.go | 54 |
2 files changed, 43 insertions, 14 deletions
diff --git a/caddyfile/lexer.go b/caddyfile/lexer.go index 2b38627be..f928772e1 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 10beb5b56..2b1409192 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 _, 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 expected { - if tokens[i].Text != val { - t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text) - } + 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) + } + } + }) } } |