Skip to content

Commit

Permalink
Preventing comments from getting into entries (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
kadern0 committed Jul 13, 2021
1 parent e29e649 commit fc23871
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
profileEnd = regexp.MustCompile(`(?i)# end\s*`)
spaceRemover = regexp.MustCompile(`\s+`)
tabReplacer = regexp.MustCompile(`\t+`)
endingComment = regexp.MustCompile(`(.[^#]*).*`)
)

// Parser is the interface for content parsers
Expand Down Expand Up @@ -142,8 +143,9 @@ func parseRouteLine(str string) (*types.Route, bool) {
clean := spaceRemover.ReplaceAllString(str, " ")
clean = tabReplacer.ReplaceAllString(clean, " ")
clean = strings.TrimSpace(clean)

p := strings.Split(clean, " ")
result := endingComment.FindStringSubmatch(clean)
tResult := strings.TrimSpace(result[1])
p := strings.Split(tResult, " ")

i := 0
if p[0] == "#" && len(p) > 1 {
Expand Down
6 changes: 4 additions & 2 deletions pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func TestParser(t *testing.T) {
}
appendLine(p, "127.0.0.1 first.loc")
appendLine(p, "127.0.0.1 second.loc")
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 2)
appendLine(p, "127.0.0.1 third.loc # test comment")
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 3)
})

t.Run("appendLine disabled", func(t *testing.T) {
Expand All @@ -102,7 +103,8 @@ func TestParser(t *testing.T) {
Routes: map[string]*types.Route{},
}
appendLine(p, "# 127.0.0.1 first.loc")
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 1)
appendLine(p, "# 127.0.0.1 second.loc # test comment")
assert.Len(t, p.Routes["127.0.0.1"].HostNames, 2)
})

t.Run("appendLine invalid lines", func(t *testing.T) {
Expand Down

0 comments on commit fc23871

Please sign in to comment.