Skip to content

Commit

Permalink
Add tests for WriteFileArtifactProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Aug 22, 2024
1 parent d133790 commit 51e64d3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
4 changes: 4 additions & 0 deletions writefileartfproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (p WriteFileArtifactProcessor) Process(ctx ArtifactProcessingContext) error
}
wg.Wait()

if errs.HasErrors() {
return errs
}

ctx.Logger.Success("Artifact files written successfully.")

return nil
Expand Down
94 changes: 94 additions & 0 deletions writefileartfproc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package specter

import (
"context"
"github.com/stretchr/testify/assert"
"testing"
)

func TestWriteFileArtifactProcessor_Process(t *testing.T) {
tests := []struct {
name string
mockFS *mockFileSystem
artifacts []Artifact
expectedFiles []string
expectError error
}{
{
name: "GIVEN file artifacts THEN successful file creation",
mockFS: &mockFileSystem{},
artifacts: []Artifact{
{
Name: "file1",
Value: FileArtifact{Path: "/path/to/file1", Mode: 0755},
},
{
Name: "file2",
Value: FileArtifact{Path: "/path/to/file2", Mode: 0755},
},
},
expectedFiles: []string{"/path/to/file1", "/path/to/file2"},
expectError: nil,
},
{
name: "GIVEN non-file artifacts THEN skip and return no error",
mockFS: &mockFileSystem{},
artifacts: []Artifact{
{
Name: "file1",
Value: FileArtifact{Path: "/path/to/file1", Mode: 0755},
},
{
Name: "not_a_file",
Value: "this is not a file",
},
},
expectedFiles: []string{"/path/to/file1"},
expectError: nil,
},
{
name: "GIVEN error creating file THEN return an error",
mockFS: &mockFileSystem{
writeFileErr: assert.AnError,
},
artifacts: []Artifact{
{
Name: "file1",
Value: FileArtifact{Path: "/path/to/file1", Mode: 0755},
},
},
expectedFiles: []string{},
expectError: assert.AnError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
processor := WriteFileArtifactProcessor{FileSystem: tt.mockFS}
ctx := ArtifactProcessingContext{
Context: context.Background(),
Artifacts: tt.artifacts,
Logger: NewDefaultLogger(DefaultLoggerConfig{}),
artifactRegistry: &InMemoryArtifactRegistry{},
processorName: processor.Name(),
}
err := processor.Process(ctx)

if tt.expectError != nil {
assert.Error(t, err)
assert.ErrorContains(t, err, tt.expectError.Error())
} else {
assert.NoError(t, err)
}

for _, file := range tt.expectedFiles {
_, fileExists := tt.mockFS.files[file]
assert.True(t, fileExists, "file %q should have been created", file)
}
})
}
}

func TestWriteFileArtifactProcessor_Name(t *testing.T) {
assert.NotEqual(t, "", WriteFileArtifactProcessor{}.Name())
}

0 comments on commit 51e64d3

Please sign in to comment.