2025-07-31 05:37:12 +00:00
|
|
|
// Copyright 2025 The Forgejo Authors
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2025-09-17 15:37:07 +02:00
|
|
|
func Test_validatePathMatch(t *testing.T) {
|
|
|
|
assert.False(t, validatePathMatch("nosuffix", "nosuffix"))
|
|
|
|
assert.True(t, validatePathMatch("something.yml", "something"))
|
|
|
|
assert.True(t, validatePathMatch("something.yaml", "something"))
|
|
|
|
assert.False(t, validatePathMatch("entire_something.yaml", "something"))
|
|
|
|
assert.True(t, validatePathMatch("nested/in/directory/something.yaml", "something"))
|
|
|
|
assert.False(t, validatePathMatch("nested/in/directory/entire_something.yaml", "something"))
|
|
|
|
}
|
|
|
|
|
2025-07-31 05:37:12 +00:00
|
|
|
func Test_validateCmd(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, testCase := range []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
message string
|
|
|
|
cmdOut string
|
|
|
|
stdOut string
|
|
|
|
stdErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "MissingFlag",
|
|
|
|
args: []string{"--path", "testdata/validate/good-action.yml"},
|
|
|
|
cmdOut: "Usage:",
|
|
|
|
message: "one of --workflow or --action must be set",
|
|
|
|
},
|
|
|
|
{
|
2025-09-17 16:40:31 +00:00
|
|
|
name: "MutuallyExclusiveActionWorkflow",
|
2025-07-31 05:37:12 +00:00
|
|
|
args: []string{"--action", "--workflow", "--path", "/tmp"},
|
|
|
|
message: "[action workflow] were all set",
|
|
|
|
},
|
2025-09-17 16:40:31 +00:00
|
|
|
{
|
|
|
|
name: "MutuallyExclusiveRepositoryDirectory",
|
|
|
|
args: []string{"--repository", "example.com", "--directory", "."},
|
|
|
|
message: "[directory repository] were all set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "MutuallyExclusiveClonedirDirectory",
|
|
|
|
args: []string{"--clonedir", ".", "--directory", "."},
|
|
|
|
message: "[clonedir directory] were all set",
|
|
|
|
},
|
2025-07-31 05:37:12 +00:00
|
|
|
{
|
|
|
|
name: "PathActionOK",
|
|
|
|
args: []string{"--action", "--path", "testdata/validate/good-action.yml"},
|
|
|
|
stdOut: "schema validation OK",
|
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "PathActionNOK",
|
|
|
|
args: []string{"--action", "--path", "testdata/validate/bad-action.yml"},
|
|
|
|
stdOut: "Expected a mapping got scalar",
|
|
|
|
message: "testdata/validate/bad-action.yml action schema validation failed",
|
2025-07-31 05:37:12 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "PathWorkflowOK",
|
|
|
|
args: []string{"--workflow", "--path", "testdata/validate/good-workflow.yml"},
|
|
|
|
stdOut: "schema validation OK",
|
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "PathWorkflowNOK",
|
|
|
|
args: []string{"--workflow", "--path", "testdata/validate/bad-workflow.yml"},
|
|
|
|
stdOut: "Unknown Property ruins-on",
|
|
|
|
message: "testdata/validate/bad-workflow.yml workflow schema validation failed",
|
2025-07-31 05:37:12 +00:00
|
|
|
},
|
2025-09-17 16:40:31 +00:00
|
|
|
{
|
|
|
|
name: "DirectoryOK",
|
|
|
|
args: []string{"--directory", "testdata/validate/good-directory"},
|
|
|
|
stdOut: "action.yml action schema validation OK\nsubaction/action.yaml action schema validation OK\n.forgejo/workflows/action.yml workflow schema validation OK\n.forgejo/workflows/workflow1.yml workflow schema validation OK\n.forgejo/workflows/workflow2.yaml workflow schema validation OK",
|
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "DirectoryActionNOK",
|
|
|
|
args: []string{"--directory", "testdata/validate/bad-directory"},
|
|
|
|
stdOut: "action.yml action schema validation failed",
|
|
|
|
message: "action.yml action schema validation failed",
|
2025-09-17 16:40:31 +00:00
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "DirectoryWorkflowNOK",
|
|
|
|
args: []string{"--directory", "testdata/validate/bad-directory"},
|
|
|
|
stdOut: ".forgejo/workflows/workflow1.yml workflow schema validation failed",
|
|
|
|
message: ".forgejo/workflows/workflow1.yml workflow schema validation failed",
|
2025-09-17 16:40:31 +00:00
|
|
|
},
|
2025-07-31 05:37:12 +00:00
|
|
|
{
|
|
|
|
name: "RepositoryOK",
|
|
|
|
args: []string{"--repository", "testdata/validate/good-repository"},
|
|
|
|
stdOut: "action.yml action schema validation OK\nsubaction/action.yaml action schema validation OK\n.forgejo/workflows/action.yml workflow schema validation OK\n.forgejo/workflows/workflow1.yml workflow schema validation OK\n.forgejo/workflows/workflow2.yaml workflow schema validation OK",
|
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "RepositoryActionNOK",
|
|
|
|
args: []string{"--repository", "testdata/validate/bad-repository"},
|
|
|
|
stdOut: "action.yml action schema validation failed",
|
|
|
|
message: "action.yml action schema validation failed",
|
2025-07-31 05:37:12 +00:00
|
|
|
},
|
|
|
|
{
|
2025-09-17 15:41:55 +02:00
|
|
|
name: "RepositoryWorkflowNOK",
|
|
|
|
args: []string{"--repository", "testdata/validate/bad-repository"},
|
|
|
|
stdOut: ".forgejo/workflows/workflow1.yml workflow schema validation failed",
|
|
|
|
message: ".forgejo/workflows/workflow1.yml workflow schema validation failed",
|
2025-07-31 05:37:12 +00:00
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
|
cmd := loadValidateCmd(ctx)
|
|
|
|
cmdOut, stdOut, stdErr, err := executeCommand(ctx, t, cmd, testCase.args...)
|
|
|
|
if testCase.message != "" {
|
|
|
|
assert.ErrorContains(t, err, testCase.message)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
if testCase.stdOut != "" {
|
|
|
|
assert.Contains(t, stdOut, testCase.stdOut)
|
|
|
|
} else {
|
|
|
|
assert.Empty(t, stdOut)
|
|
|
|
}
|
|
|
|
if testCase.stdErr != "" {
|
|
|
|
assert.Contains(t, stdErr, testCase.stdErr)
|
|
|
|
} else {
|
|
|
|
assert.Empty(t, stdErr)
|
|
|
|
}
|
|
|
|
if testCase.cmdOut != "" {
|
|
|
|
assert.Contains(t, cmdOut, testCase.cmdOut)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|