Skip to content

Commit

Permalink
test: add injection tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Rose <[email protected]>
  • Loading branch information
matty-rose committed Nov 14, 2021
1 parent bb0860f commit ac28626
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/writer/testdata/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name: "test"
description: "test"
Empty file added pkg/writer/testdata/empty.md
Empty file.
4 changes: 4 additions & 0 deletions pkg/writer/testdata/end_before_begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- END GHA DOCS -->
something blah blah
<!-- BEGIN GHA DOCS -->
testdata
5 changes: 5 additions & 0 deletions pkg/writer/testdata/existing_and_markers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
something already existing
<!-- BEGIN GHA DOCS -->
something that will be overwritten
<!-- END GHA DOCS -->
something else not overwritten
3 changes: 3 additions & 0 deletions pkg/writer/testdata/no_begin_marker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
something blah blah
testdata
<!-- END GHA DOCS -->
3 changes: 3 additions & 0 deletions pkg/writer/testdata/no_end_marker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
something blah blah
<!-- BEGIN GHA DOCS -->
testdata
2 changes: 2 additions & 0 deletions pkg/writer/testdata/only_markers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- BEGIN GHA DOCS -->
<!-- END GHA DOCS -->
53 changes: 53 additions & 0 deletions pkg/writer/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package writer_test

import (
"bytes"
"fmt"
"io"
"os"
"testing"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit ac28626

Please sign in to comment.