From ac28626bd192b70331f731777fe6b70c3f8e39a5 Mon Sep 17 00:00:00 2001 From: Matthew Rose Date: Sun, 14 Nov 2021 14:23:13 +1000 Subject: [PATCH] test: add injection tests Signed-off-by: Matthew Rose --- pkg/writer/testdata/action.yaml | 2 + pkg/writer/testdata/empty.md | 0 pkg/writer/testdata/end_before_begin.md | 4 ++ pkg/writer/testdata/existing_and_markers.md | 5 ++ pkg/writer/testdata/no_begin_marker.md | 3 ++ pkg/writer/testdata/no_end_marker.md | 3 ++ pkg/writer/testdata/only_markers.md | 2 + pkg/writer/writer_test.go | 53 +++++++++++++++++++++ 8 files changed, 72 insertions(+) create mode 100644 pkg/writer/testdata/action.yaml create mode 100644 pkg/writer/testdata/empty.md create mode 100644 pkg/writer/testdata/end_before_begin.md create mode 100644 pkg/writer/testdata/existing_and_markers.md create mode 100644 pkg/writer/testdata/no_begin_marker.md create mode 100644 pkg/writer/testdata/no_end_marker.md create mode 100644 pkg/writer/testdata/only_markers.md diff --git a/pkg/writer/testdata/action.yaml b/pkg/writer/testdata/action.yaml new file mode 100644 index 0000000..d3bdd65 --- /dev/null +++ b/pkg/writer/testdata/action.yaml @@ -0,0 +1,2 @@ +name: "test" +description: "test" diff --git a/pkg/writer/testdata/empty.md b/pkg/writer/testdata/empty.md new file mode 100644 index 0000000..e69de29 diff --git a/pkg/writer/testdata/end_before_begin.md b/pkg/writer/testdata/end_before_begin.md new file mode 100644 index 0000000..832b821 --- /dev/null +++ b/pkg/writer/testdata/end_before_begin.md @@ -0,0 +1,4 @@ + +something blah blah + +testdata diff --git a/pkg/writer/testdata/existing_and_markers.md b/pkg/writer/testdata/existing_and_markers.md new file mode 100644 index 0000000..4ea55da --- /dev/null +++ b/pkg/writer/testdata/existing_and_markers.md @@ -0,0 +1,5 @@ +something already existing + +something that will be overwritten + +something else not overwritten diff --git a/pkg/writer/testdata/no_begin_marker.md b/pkg/writer/testdata/no_begin_marker.md new file mode 100644 index 0000000..0b1eeff --- /dev/null +++ b/pkg/writer/testdata/no_begin_marker.md @@ -0,0 +1,3 @@ +something blah blah +testdata + diff --git a/pkg/writer/testdata/no_end_marker.md b/pkg/writer/testdata/no_end_marker.md new file mode 100644 index 0000000..208a0cf --- /dev/null +++ b/pkg/writer/testdata/no_end_marker.md @@ -0,0 +1,3 @@ +something blah blah + +testdata diff --git a/pkg/writer/testdata/only_markers.md b/pkg/writer/testdata/only_markers.md new file mode 100644 index 0000000..2cdebe9 --- /dev/null +++ b/pkg/writer/testdata/only_markers.md @@ -0,0 +1,2 @@ + + diff --git a/pkg/writer/writer_test.go b/pkg/writer/writer_test.go index 203e475..e3898f0 100644 --- a/pkg/writer/writer_test.go +++ b/pkg/writer/writer_test.go @@ -23,6 +23,7 @@ package writer_test import ( "bytes" + "fmt" "io" "os" "testing" @@ -88,3 +89,55 @@ func TestFileWriter(t *testing.T) { assert.Equal(t, tc, string(got)) } } + +func TestFileWriterInject(t *testing.T) { + t.Parallel() + + content := "dummy\n" + + testCases := []struct { + outputFile string + expectedContent string + }{ + {"./testdata/non_existent.md", "dummy\n"}, + {"./testdata/empty.md", "dummy\n"}, + {"./testdata/only_markers.md", fmt.Sprintf("%s\ndummy\n%s\n", writer.BeginInjection, writer.EndInjection)}, + {"./testdata/existing_and_markers.md", + fmt.Sprintf( + "something already existing\n%s\ndummy\n%s\nsomething else not overwritten\n", + writer.BeginInjection, + writer.EndInjection, + ), + }, + } + + for _, tc := range testCases { + writer.Write(writer.WriteInputs{Content: content, OutputFile: tc.outputFile, Inject: true}) + + got, err := os.ReadFile(tc.outputFile) + if err != nil { + t.Fatal(err) + } + + assert.Equal(t, tc.expectedContent, string(got)) + } +} + +func TestFileWriterInjectInvalid(t *testing.T) { + t.Parallel() + + testCases := []struct { + outputFile string + expectedErrMsg string + }{ + {"./testdata/no_begin_marker.md", "missing begin injection marker"}, + {"./testdata/no_end_marker.md", "missing end injection marker"}, + {"./testdata/end_before_begin.md", "end injection marker is before begin"}, + } + + for _, tc := range testCases { + err := writer.Write(writer.WriteInputs{Content: "dummy", OutputFile: tc.outputFile, Inject: true}) + assert.Error(t, err) + assert.Contains(t, err.Error(), tc.expectedErrMsg) + } +}