Correctly parse files without trailing newline
If the file did not have a newline character as the last character, parseKV() would panic with an NPE. Handle the parser changes better. Fixes #21.
This commit is contained in:
@@ -362,3 +362,19 @@ func TestDosLinesEndingsDecode(t *testing.T) {
|
||||
t.Errorf("expected HostName to be %q, got %q", "8.8.8.8", host)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoTrailingNewline(t *testing.T) {
|
||||
us := &UserSettings{
|
||||
userConfigFinder: testConfigFinder("testdata/config-no-ending-newline"),
|
||||
systemConfigFinder: nullConfigFinder,
|
||||
}
|
||||
|
||||
port, err := us.GetStrict("example", "Port")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if port != "4242" {
|
||||
t.Errorf("wrong port: got %q want 4242", port)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,6 +96,9 @@ func (p *sshParser) parseKV() sshParserStateFn {
|
||||
}
|
||||
comment := ""
|
||||
tok := p.peek()
|
||||
if tok == nil {
|
||||
tok = &token{typ: tokenEOF}
|
||||
}
|
||||
if tok.typ == tokenComment && tok.Position.Line == val.Position.Line {
|
||||
tok = p.getToken()
|
||||
comment = tok.val
|
||||
|
||||
3
testdata/config-no-ending-newline
vendored
Normal file
3
testdata/config-no-ending-newline
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
Host example
|
||||
HostName example.com
|
||||
Port 4242
|
||||
Reference in New Issue
Block a user