From 8c29ce9b519ea2e36f40b51369975068b8c58b16 Mon Sep 17 00:00:00 2001 From: Andrei Burdulescu Date: Sun, 9 Apr 2023 13:42:45 +0300 Subject: [PATCH] Update baxsfile parsing and tests --- internal/baxsfile/baxsfile.go | 8 +++++--- internal/baxsfile/baxsfile_test.go | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/internal/baxsfile/baxsfile.go b/internal/baxsfile/baxsfile.go index 1885bc2..f717b2a 100644 --- a/internal/baxsfile/baxsfile.go +++ b/internal/baxsfile/baxsfile.go @@ -11,6 +11,8 @@ type Entry struct { Command string } +const spaces = " \t" + func Parse(r io.Reader) ([]Entry, error) { data, err := io.ReadAll(r) if err != nil { @@ -18,7 +20,7 @@ func Parse(r io.Reader) ([]Entry, error) { } 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 } @@ -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 diff --git a/internal/baxsfile/baxsfile_test.go b/internal/baxsfile/baxsfile_test.go index d533dd7..9252a8a 100644 --- a/internal/baxsfile/baxsfile_test.go +++ b/internal/baxsfile/baxsfile_test.go @@ -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) { @@ -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