mirror of
https://github.com/go-task/task.git
synced 2026-05-18 05:05:20 +02:00
feat: add conditional execution for tasks and commands (#2564)
This commit is contained in:
160
testdata/if/Taskfile.yml
vendored
Normal file
160
testdata/if/Taskfile.yml
vendored
Normal file
@@ -0,0 +1,160 @@
|
||||
version: '3'
|
||||
|
||||
vars:
|
||||
SHOULD_RUN: "yes"
|
||||
ENV: "prod"
|
||||
FEATURE_ENABLED: "true"
|
||||
FEATURE_DISABLED: "false"
|
||||
|
||||
tasks:
|
||||
# Basic command-level if (condition met)
|
||||
cmd-if-true:
|
||||
cmds:
|
||||
- cmd: echo "executed"
|
||||
if: "true"
|
||||
|
||||
# Basic command-level if (condition not met)
|
||||
cmd-if-false:
|
||||
cmds:
|
||||
- cmd: echo "should not appear"
|
||||
if: "false"
|
||||
- echo "this runs"
|
||||
|
||||
# Task-level if (condition met)
|
||||
task-if-true:
|
||||
if: "true"
|
||||
cmds:
|
||||
- echo "task executed"
|
||||
|
||||
# Task-level if (condition not met)
|
||||
task-if-false:
|
||||
if: "false"
|
||||
cmds:
|
||||
- echo "should not appear"
|
||||
|
||||
# With template variables
|
||||
if-with-template:
|
||||
cmds:
|
||||
- cmd: echo "Running because SHOULD_RUN={{.SHOULD_RUN}}"
|
||||
if: '[ "{{.SHOULD_RUN}}" = "yes" ]'
|
||||
|
||||
# If inside for loop
|
||||
if-in-for-loop:
|
||||
cmds:
|
||||
- for: ["a", "b", "c"]
|
||||
cmd: echo "processing {{.ITEM}}"
|
||||
if: '[ "{{.ITEM}}" != "b" ]'
|
||||
|
||||
# If on task call
|
||||
if-on-task-call:
|
||||
cmds:
|
||||
- task: subtask
|
||||
if: "true"
|
||||
|
||||
subtask:
|
||||
internal: true
|
||||
cmds:
|
||||
- echo "subtask ran"
|
||||
|
||||
# If combined with platforms (both must pass)
|
||||
if-with-platforms:
|
||||
cmds:
|
||||
- cmd: echo "condition and platform met"
|
||||
platforms: [linux, darwin, windows]
|
||||
if: "true"
|
||||
|
||||
# Skip task call
|
||||
skip-task-call:
|
||||
cmds:
|
||||
- task: subtask
|
||||
if: "false"
|
||||
- echo "after skipped task call"
|
||||
|
||||
# Task call in cmds with if condition met
|
||||
task-call-if-true:
|
||||
cmds:
|
||||
- task: subtask
|
||||
if: "true"
|
||||
- echo "after task call"
|
||||
|
||||
# Task call in cmds with if condition not met
|
||||
task-call-if-false:
|
||||
cmds:
|
||||
- task: subtask
|
||||
if: "false"
|
||||
- echo "continues after skipped task"
|
||||
|
||||
# Template eq - condition met
|
||||
template-eq-true:
|
||||
cmds:
|
||||
- cmd: echo "env is prod"
|
||||
if: '{{ eq .ENV "prod" }}'
|
||||
|
||||
# Template eq - condition not met
|
||||
template-eq-false:
|
||||
cmds:
|
||||
- cmd: echo "should not appear"
|
||||
if: '{{ eq .ENV "dev" }}'
|
||||
- echo "this runs"
|
||||
|
||||
# Template ne (not equal)
|
||||
template-ne:
|
||||
cmds:
|
||||
- cmd: echo "env is not dev"
|
||||
if: '{{ ne .ENV "dev" }}'
|
||||
|
||||
# Template with boolean-like variable
|
||||
template-bool-true:
|
||||
cmds:
|
||||
- cmd: echo "feature enabled"
|
||||
if: '{{ eq .FEATURE_ENABLED "true" }}'
|
||||
|
||||
# Template with boolean-like variable (false)
|
||||
template-bool-false:
|
||||
cmds:
|
||||
- cmd: echo "should not appear"
|
||||
if: '{{ eq .FEATURE_DISABLED "true" }}'
|
||||
- echo "feature was disabled"
|
||||
|
||||
# Direct true/false from template
|
||||
template-direct-true:
|
||||
cmds:
|
||||
- cmd: echo "direct true works"
|
||||
if: '{{ .FEATURE_ENABLED }}'
|
||||
|
||||
# Direct true/false from template (false case)
|
||||
template-direct-false:
|
||||
cmds:
|
||||
- cmd: echo "should not appear"
|
||||
if: '{{ .FEATURE_DISABLED }}'
|
||||
- echo "direct false skipped correctly"
|
||||
|
||||
# Template with CLI variable override
|
||||
template-cli-var:
|
||||
cmds:
|
||||
- cmd: echo "MY_VAR is yes"
|
||||
if: '{{ eq .MY_VAR "yes" }}'
|
||||
|
||||
# Combined template conditions with and
|
||||
template-and:
|
||||
cmds:
|
||||
- cmd: echo "both conditions met"
|
||||
if: '{{ and (eq .ENV "prod") (eq .FEATURE_ENABLED "true") }}'
|
||||
|
||||
# Combined template conditions with or
|
||||
template-or:
|
||||
cmds:
|
||||
- cmd: echo "at least one condition met"
|
||||
if: '{{ or (eq .ENV "dev") (eq .ENV "prod") }}'
|
||||
|
||||
# Task-level if with template
|
||||
task-level-template:
|
||||
if: '{{ eq .ENV "prod" }}'
|
||||
cmds:
|
||||
- echo "task runs in prod"
|
||||
|
||||
# Task-level if with template (not met)
|
||||
task-level-template-false:
|
||||
if: '{{ eq .ENV "dev" }}'
|
||||
cmds:
|
||||
- echo "should not appear"
|
||||
1
testdata/if/testdata/TestIf-cmd-if-false.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-cmd-if-false.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
this runs
|
||||
1
testdata/if/testdata/TestIf-cmd-if-true.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-cmd-if-true.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
executed
|
||||
7
testdata/if/testdata/TestIf-if-in-for-loop.golden
vendored
Normal file
7
testdata/if/testdata/TestIf-if-in-for-loop.golden
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
task: "if-in-for-loop" started
|
||||
task: [if-in-for-loop] echo "processing a"
|
||||
processing a
|
||||
task: [if-in-for-loop] if condition not met - skipped
|
||||
task: [if-in-for-loop] echo "processing c"
|
||||
processing c
|
||||
task: "if-in-for-loop" finished
|
||||
5
testdata/if/testdata/TestIf-task-call-if-false.golden
vendored
Normal file
5
testdata/if/testdata/TestIf-task-call-if-false.golden
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
task: "task-call-if-false" started
|
||||
task: [task-call-if-false] if condition not met - skipped
|
||||
task: [task-call-if-false] echo "continues after skipped task"
|
||||
continues after skipped task
|
||||
task: "task-call-if-false" finished
|
||||
2
testdata/if/testdata/TestIf-task-call-if-true.golden
vendored
Normal file
2
testdata/if/testdata/TestIf-task-call-if-true.golden
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
subtask ran
|
||||
after task call
|
||||
1
testdata/if/testdata/TestIf-task-if-false.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-task-if-false.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
task: if condition not met - skipped: "task-if-false"
|
||||
1
testdata/if/testdata/TestIf-task-if-true.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-task-if-true.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
task executed
|
||||
1
testdata/if/testdata/TestIf-task-level-template-false.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-task-level-template-false.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
task: if condition not met - skipped: "task-level-template-false"
|
||||
1
testdata/if/testdata/TestIf-task-level-template.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-task-level-template.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
task runs in prod
|
||||
1
testdata/if/testdata/TestIf-template-and.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-and.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
both conditions met
|
||||
1
testdata/if/testdata/TestIf-template-bool-false.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-bool-false.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
feature was disabled
|
||||
1
testdata/if/testdata/TestIf-template-bool-true.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-bool-true.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
feature enabled
|
||||
1
testdata/if/testdata/TestIf-template-cli-var.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-cli-var.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
MY_VAR is yes
|
||||
1
testdata/if/testdata/TestIf-template-direct-false.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-direct-false.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
direct false skipped correctly
|
||||
1
testdata/if/testdata/TestIf-template-direct-true.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-direct-true.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
direct true works
|
||||
5
testdata/if/testdata/TestIf-template-eq-false.golden
vendored
Normal file
5
testdata/if/testdata/TestIf-template-eq-false.golden
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
task: "template-eq-false" started
|
||||
task: [template-eq-false] if condition not met - skipped
|
||||
task: [template-eq-false] echo "this runs"
|
||||
this runs
|
||||
task: "template-eq-false" finished
|
||||
1
testdata/if/testdata/TestIf-template-eq-true.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-eq-true.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
env is prod
|
||||
1
testdata/if/testdata/TestIf-template-ne.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-ne.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
env is not dev
|
||||
1
testdata/if/testdata/TestIf-template-or.golden
vendored
Normal file
1
testdata/if/testdata/TestIf-template-or.golden
vendored
Normal file
@@ -0,0 +1 @@
|
||||
at least one condition met
|
||||
Reference in New Issue
Block a user