Skip to content

Commit

Permalink
Fix JSON artifact registry having a nil map
Browse files Browse the repository at this point in the history
  • Loading branch information
jwillp committed Aug 26, 2024
1 parent 51e64d3 commit 3fc0870
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 18 deletions.
6 changes: 5 additions & 1 deletion artefactregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type JSONArtifactRegistryProcessor struct {
// NewJSONArtifactRegistry returns a new artifact file registry.
func NewJSONArtifactRegistry(fileName string, fs FileSystem) *JSONArtifactRegistry {
return &JSONArtifactRegistry{
ArtifactMap: nil,
ArtifactMap: map[string]*JSONArtifactRegistryProcessor{},
FilePath: fileName,
CurrentTimeProvider: func() time.Time {
return time.Now()
Expand Down Expand Up @@ -138,6 +138,10 @@ func (r *JSONArtifactRegistry) AddArtifact(processorName string, artifactName st
r.mu.Lock()
defer r.mu.Unlock()

if r.ArtifactMap == nil {
r.ArtifactMap = map[string]*JSONArtifactRegistryProcessor{}
}

if _, ok := r.ArtifactMap[processorName]; !ok {
r.ArtifactMap[processorName] = &JSONArtifactRegistryProcessor{}
}
Expand Down
39 changes: 33 additions & 6 deletions mkdirartfproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
// DirectoryArtifact is a data structure that can be used by a SpecificationProcessor to directory artifacts
// that can be written by the MakeDirectoryArtifactsProcessor.
type DirectoryArtifact struct {
Path string
Mode os.FileMode
Path string
FileMode os.FileMode
WriteMode WriteMode
}

type MakeDirectoryArtifactsProcessor struct {
Expand Down Expand Up @@ -42,17 +43,43 @@ func (p MakeDirectoryArtifactsProcessor) Process(ctx ArtifactProcessingContext)
if !ok {
continue
}
if dir.WriteMode == "" {
dir.WriteMode = Once

Check failure on line 47 in mkdirartfproc.go

View workflow job for this annotation

GitHub Actions / build

undefined: Once
}

wg.Add(1)
go func(ctx ArtifactProcessingContext, artifactName string, dir DirectoryArtifact) {
defer wg.Done()
ctx.Logger.Info(fmt.Sprintf("Creating directory %q ...", dir.Path))

err := p.FileSystem.Mkdir(dir.Path, dir.Mode)
dirPath, err := p.FileSystem.Abs(dir.Path)
if err != nil {
ctx.Logger.Error(fmt.Sprintf("failed creating directory at %q", dir.Path))
errs = errs.Append(err)
ctx.AddToRegistry(artifactName)
return
}

dirExists := true
if _, err := p.FileSystem.StatPath(dirPath); err != nil {
if !os.IsNotExist(err) {
ctx.Logger.Error(fmt.Sprintf("failed writing artifact file at %q", dirPath))
errs = errs.Append(err)
return
}
dirExists = false
}

if dir.WriteMode == Once && dirExists {

Check failure on line 70 in mkdirartfproc.go

View workflow job for this annotation

GitHub Actions / build

undefined: Once
ctx.Logger.Info(fmt.Sprintf("Directory %q already exists ... skipping", dirPath))
return
}

ctx.Logger.Info(fmt.Sprintf("Creating directory %q ...", dirPath))
if err = p.FileSystem.Mkdir(dirPath, dir.FileMode); err != nil {
ctx.Logger.Error(fmt.Sprintf("failed creating directory at %q", dirPath))
errs = errs.Append(err)
return
}

ctx.AddToRegistry(artifactName)
}(ctx, artifact.Name, dir)
}
wg.Wait()
Expand Down
24 changes: 20 additions & 4 deletions mkdirartfproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ func TestMakeDirectoryArtifactsProcessor_Process(t *testing.T) {
artifacts: []Artifact{
{
Name: "dir1",
Value: DirectoryArtifact{Path: "/path/to/dir1", Mode: 0755},
Value: DirectoryArtifact{Path: "/path/to/dir1", FileMode: 0755},
},
{
Name: "dir2",
Value: DirectoryArtifact{Path: "/path/to/dir2", Mode: 0755},
Value: DirectoryArtifact{Path: "/path/to/dir2", FileMode: 0755},
},
},
expectedDirs: []string{"/path/to/dir1", "/path/to/dir2"},
Expand All @@ -41,7 +41,7 @@ func TestMakeDirectoryArtifactsProcessor_Process(t *testing.T) {
artifacts: []Artifact{
{
Name: "dir1",
Value: DirectoryArtifact{Path: "/path/to/dir1", Mode: 0755},
Value: DirectoryArtifact{Path: "/path/to/dir1", FileMode: 0755},
},
{
Name: "not_a_dir",
Expand All @@ -60,12 +60,28 @@ func TestMakeDirectoryArtifactsProcessor_Process(t *testing.T) {
artifacts: []Artifact{
{
Name: "dir1",
Value: DirectoryArtifact{Path: "/path/to/dir1", Mode: 0755},
Value: DirectoryArtifact{Path: "/path/to/dir1", FileMode: 0755},
},
},
expectedDirs: []string{},
expectError: assert.AnError,
},
{
name: "GIVEN file already exists WHEN write mode is Once THEN do not write file",
mockFS: &mockFileSystem{
dirs: map[string]bool{
"/dir": true,
},
},
artifacts: []Artifact{
{
Name: "file1",
Value: DirectoryArtifact{Path: "/dir", WriteMode: Once},
},
},
expectedDirs: []string{},
expectError: nil,
},
}

for _, tt := range tests {
Expand Down
56 changes: 49 additions & 7 deletions writefileartfproc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ import (

const WriteFileArtifactsProcessorErrorCode = "write_file_artifacts_processor_error"

type WriteMode string

const (
RecreateWriteMode WriteMode = "recreate"
OnceWriteMode WriteMode = "once"
)

// FileArtifact is a data structure that can be used by a SpecificationProcessor to generate file artifacts
// that can be written by the WriteFileArtifactProcessor.
type FileArtifact struct {
Path string
Data []byte
Mode os.FileMode
Path string
Data []byte
Mode os.FileMode
WriteMode WriteMode
}

// WriteFileArtifactProcessor is a processor responsible for writing Artifact referring to files.
Expand Down Expand Up @@ -52,16 +60,50 @@ func (p WriteFileArtifactProcessor) Process(ctx ArtifactProcessingContext) error
if err := CheckContextDone(ctx); err != nil {
return err
}
if file.WriteMode == "" {
file.WriteMode = Once

Check failure on line 64 in writefileartfproc.go

View workflow job for this annotation

GitHub Actions / build

undefined: Once
}

wg.Add(1)
go func(ctx ArtifactProcessingContext, file FileArtifact) {
defer wg.Done()
ctx.Logger.Info(fmt.Sprintf("Writing file %q ...", file.Path))
err := p.FileSystem.WriteFile(file.Path, file.Data, os.ModePerm)

filePath, err := p.FileSystem.Abs(file.Path)
if err != nil {
ctx.Logger.Error(fmt.Sprintf("failed writing artifact file at %q", file.Path))
ctx.Logger.Error(fmt.Sprintf("failed writing artifact file at %q", filePath))
errs = errs.Append(err)
return
}

fileExists := true
if _, err := p.FileSystem.StatPath(filePath); err != nil {
if !os.IsNotExist(err) {
ctx.Logger.Error(fmt.Sprintf("failed writing artifact file at %q", filePath))
errs = errs.Append(err)
return
}
fileExists = false
}
ctx.AddToRegistry(file.Path)

if file.WriteMode == Once && fileExists {

Check failure on line 88 in writefileartfproc.go

View workflow job for this annotation

GitHub Actions / build

undefined: Once
ctx.Logger.Info(fmt.Sprintf("File %q already exists ... skipping", filePath))
return
}

// At this point if the file still already exists, this means that the clean step has not
// been executed properly.

ctx.Logger.Info(fmt.Sprintf("Writing file %q ...", filePath))
if err := p.FileSystem.WriteFile(filePath, file.Data, os.ModePerm); err != nil {
ctx.Logger.Error(fmt.Sprintf("failed writing artifact file at %q", filePath))
errs = errs.Append(err)
return
}

if file.WriteMode != Once {

Check failure on line 103 in writefileartfproc.go

View workflow job for this annotation

GitHub Actions / build

undefined: Once
ctx.AddToRegistry(file.Path)
}

}(ctx, file)
}
wg.Wait()
Expand Down
16 changes: 16 additions & 0 deletions writefileartfproc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ func TestWriteFileArtifactProcessor_Process(t *testing.T) {
expectedFiles: []string{},
expectError: assert.AnError,
},
{
name: "GIVEN file already exists WHEN write mode is Once THEN do not write file",
mockFS: &mockFileSystem{
files: map[string][]byte{
"/path/to/file1": []byte("file content"),
},
},
artifacts: []Artifact{
{
Name: "file1",
Value: FileArtifact{Path: "/path/to/file1", Mode: 0755, WriteMode: Once},
},
},
expectedFiles: []string{},
expectError: nil,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 3fc0870

Please sign in to comment.