mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 13:43:47 +03: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:
parent
3b4f7a3c81
commit
240de5a5dd
2 changed files with 42 additions and 13 deletions
|
@ -48,6 +48,9 @@ func (l *lexer) load(input io.Reader) error {
|
||||||
// discard byte order mark, if present
|
// discard byte order mark, if present
|
||||||
firstCh, _, err := l.reader.ReadRune()
|
firstCh, _, err := l.reader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if err == io.EOF {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if firstCh != 0xFEFF {
|
if firstCh != 0xFEFF {
|
||||||
|
|
|
@ -23,21 +23,47 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAllTokens(t *testing.T) {
|
func TestAllTokens(t *testing.T) {
|
||||||
input := strings.NewReader("a b c\nd e")
|
tests := []struct {
|
||||||
expected := []string{"a", "b", "c", "d", "e"}
|
name string
|
||||||
tokens, err := allTokens(input)
|
input string
|
||||||
|
expected []string
|
||||||
if err != nil {
|
}{
|
||||||
t.Fatalf("Expected no error, got %v", err)
|
{
|
||||||
}
|
name: "not-empty",
|
||||||
if len(tokens) != len(expected) {
|
input: "a b c\nd e",
|
||||||
t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens))
|
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 {
|
for _, tt := range tests {
|
||||||
if tokens[i].Text != val {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
t.Errorf("Token %d should be '%s' but was '%s'", i, val, tokens[i].Text)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue