Skip to content
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
20 changes: 20 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3417,6 +3417,26 @@ func TestWildcard(t *testing.T) {
call: "wildcard-foo-bar",
expectedOutput: "Hello foo-bar\n",
},
{
name: "regex metacharacters are matched literally",
call: "c++",
expectedOutput: "Building c++\n",
},
{
name: "regex metacharacters do not match as a pattern",
call: "cxx",
wantErr: true,
},
{
name: "a dot matches itself",
call: "deploy.prod",
expectedOutput: "Deploying prod\n",
},
{
name: "a dot is not a wildcard",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a "dot" somewhere in the call?

call: "deploy-prod",
wantErr: true,
},
}

for _, test := range tests {
Expand Down
5 changes: 4 additions & 1 deletion taskfile/ast/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (t *Task) WildcardMatch(name string) (bool, []string) {
names := append([]string{t.Task}, t.Aliases...)

for _, taskName := range names {
regexStr := fmt.Sprintf("^%s$", strings.ReplaceAll(taskName, "*", "(.*)"))
// Escape the task name so a name like "c++" or "a.b" is matched literally
// and does not panic in MustCompile, then turn the escaped "*" back into
// the wildcard group
regexStr := fmt.Sprintf("^%s$", strings.ReplaceAll(regexp.QuoteMeta(taskName), `\*`, "(.*)"))
regex := regexp.MustCompile(regexStr)
wildcards := regex.FindStringSubmatch(name)

Expand Down
9 changes: 9 additions & 0 deletions testdata/wildcards/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ tasks:
SERVICE: "{{index .MATCH 0}}"
cmds:
- echo "Starting {{.SERVICE}}"

# Regex metacharacters in a task name must be matched literally
c++:
cmds:
- echo "Building c++"

deploy.prod:
cmds:
- echo "Deploying prod"