From 7a07075e09845e77311f772d49ae145acf0494c2 Mon Sep 17 00:00:00 2001 From: Ankitsinghsisodya Date: Thu, 2 Apr 2026 00:35:16 +0530 Subject: [PATCH 1/2] test: add unit coverage for ci writer helpers Add tests for the fileWriter (DefaultWorkflowWriter) and BufferWriter implementations in cmd/ci/writer.go. Covered: - Write creates all missing parent directories - Write overwrites existing file content (no append) - Exist returns false before any write - Exist returns true after a successful write - BufferWriter.Write stores path and resets buffer on each call - BufferWriter.Exist reflects buffer non-emptiness Fixes zero-coverage on writer.go:26 and writer.go:38. --- cmd/ci/writer_test.go | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 cmd/ci/writer_test.go diff --git a/cmd/ci/writer_test.go b/cmd/ci/writer_test.go new file mode 100644 index 0000000000..3482ca217a --- /dev/null +++ b/cmd/ci/writer_test.go @@ -0,0 +1,94 @@ +package ci_test + +import ( + "os" + "path/filepath" + "testing" + + "gotest.tools/v3/assert" + "knative.dev/func/cmd/ci" +) + +// TestFileWriter_Write_CreatesParentDirs verifies that Write creates any missing +// parent directories before writing the file. +func TestFileWriter_Write_CreatesParentDirs(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "a", "b", "c", "file.yaml") + content := []byte("hello: world") + + err := ci.DefaultWorkflowWriter.Write(path, content) + assert.NilError(t, err) + + got, err := os.ReadFile(path) + assert.NilError(t, err) + assert.DeepEqual(t, got, content) +} + +// TestFileWriter_Write_OverwritesExistingFile verifies that a second Write call +// replaces the previous content rather than appending to it. +func TestFileWriter_Write_OverwritesExistingFile(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "workflow.yaml") + + assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("old"))) + assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("new"))) + + got, err := os.ReadFile(path) + assert.NilError(t, err) + assert.DeepEqual(t, got, []byte("new")) +} + +// TestFileWriter_Exist_FalseBeforeWrite confirms Exist returns false for a path +// that does not yet exist. +func TestFileWriter_Exist_FalseBeforeWrite(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "nonexistent.yaml") + + assert.Assert(t, !ci.DefaultWorkflowWriter.Exist(path)) +} + +// TestFileWriter_Exist_TrueAfterWrite confirms Exist returns true once the file +// has been written. +func TestFileWriter_Exist_TrueAfterWrite(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "workflow.yaml") + + assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("data"))) + assert.Assert(t, ci.DefaultWorkflowWriter.Exist(path)) +} + +// TestBufferWriter_WriteAndExist exercises the in-memory BufferWriter test double: +// Exist is false on an empty buffer and true after Write. +func TestBufferWriter_WriteAndExist(t *testing.T) { + bw := ci.NewBufferWriter() + + assert.Assert(t, !bw.Exist("any/path"), "empty buffer should report Exist=false") + + content := []byte("name: deploy") + err := bw.Write("some/path", content) + assert.NilError(t, err) + + assert.Assert(t, bw.Exist("any/path"), "non-empty buffer should report Exist=true") + assert.DeepEqual(t, bw.Buffer.Bytes(), content) +} + +// TestBufferWriter_Write_StoresPath checks that Write records the path that was +// passed to it. +func TestBufferWriter_Write_StoresPath(t *testing.T) { + bw := ci.NewBufferWriter() + path := ".github/workflows/func-deploy.yaml" + + assert.NilError(t, bw.Write(path, []byte("data"))) + assert.Equal(t, bw.Path, path) +} + +// TestBufferWriter_Write_ResetsBuffer ensures that consecutive Write calls do +// not accumulate content — each call starts with a fresh buffer. +func TestBufferWriter_Write_ResetsBuffer(t *testing.T) { + bw := ci.NewBufferWriter() + + assert.NilError(t, bw.Write("p", []byte("first"))) + assert.NilError(t, bw.Write("p", []byte("second"))) + + assert.DeepEqual(t, bw.Buffer.Bytes(), []byte("second")) +} From d5e6ba7158a8b91e88ef1b92cee11605d327fff4 Mon Sep 17 00:00:00 2001 From: Ankitsinghsisodya Date: Thu, 2 Apr 2026 04:18:51 +0530 Subject: [PATCH 2/2] test: removing unnecessary test --- cmd/ci/writer_test.go | 50 ------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/cmd/ci/writer_test.go b/cmd/ci/writer_test.go index 3482ca217a..5a64f13004 100644 --- a/cmd/ci/writer_test.go +++ b/cmd/ci/writer_test.go @@ -1,62 +1,12 @@ package ci_test import ( - "os" - "path/filepath" "testing" "gotest.tools/v3/assert" "knative.dev/func/cmd/ci" ) -// TestFileWriter_Write_CreatesParentDirs verifies that Write creates any missing -// parent directories before writing the file. -func TestFileWriter_Write_CreatesParentDirs(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "a", "b", "c", "file.yaml") - content := []byte("hello: world") - - err := ci.DefaultWorkflowWriter.Write(path, content) - assert.NilError(t, err) - - got, err := os.ReadFile(path) - assert.NilError(t, err) - assert.DeepEqual(t, got, content) -} - -// TestFileWriter_Write_OverwritesExistingFile verifies that a second Write call -// replaces the previous content rather than appending to it. -func TestFileWriter_Write_OverwritesExistingFile(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "workflow.yaml") - - assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("old"))) - assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("new"))) - - got, err := os.ReadFile(path) - assert.NilError(t, err) - assert.DeepEqual(t, got, []byte("new")) -} - -// TestFileWriter_Exist_FalseBeforeWrite confirms Exist returns false for a path -// that does not yet exist. -func TestFileWriter_Exist_FalseBeforeWrite(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "nonexistent.yaml") - - assert.Assert(t, !ci.DefaultWorkflowWriter.Exist(path)) -} - -// TestFileWriter_Exist_TrueAfterWrite confirms Exist returns true once the file -// has been written. -func TestFileWriter_Exist_TrueAfterWrite(t *testing.T) { - dir := t.TempDir() - path := filepath.Join(dir, "workflow.yaml") - - assert.NilError(t, ci.DefaultWorkflowWriter.Write(path, []byte("data"))) - assert.Assert(t, ci.DefaultWorkflowWriter.Exist(path)) -} - // TestBufferWriter_WriteAndExist exercises the in-memory BufferWriter test double: // Exist is false on an empty buffer and true after Write. func TestBufferWriter_WriteAndExist(t *testing.T) {