Files
dagu/internal/cmd/validate_test.go

376 lines
9.7 KiB
Go

// Copyright (C) 2026 Yota Hamada
// SPDX-License-Identifier: GPL-3.0-or-later
package cmd_test
import (
"os"
"testing"
"github.com/dagucloud/dagu/v2/internal/cmd"
"github.com/dagucloud/dagu/v2/internal/test"
"github.com/stretchr/testify/require"
)
func TestValidateCommand(t *testing.T) {
th := test.SetupCommand(t)
t.Run("ValidSpec", func(t *testing.T) {
dag := th.DAG(t, `
steps:
- run: echo ok
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dag.Location},
})
})
t.Run("HarnessWorkingDirWarning", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "harness_warning.yaml", `steps:
- id: review
action: harness.run
with:
provider: claude
prompt: Review this repository.
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"working_dir", "review"},
})
})
t.Run("StateExpectedVersionExpression", func(t *testing.T) {
dagFile := th.CreateDAGFile(t, "state_expected_version.yaml", `
steps:
- id: load
action: state.get
output: STATE
with:
key: counters/jobs
required: true
- id: save
action: state.set
with:
key: counters/jobs
expected_version: "${steps.load.outputs.version}"
value:
count: 1
depends: load
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
})
t.Run("BaseConfigStepTypes", func(t *testing.T) {
require.NoError(t, os.WriteFile(th.Config.Paths.BaseConfig, []byte(`
step_types:
greet:
type: command
input_schema:
type: object
additionalProperties: false
required: [message]
properties:
message:
type: string
template:
exec:
command: /bin/echo
args:
- {$input: message}
`), 0600))
dagFile := th.CreateDAGFile(t, "base_config_step_type.yaml", `
steps:
- type: greet
with:
message: hello
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
})
t.Run("LegacySyntaxWarnsButSucceeds", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "legacy_syntax.yaml", `
steps:
- command: echo legacy
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"deprecated", "steps[0].command", "use run"},
})
})
t.Run("ValueReferenceNotices", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "value_resolution_notice.yaml", `
consts:
- image: ${consts.missing}
steps:
- run: echo ok
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"${consts.missing}", "was left unchanged", "consts.image"},
})
})
t.Run("RuntimeOnlyNoticesAreHiddenByDefault", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "runtime_only_notice.yaml", `
steps:
- id: archive
run: cp report.csv "${context.paths.artifacts_dir}/"
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.NotContains(t, th.LoggingOutput.String(), "context.paths.artifacts_dir")
})
t.Run("RuntimeOnlyNoticesAreShownOnRequest", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "runtime_only_notice_shown.yaml", `
steps:
- id: archive
run: cp report.csv "${context.paths.artifacts_dir}/"
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", "--show-unresolved", dagFile},
ExpectedOut: []string{"${context.paths.artifacts_dir}", "reason=namespace_unavailable"},
})
})
t.Run("UnknownContextFieldIsReported", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "unknown_context_field.yaml", `
steps:
- id: archive
run: cp report.csv "${context.paths.artifact_dir}/"
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"${context.paths.artifact_dir}", "reason=unknown_context_field"},
})
})
t.Run("ForeachReferenceOutsideItemScopeIsReported", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "foreach_reference_outside_item_scope.yaml", `
steps:
- run: echo ${foreach.item}
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"${foreach.item}", "reason=namespace_unavailable"},
})
})
t.Run("StepEnvIsInScopeForOtherStepFields", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "step_env_scope.yaml", `
steps:
- id: call
env:
- MY_ENDPOINT: https://example.internal/api
action: http.request
with:
method: GET
url: ${env.MY_ENDPOINT}
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", "--show-unresolved", dagFile},
})
require.NotContains(t, th.LoggingOutput.String(), "MY_ENDPOINT")
})
t.Run("StepOutputReferenceInfersDependency", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "step_output_inferred_dependency.yaml", `
steps:
- id: build
run: printf 'image=v1\n' >> "$DAGU_OUTPUT_FILE"
outputs:
- name: image
- id: deploy
run: echo ${steps.build.outputs.image}
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
ExpectedOut: []string{"inferred: build -> deploy (steps[1].run)"},
})
require.NotContains(t, th.LoggingOutput.String(), "was left unchanged")
})
t.Run("StepOutputReferenceCycle", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "step_output_reference_cycle.yaml", `
steps:
- id: a
run: echo ${steps.b.outputs.y}
outputs:
- name: x
- id: b
run: echo ${steps.a.outputs.x}
outputs:
- name: y
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "inferred dependency a -> b creates a cycle")
})
t.Run("V2SyntaxDoesNotWarn", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "v2_syntax.yaml", `
steps:
- run: echo ok
`)
th.RunCommand(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.NotContains(t, th.LoggingOutput.String(), "deprecated")
})
t.Run("MixedRunAndLegacyFails", func(t *testing.T) {
th.LoggingOutput.Reset()
dagFile := th.CreateDAGFile(t, "mixed_v2_legacy.yaml", `
steps:
- run: echo ok
command: echo legacy
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "run cannot be used together with command")
})
t.Run("InvalidDependency", func(t *testing.T) {
// This DAG has a step depending on a non-existent step
dagFile := th.CreateDAGFile(t, "invalid.yaml", `
type: graph
steps:
- run: echo A
- name: "b"
run: echo B
depends: ["missing_step"]
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "Validation failed")
})
t.Run("EmptyDocumentBetweenDocumentsFails", func(t *testing.T) {
// spec002 rejects an empty document wherever it appears, even when
// every other document is valid.
dagFile := th.CreateDAGFile(t, "empty_doc_between.yaml", `
steps:
- run: echo ok
---
---
name: child
steps:
- run: echo child
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "document 2")
require.Contains(t, err.Error(), "empty")
})
t.Run("TrailingEmptyDocumentFails", func(t *testing.T) {
// Validate is stricter than the loader: an empty document in the
// stream is a spec violation, even after a trailing `---`.
dagFile := th.CreateDAGFile(t, "trailing_empty_doc.yaml", `
steps:
- run: echo ok
---
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "document 2")
require.Contains(t, err.Error(), "empty")
})
t.Run("OnlyEmptyDocumentsFails", func(t *testing.T) {
dagFile := th.CreateDAGFile(t, "only_empty_docs.yaml", `---
---
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "empty")
})
t.Run("InvalidYAML", func(t *testing.T) {
// This DAG has invalid YAML syntax
dagFile := th.CreateDAGFile(t, "invalid_yaml.yaml", `
steps:
- name: "test"
run: echo test
invalid yaml here: [[[
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", dagFile},
})
require.Error(t, err)
require.Contains(t, err.Error(), "Validation failed")
})
t.Run("ResolvedPathValidationErrorUsesResolvedLocation", func(t *testing.T) {
dagFile := th.CreateDAGFile(t, "resolved_path.yaml", `
name: invalid-entrypoint-name
steps:
- run: echo ok
`)
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", "resolved_path.yaml"},
})
require.Error(t, err)
require.Contains(t, err.Error(), "Validation failed for "+dagFile)
require.NotContains(t, err.Error(), "Validation failed for resolved_path.yaml")
})
t.Run("MissingFile", func(t *testing.T) {
err := th.RunCommandWithError(t, cmd.Validate(), test.CmdTest{
Args: []string{"validate", "/nonexistent/file.yaml"},
})
require.Error(t, err)
require.Contains(t, err.Error(), "Validation failed")
})
}