Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support empty workflow yaml files #2271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pkg/model/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
log.Debugf("Reading workflow '%s'", f.Name())
workflow, err := ReadWorkflow(f)
if err != nil {
_ = f.Close()
// If the error is EOF, it means the file is empty.
// Log a warning and continue to the next file.
if err == io.EOF {
return nil, fmt.Errorf("unable to read workflow '%s': file is empty: %w", wf.workflowDirEntry.Name(), err)
log.Warnf("unable to read workflow '%s': file is empty: %v", wf.workflowDirEntry.Name(), err)
} else {
// If there's an error other than EOF, close the file and return an error.
_ = f.Close()
return nil, fmt.Errorf("workflow is not valid. '%s': %w", wf.workflowDirEntry.Name(), err)
}
return nil, fmt.Errorf("workflow is not valid. '%s': %w", wf.workflowDirEntry.Name(), err)
}
_, err = f.Seek(0, 0)
if err != nil {
Expand Down
55 changes: 48 additions & 7 deletions pkg/model/planner_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package model

import (
"bytes"
"io"
"path/filepath"
"strings"
"testing"

log "github.com/sirupsen/logrus"
Expand All @@ -11,27 +14,65 @@ import (
type WorkflowPlanTest struct {
workflowPath string
errorMessage string
warnMessage string
noWorkflowRecurse bool
}

var getLog GetLog

type GetLog struct {
buffer bytes.Buffer
oldLoggerOutput io.Writer
isLock bool
}

func (g *GetLog) Lock() {
if g.isLock {
log.Fatal("log is already locked")
return
}
g.isLock = true
g.oldLoggerOutput = log.StandardLogger().Out
log.SetOutput(&g.buffer)
}

func (g *GetLog) Unlock() string {
if !g.isLock {
log.Fatal("log is not locked")
return ""
}
g.isLock = false
log.SetOutput(g.oldLoggerOutput)
return g.buffer.String()
}

func TestPlanner(t *testing.T) {
log.SetLevel(log.DebugLevel)

tables := []WorkflowPlanTest{
{"invalid-job-name/invalid-1.yml", "workflow is not valid. 'invalid-job-name-1': Job name 'invalid-JOB-Name-v1.2.3-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", false},
{"invalid-job-name/invalid-2.yml", "workflow is not valid. 'invalid-job-name-2': Job name '1234invalid-JOB-Name-v123-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", false},
{"invalid-job-name/valid-1.yml", "", false},
{"invalid-job-name/valid-2.yml", "", false},
{"empty-workflow", "unable to read workflow 'push.yml': file is empty: EOF", false},
{"nested", "unable to read workflow 'fail.yml': file is empty: EOF", false},
{"nested", "", true},
{"invalid-job-name/invalid-1.yml", "workflow is not valid. 'invalid-job-name-1': Job name 'invalid-JOB-Name-v1.2.3-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", "-", false},
{"invalid-job-name/invalid-2.yml", "workflow is not valid. 'invalid-job-name-2': Job name '1234invalid-JOB-Name-v123-docker_hub' is invalid. Names must start with a letter or '_' and contain only alphanumeric characters, '-', or '_'", "-", false},
{"invalid-job-name/valid-1.yml", "", "-", false},
{"invalid-job-name/valid-2.yml", "", "-", false},
{"empty-workflow", "", "unable to read workflow 'push.yml': file is empty: EOF", false},
{"nested", "", "unable to read workflow 'push.yml': file is empty: EOF", false},
{"nested", "", "-", true},
}

workdir, err := filepath.Abs("testdata")
assert.NoError(t, err, workdir)
for _, table := range tables {
fullWorkflowPath := filepath.Join(workdir, table.workflowPath)
getLog.Lock()
_, err = NewWorkflowPlanner(fullWorkflowPath, table.noWorkflowRecurse)
warnMessage := getLog.Unlock()

// Check if the expected warning message is present in the log output
if table.warnMessage != "-" {
assert.True(t, strings.Contains(warnMessage, table.warnMessage))
}

// Check if an error is expected and if so, assert that the error matches the expected message
if table.errorMessage == "" {
assert.NoError(t, err, "WorkflowPlanner should exit without any error")
} else {
Expand Down