1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

v1: Allow empty files to be imported (#3421)

* add tests for an empty file

* return nil from lexer.load when the file is empty
This commit is contained in:
Billie Cleek 2020-05-16 10:14:03 -07:00 committed by GitHub
parent 3b4f7a3c81
commit 240de5a5dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View file

@ -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 {

View file

@ -23,22 +23,48 @@ 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)
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(expected) {
t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens))
if len(tokens) != len(tt.expected) {
t.Fatalf("Expected %d tokens, got %d", len(tt.expected), len(tokens))
}
for i, val := range expected {
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)
}
}
})
}
}
func TestParseOneAndImport(t *testing.T) {