Skip to content

Commit

Permalink
[BUGFIX] Handle whitespace during option parsing
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
lthurston committed Nov 15, 2015
1 parent 96a43b1 commit 8279776
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
13 changes: 10 additions & 3 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Host struct {
options []Option
}

// GetOptionArgument gets a argument for an option keyword for a host
// GetOptionArgument gets an argument for an option keyword for a host
func (host Host) GetOptionArgument(keyword string) string {
for _, option := range host.options {
if option.keyword == keyword {
Expand All @@ -53,8 +53,15 @@ func (host Host) GetOptionArgument(keyword string) string {
func (host *Host) AddOptionFromString(line string) {
line = strings.TrimSpace(line)
sepIndex := strings.IndexAny(line, " \t")
keyword, argument := line[0:sepIndex], line[sepIndex+1:]
host.options = append(host.options, Option{keyword: keyword, argument: argument})

var keyword, argument string
if sepIndex != -1 {
keyword, argument = line[0:sepIndex], line[sepIndex+1:]
} else {
keyword, argument = line, ""
}

host.options = append(host.options, Option{keyword: strings.TrimSpace(keyword), argument: strings.TrimSpace(argument)})
}

// SetName sets name
Expand Down
26 changes: 26 additions & 0 deletions host/host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ func TestContainsStrings(t *testing.T) {
t.Errorf("Contains strings isn't returning false when it should")
}
}

func TestAddOptionFromString(t *testing.T) {
host := h.MakeHost()

host.AddOptionFromString("junktown ~/.sdklfj/hosrsefj")
host.AddOptionFromString("Mule")
host.AddOptionFromString(" sdlkfj ")
host.AddOptionFromString(" sddfsdflkfj sdfsdf sdfsdfsdf ")


if arg := host.GetOptionArgument("junktown"); arg != "~/.sdklfj/hosrsefj" {
t.Errorf("Unexpected option argument. Expected \"~/.sdklfj/hosrsefj\" got %s.", arg)
}

if arg := host.GetOptionArgument("Mule"); arg != "" {
t.Errorf("Unexpected option argument. Expected empty string got %s.", arg)
}

if arg := host.GetOptionArgument("sdlkfj"); arg != "" {
t.Errorf("Unexpected option argument. Expected empty string got \"%s\".", arg)
}

if arg := host.GetOptionArgument("sddfsdflkfj"); arg != "sdfsdf sdfsdfsdf" {
t.Errorf("Unexpected option argument. Expected \"sdfsdf sdfsdfsdf\" got \"%s\".", arg)
}
}
23 changes: 0 additions & 23 deletions host/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,29 +159,6 @@ Host ormulex-qa
if found && host.Name() != "ormulex-qa" {
t.Errorf("hosts.FindHostByName found the wrong host (#4)")
}

}

func TestParseInvalidHostOptions(t *testing.T) {
hosts := host.HostsCollection{}
hosts.ReadFromString(
`
# Host options should have a value
Host ormulex-dev
Hostname dev.ormulex.com
User mule
IdentityFile
Host ormulex-qa
Hostname cmw3wbq10
User
ProxyCommand ssh cmw3wbq10 'nc %h %banana %p'
# A comment with some empty lines around it
`)
}

func TestFindHostValue(t *testing.T) {
Expand Down

0 comments on commit 8279776

Please sign in to comment.