Skip to content

Commit

Permalink
Update baxsfile parsing and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aburdulescu committed Apr 9, 2023
1 parent aff8667 commit 8c29ce9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 5 additions & 3 deletions internal/baxsfile/baxsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ type Entry struct {
Command string
}

const spaces = " \t"

func Parse(r io.Reader) ([]Entry, error) {
data, err := io.ReadAll(r)
if err != nil {
return nil, err
}
var entries []Entry
for i, line := range strings.Split(string(data), "\n") {
line = strings.Trim(line, " \t")
line = strings.Trim(line, spaces)
if line == "" {
continue
}
Expand All @@ -30,8 +32,8 @@ func Parse(r io.Reader) ([]Entry, error) {
return nil, fmt.Errorf("failed to parse baxfile: line %d is missing ':'", i+1)
}
entries = append(entries, Entry{
Name: line[:dot],
Command: strings.Trim(line[dot+1:], " \t"),
Name: strings.Trim(line[:dot], spaces),
Command: strings.Trim(line[dot+1:], spaces),
})
}
return entries, nil
Expand Down
19 changes: 15 additions & 4 deletions internal/baxsfile/baxsfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ func TestParse(t *testing.T) {
r := strings.NewReader(`
# this is a comment
cmd1: foo bar baz
cmd2: fooz barz bazz
cmd1 : foo bar baz
cmd2 : fooz barz bazz
`)
procs, err := Parse(r)
if err != nil {
t.Fatal(err)
}
t.Log(procs)
expected := []Entry{
{"cmd1", "foo bar baz"},
{"cmd2", "fooz barz bazz"},
}
if len(procs) != len(expected) {
t.Fatalf("expected %d, have %d", len(expected), len(procs))
}
for i := range expected {
if procs[i] != expected[i] {
t.Fatalf("expected %v, have %v", expected[i], procs[i])
}
}
}

func FuzzParse(f *testing.F) {
Expand All @@ -27,7 +38,7 @@ func FuzzParse(f *testing.F) {
foo: bar baz
# valid entry with leading whitespace
\t foo: bar baz
\t foo : bar baz
\t\n
Expand Down

0 comments on commit 8c29ce9

Please sign in to comment.