From 66eb0e9110966bf5cf501a758f70aab2f13c766d Mon Sep 17 00:00:00 2001 From: Dan Luhring Date: Sun, 1 Dec 2024 14:16:47 -0500 Subject: [PATCH] test(compile): basic tests for stripComments Signed-off-by: Dan Luhring --- pkg/build/compile_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/pkg/build/compile_test.go b/pkg/build/compile_test.go index 9c7c05972..26800f084 100644 --- a/pkg/build/compile_test.go +++ b/pkg/build/compile_test.go @@ -119,3 +119,32 @@ func TestCompileTest(t *testing.T) { t.Errorf("subpackage test packages: want %v, got %v", want, got) } } + +func Test_stripComments(t *testing.T) { + tests := []struct { + in, want string + }{ + {"", ""}, + {"# foo\n", ""}, + {"\n", ""}, + {"#!/bin/bash\n", "#!/bin/bash\n"}, + {"#!/bin/bash\n# foo\n", "#!/bin/bash\n"}, + {"#!/bin/bash\nfoo\n", "#!/bin/bash\nfoo\n"}, + {"#!/bin/bash\nfoo\n# bar\n", "#!/bin/bash\nfoo\n"}, + {"#!/bin/bash\nfoo\nbar\n", "#!/bin/bash\nfoo\nbar\n"}, + {"#!/bin/bash\nfoo\n# bar\nbaz\n", "#!/bin/bash\nfoo\nbaz\n"}, + } + + for _, test := range tests { + t.Run(test.in, func(t *testing.T) { + got, err := stripComments(test.in) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if got != test.want { + t.Errorf("stripComments(%q): want %q, got %q", test.in, test.want, got) + } + }) + } +}