1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-06 17:40:58 +00:00
forgejo-runner/internal/app/cmd/validate_test.go
Earl Warren 20f115fdac
feat: add the runner validate subcommand (#757)
<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- features
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/757): <!--number 757 --><!--line 0 --><!--description ZmVhdDogdGhlIG5ldyBgZm9yZ2Vqby1ydW5uZXIgdmFsaWRhdGVgIGNvbW1hbmQgY2FuIGJlIHVzZWQgdG8gdmVyaWZ5IGlmIGFuIGFjdGlvbiBvciBhIHdvcmtmbG93IGlzIGNvbmZvcm1hbnQgd2l0aCB0aGUgZXhwZWN0ZWQgc2NoZW1hLiBgZm9yZ2Vqby1ydW5uZXIgdmFsaWRhdGUgLS1yZXBvc2l0b3J5IGh0dHBzOi8vZXhhbXBsZS5jb20vbXkvcmVwb3NpdG9yeWAgd2lsbCB2YWxpZGF0ZSBhbGwgdGhlIHdvcmtmbG93cyBhbmQgYWN0aW9ucyBhIEdpdCByZXBvc2l0b3J5IGNvbnRhaW5zLiBBbHRlcm5hdGl2ZWx5ICBgZm9yZ2Vqby1ydW5uZXIgdmFsaWRhdGUgLS1wYXRoIG15YWN0aW9uL2FjdGlvbi55bWwgLS1hY3Rpb25gIG9yIGBmb3JnZWpvLXJ1bm5lciB2YWxpZGF0ZSAtLXBhdGggLmZvcmdlam8vd29ya2Zsb3dzL3Rlc3QueW1sIC0td29ya2Zsb3dgIGNhbiBiZSB1c2VkIHRvIHZhbGlkYXRlIGEgc2luZ2xlIGZpbGUuIEl0IGlzIHJlY29tbWVuZGVkIHRvIHVzZSB0aGVzZSBjb21tYW5kcyB0byB2ZXJpZnkgZXhpc3RpbmcgYWN0aW9ucyBhbmQgd29ya2Zsb3dzIHBhc3MgYmVmb3JlIHVwZ3JhZGluZyB0byBbRm9yZ2VqbyBydW5uZXIgdjguMC4wXShodHRwczovL2NvZGUuZm9yZ2Vqby5vcmcvZm9yZ2Vqby9ydW5uZXIvc3JjL2JyYW5jaC9tYWluL1JFTEVBU0UtTk9URVMubWQjOC0wLTApIG9yIGFib3ZlIHRvIG5vdCBkaXNydXB0IGV4aXN0aW5nIHdvcmtmbG93cy4=-->feat: the new `forgejo-runner validate` command can be used to verify if an action or a workflow is conformant with the expected schema. `forgejo-runner validate --repository https://example.com/my/repository` will validate all the workflows and actions a Git repository contains. Alternatively  `forgejo-runner validate --path myaction/action.yml --action` or `forgejo-runner validate --path .forgejo/workflows/test.yml --workflow` can be used to validate a single file. It is recommended to use these commands to verify existing actions and workflows pass before upgrading to [Forgejo runner v8.0.0](https://code.forgejo.org/forgejo/runner/src/branch/main/RELEASE-NOTES.md#8-0-0) or above to not disrupt existing workflows.<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/757
Reviewed-by: Michael Kriese <michael.kriese@gmx.de>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
2025-07-31 05:37:12 +00:00

93 lines
2.7 KiB
Go

// Copyright 2025 The Forgejo Authors
// SPDX-License-Identifier: MIT
package cmd
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
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",
},
{
name: "MutuallyExclusive",
args: []string{"--action", "--workflow", "--path", "/tmp"},
message: "[action workflow] were all set",
},
{
name: "PathActionOK",
args: []string{"--action", "--path", "testdata/validate/good-action.yml"},
stdOut: "schema validation OK",
},
{
name: "PathActionNOK",
args: []string{"--action", "--path", "testdata/validate/bad-action.yml"},
stdOut: "Expected a mapping got scalar",
},
{
name: "PathWorkflowOK",
args: []string{"--workflow", "--path", "testdata/validate/good-workflow.yml"},
stdOut: "schema validation OK",
},
{
name: "PathWorkflowNOK",
args: []string{"--workflow", "--path", "testdata/validate/bad-workflow.yml"},
stdOut: "Unknown Property ruins-on",
},
{
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",
},
{
name: "RepositoryActionNOK",
args: []string{"--repository", "testdata/validate/bad-repository"},
stdOut: "action.yml action schema validation failed",
},
{
name: "RepositoryWorkflowNOK",
args: []string{"--repository", "testdata/validate/bad-repository"},
stdOut: ".forgejo/workflows/workflow1.yml workflow schema validation failed",
},
} {
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)
}
})
}
}