From 596fd29cb21f9e01a1b5f9edbff3e11deb43cd8b Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Sun, 5 Jan 2025 13:46:24 +0100 Subject: [PATCH] add test --- task_test.go | 46 ++++++++++++++++++- testdata/precondition/global/Taskfile.yml | 9 ++++ .../global/with_local/Taskfile.yml | 12 +++++ .../{ => global/with_local}/foo.txt | 0 .../precondition/{ => local}/Taskfile.yml | 0 testdata/precondition/local/foo.txt | 0 6 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 testdata/precondition/global/Taskfile.yml create mode 100644 testdata/precondition/global/with_local/Taskfile.yml rename testdata/precondition/{ => global/with_local}/foo.txt (100%) rename testdata/precondition/{ => local}/Taskfile.yml (100%) create mode 100644 testdata/precondition/local/foo.txt diff --git a/task_test.go b/task_test.go index ff407f18..9fe02aa0 100644 --- a/task_test.go +++ b/task_test.go @@ -456,10 +456,10 @@ func TestStatus(t *testing.T) { buff.Reset() } -func TestPrecondition(t *testing.T) { +func TestPreconditionLocal(t *testing.T) { t.Parallel() - const dir = "testdata/precondition" + const dir = "testdata/precondition/local" var buff bytes.Buffer e := &task.Executor{ @@ -499,6 +499,48 @@ func TestPrecondition(t *testing.T) { buff.Reset() } +func TestPreconditionGlobal(t *testing.T) { + t.Parallel() + + var buff bytes.Buffer + e := &task.Executor{ + Dir: "testdata/precondition/global", + Stdout: &buff, + Stderr: &buff, + } + + require.NoError(t, e.Setup()) + + // A global precondition that was not met + require.Error(t, e.Run(context.Background(), &ast.Call{Task: "impossible"})) + + if buff.String() != "task: 1 != 0 obviously!\n" { + t.Errorf("Wrong output message: %s", buff.String()) + } + buff.Reset() + + e = &task.Executor{ + Dir: "testdata/precondition/global/with_local", + Stdout: &buff, + Stderr: &buff, + } + + require.NoError(t, e.Setup()) + + // A global precondition that was met + require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "foo"})) + if buff.String() != "" { + t.Errorf("Got Output when none was expected: %s", buff.String()) + } + + // A local precondition that was not met + require.Error(t, e.Run(context.Background(), &ast.Call{Task: "impossible"})) + + if buff.String() != "task: 1 != 0 obviously!\n" { + t.Errorf("Wrong output message: %s", buff.String()) + } +} + func TestGenerates(t *testing.T) { t.Parallel() diff --git a/testdata/precondition/global/Taskfile.yml b/testdata/precondition/global/Taskfile.yml new file mode 100644 index 00000000..cc2e5e59 --- /dev/null +++ b/testdata/precondition/global/Taskfile.yml @@ -0,0 +1,9 @@ +version: '3' + +preconditions: + - sh: "[ 1 = 0 ]" + msg: "1 != 0 obviously!" + +tasks: + impossible: + cmd: echo "won't run" diff --git a/testdata/precondition/global/with_local/Taskfile.yml b/testdata/precondition/global/with_local/Taskfile.yml new file mode 100644 index 00000000..d70d809f --- /dev/null +++ b/testdata/precondition/global/with_local/Taskfile.yml @@ -0,0 +1,12 @@ +version: '3' + +preconditions: + - test -f foo.txt + +tasks: + foo: + + impossible: + preconditions: + - sh: "[ 1 = 0 ]" + msg: "1 != 0 obviously!" diff --git a/testdata/precondition/foo.txt b/testdata/precondition/global/with_local/foo.txt similarity index 100% rename from testdata/precondition/foo.txt rename to testdata/precondition/global/with_local/foo.txt diff --git a/testdata/precondition/Taskfile.yml b/testdata/precondition/local/Taskfile.yml similarity index 100% rename from testdata/precondition/Taskfile.yml rename to testdata/precondition/local/Taskfile.yml diff --git a/testdata/precondition/local/foo.txt b/testdata/precondition/local/foo.txt new file mode 100644 index 00000000..e69de29b