From 1c7ca94d492c0a862af2ccca503c69491d3332d2 Mon Sep 17 00:00:00 2001 From: Josh Bebbington Date: Sun, 29 Jul 2018 00:38:52 +0100 Subject: [PATCH 1/4] Implemented dry run mode Added a --dry-run flag that compiles and steps through each task, but does not execute them. The commands that would have been run are printed. See #125. --- README.md | 5 +++++ cmd/task/task.go | 5 ++++- completion/zsh/_task | 1 + task.go | 5 +++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 98d3b857..cc55baa1 100644 --- a/README.md +++ b/README.md @@ -635,6 +635,11 @@ tasks: - echo "This will print nothing" > /dev/null ``` +## Dry Run Mode + +Dry run mode (`--dry-run`) compiles and steps through each task, printing the commands +that would be run without executing them. This is useful for debugging your Taskfiles. + ## Output syntax By default, Task just redirect the STDOUT and STDERR of the running commands diff --git a/cmd/task/task.go b/cmd/task/task.go index 6afde2d8..8f9c2a76 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -17,7 +17,7 @@ var ( version = "master" ) -const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [task...] +const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--dry-run] [task...] Runs the specified task(s). Falls back to the "default" task if no task name was specified, or lists all tasks if an unknown task name was specified. @@ -55,6 +55,7 @@ func main() { watch bool verbose bool silent bool + dryRun bool dir string ) @@ -66,6 +67,7 @@ func main() { pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing") + pflag.BoolVar(&dryRun, "dry-run", false, "compiles and prints tasks in the order that they would be run, without executing them") pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution") pflag.Parse() @@ -91,6 +93,7 @@ func main() { Verbose: verbose, Silent: silent, Dir: dir, + DryRun: dryRun, Context: getSignalContext(), diff --git a/completion/zsh/_task b/completion/zsh/_task index 362ee9ac..6c9a7b13 100644 --- a/completion/zsh/_task +++ b/completion/zsh/_task @@ -12,6 +12,7 @@ function __list() { _arguments \ '(-d --dir)'{-d,--dir}': :_files' \ + '(--dry-run)'--dry-run \ '(-f --force)'{-f,--force} \ '(-i --init)'{-i,--init} \ '(-l --list)'{-l,--list} \ diff --git a/task.go b/task.go index 48092e48..019faeea 100644 --- a/task.go +++ b/task.go @@ -35,6 +35,7 @@ type Executor struct { Watch bool Verbose bool Silent bool + DryRun bool Context context.Context @@ -211,6 +212,10 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi e.Logger.Errf(cmd.Cmd) } + if e.DryRun { + return nil + } + stdOut := e.Output.WrapWriter(e.Stdout, t.Prefix) stdErr := e.Output.WrapWriter(e.Stderr, t.Prefix) defer stdOut.Close() From 3f7e8c88eb08cfa73fbf1eb4d83d76f9bc428a74 Mon Sep 17 00:00:00 2001 From: Josh Bebbington Date: Sun, 29 Jul 2018 22:03:22 +0100 Subject: [PATCH 2/4] Changed --dry-run flag to --dry --- README.md | 2 +- cmd/task/task.go | 4 ++-- completion/zsh/_task | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cc55baa1..919c7e5d 100644 --- a/README.md +++ b/README.md @@ -637,7 +637,7 @@ tasks: ## Dry Run Mode -Dry run mode (`--dry-run`) compiles and steps through each task, printing the commands +Dry run mode (`--dry`) compiles and steps through each task, printing the commands that would be run without executing them. This is useful for debugging your Taskfiles. ## Output syntax diff --git a/cmd/task/task.go b/cmd/task/task.go index 8f9c2a76..18a4e2bd 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -17,7 +17,7 @@ var ( version = "master" ) -const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--dry-run] [task...] +const usage = `Usage: task [-ilfwvsd] [--init] [--list] [--force] [--watch] [--verbose] [--silent] [--dir] [--dry] [task...] Runs the specified task(s). Falls back to the "default" task if no task name was specified, or lists all tasks if an unknown task name was specified. @@ -67,7 +67,7 @@ func main() { pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing") - pflag.BoolVar(&dryRun, "dry-run", false, "compiles and prints tasks in the order that they would be run, without executing them") + pflag.BoolVar(&dryRun, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them") pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution") pflag.Parse() diff --git a/completion/zsh/_task b/completion/zsh/_task index 6c9a7b13..438465ce 100644 --- a/completion/zsh/_task +++ b/completion/zsh/_task @@ -12,7 +12,7 @@ function __list() { _arguments \ '(-d --dir)'{-d,--dir}': :_files' \ - '(--dry-run)'--dry-run \ + '(--dry)'--dry \ '(-f --force)'{-f,--force} \ '(-i --init)'{-i,--init} \ '(-l --list)'{-l,--list} \ From 00a0755ff3f4c96932c166f0821678e7e2d9a1be Mon Sep 17 00:00:00 2001 From: Josh Bebbington Date: Tue, 31 Jul 2018 23:09:55 +0100 Subject: [PATCH 3/4] Added test for executor.DryRun --- task_test.go | 22 ++++++++++++++++++++++ testdata/dryrun/Taskfile.yml | 3 +++ 2 files changed, 25 insertions(+) create mode 100644 testdata/dryrun/Taskfile.yml diff --git a/task_test.go b/task_test.go index 6f0214c9..cac097b0 100644 --- a/task_test.go +++ b/task_test.go @@ -432,3 +432,25 @@ func TestExpand(t *testing.T) { assert.NoError(t, e.Run(taskfile.Call{Task: "pwd"})) assert.Equal(t, home, strings.TrimSpace(buff.String())) } + +func TestDryRun(t *testing.T) { + const dir = "testdata/dryrun" + + file := filepath.Join(dir, "file.txt") + _ = os.Remove(file) + + var buff bytes.Buffer + + e := task.Executor{ + Dir: dir, + Stdout: &buff, + Stderr: &buff, + DryRun: true, + } + assert.NoError(t, e.Setup()) + assert.NoError(t, e.Run(taskfile.Call{Task: "build"})) + + if _, err := os.Stat(file); err == nil { + t.Errorf("File should not exist %s", file) + } +} diff --git a/testdata/dryrun/Taskfile.yml b/testdata/dryrun/Taskfile.yml new file mode 100644 index 00000000..62c6dde7 --- /dev/null +++ b/testdata/dryrun/Taskfile.yml @@ -0,0 +1,3 @@ +build: + cmds: + - touch file.txt \ No newline at end of file From a5f31a42806c9a3499cddac930d353e26f0b3acb Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 5 Aug 2018 11:28:02 -0300 Subject: [PATCH 4/4] Dry run small code style changes --- cmd/task/task.go | 6 +++--- task.go | 4 ++-- task_test.go | 7 ++++--- testdata/dry/Taskfile.yml | 6 ++++++ testdata/dryrun/Taskfile.yml | 3 --- 5 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 testdata/dry/Taskfile.yml delete mode 100644 testdata/dryrun/Taskfile.yml diff --git a/cmd/task/task.go b/cmd/task/task.go index 18a4e2bd..74c54697 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -55,7 +55,7 @@ func main() { watch bool verbose bool silent bool - dryRun bool + dry bool dir string ) @@ -67,7 +67,7 @@ func main() { pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") pflag.BoolVarP(&verbose, "verbose", "v", false, "enables verbose mode") pflag.BoolVarP(&silent, "silent", "s", false, "disables echoing") - pflag.BoolVar(&dryRun, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them") + pflag.BoolVar(&dry, "dry", false, "compiles and prints tasks in the order that they would be run, without executing them") pflag.StringVarP(&dir, "dir", "d", "", "sets directory of execution") pflag.Parse() @@ -93,7 +93,7 @@ func main() { Verbose: verbose, Silent: silent, Dir: dir, - DryRun: dryRun, + Dry: dry, Context: getSignalContext(), diff --git a/task.go b/task.go index 019faeea..6f5dd434 100644 --- a/task.go +++ b/task.go @@ -35,7 +35,7 @@ type Executor struct { Watch bool Verbose bool Silent bool - DryRun bool + Dry bool Context context.Context @@ -212,7 +212,7 @@ func (e *Executor) runCommand(ctx context.Context, t *taskfile.Task, call taskfi e.Logger.Errf(cmd.Cmd) } - if e.DryRun { + if e.Dry { return nil } diff --git a/task_test.go b/task_test.go index cac097b0..24d8c00e 100644 --- a/task_test.go +++ b/task_test.go @@ -433,8 +433,8 @@ func TestExpand(t *testing.T) { assert.Equal(t, home, strings.TrimSpace(buff.String())) } -func TestDryRun(t *testing.T) { - const dir = "testdata/dryrun" +func TestDry(t *testing.T) { + const dir = "testdata/dry" file := filepath.Join(dir, "file.txt") _ = os.Remove(file) @@ -445,11 +445,12 @@ func TestDryRun(t *testing.T) { Dir: dir, Stdout: &buff, Stderr: &buff, - DryRun: true, + Dry: true, } assert.NoError(t, e.Setup()) assert.NoError(t, e.Run(taskfile.Call{Task: "build"})) + assert.Equal(t, "touch file.txt", strings.TrimSpace(buff.String())) if _, err := os.Stat(file); err == nil { t.Errorf("File should not exist %s", file) } diff --git a/testdata/dry/Taskfile.yml b/testdata/dry/Taskfile.yml new file mode 100644 index 00000000..b4e932ed --- /dev/null +++ b/testdata/dry/Taskfile.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + build: + cmds: + - touch file.txt diff --git a/testdata/dryrun/Taskfile.yml b/testdata/dryrun/Taskfile.yml deleted file mode 100644 index 62c6dde7..00000000 --- a/testdata/dryrun/Taskfile.yml +++ /dev/null @@ -1,3 +0,0 @@ -build: - cmds: - - touch file.txt \ No newline at end of file