Skip to content

Commit 7d77c36

Browse files
committed
Add Executor WithTaskfile() to support Package API use cases.
1 parent f204b96 commit 7d77c36

File tree

7 files changed

+83
-7
lines changed

7 files changed

+83
-7
lines changed

executor.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ type (
5353
Stdout io.Writer
5454
Stderr io.Writer
5555

56+
// Taskfile
57+
Taskfile *ast.Taskfile
58+
5659
// Internal
57-
Taskfile *ast.Taskfile
5860
Logger *logger.Logger
5961
Compiler *Compiler
6062
Output output.Output
@@ -86,6 +88,7 @@ func NewExecutor(opts ...ExecutorOption) *Executor {
8688
Stdin: os.Stdin,
8789
Stdout: os.Stdout,
8890
Stderr: os.Stderr,
91+
Taskfile: nil,
8992
Logger: nil,
9093
Compiler: nil,
9194
Output: nil,
@@ -502,3 +505,16 @@ type versionCheckOption struct {
502505
func (o *versionCheckOption) ApplyToExecutor(e *Executor) {
503506
e.EnableVersionCheck = o.enableVersionCheck
504507
}
508+
509+
// WithTaskfile set the [Executor]'s Taskfile to the provided [ast.Taskfile].
510+
func WithTaskfile(taskfile *ast.Taskfile) ExecutorOption {
511+
return &taskfileOption{taskfile}
512+
}
513+
514+
type taskfileOption struct {
515+
taskfile *ast.Taskfile
516+
}
517+
518+
func (o *taskfileOption) ApplyToExecutor(e *Executor) {
519+
e.Taskfile = o.taskfile
520+
}

executor_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type (
3737
wantSetupError bool
3838
wantRunError bool
3939
wantStatusError bool
40+
callbackFunc func(e *task.Executor)
4041
}
4142
)
4243

@@ -113,6 +114,21 @@ func (opt *statusErrorTestOption) applyToExecutorTest(t *ExecutorTest) {
113114
t.wantStatusError = true
114115
}
115116

117+
// WithCallback calls the provided function after the the test execution.
118+
func WithCallback(f func(e *task.Executor)) ExecutorTestOption {
119+
return &callbackTestOption{
120+
callback: f,
121+
}
122+
}
123+
124+
type callbackTestOption struct {
125+
callback func(e *task.Executor)
126+
}
127+
128+
func (opt *callbackTestOption) applyToExecutorTest(t *ExecutorTest) {
129+
t.callbackFunc = opt.callback
130+
}
131+
116132
// Helpers
117133

118134
// writeFixtureErrRun is a wrapper for writing the output of an error during the
@@ -161,6 +177,11 @@ func (tt *ExecutorTest) run(t *testing.T) {
161177

162178
// Set up the task executor
163179
e := task.NewExecutor(opts...)
180+
if tt.callbackFunc != nil {
181+
defer func() {
182+
tt.callbackFunc(e)
183+
}()
184+
}
164185

165186
// Create a golden fixture file for the output
166187
g := goldie.New(t,
@@ -996,3 +1017,31 @@ func TestIncludeChecksum(t *testing.T) {
9961017
WithFixtureTemplating(),
9971018
)
9981019
}
1020+
1021+
func TestWithTaskfile(t *testing.T) {
1022+
t.Parallel()
1023+
1024+
// Build an ast.Taskfile (using an executor).
1025+
var executor *task.Executor
1026+
NewExecutorTest(t,
1027+
WithName("WithTaskfile build"),
1028+
WithExecutorOptions(
1029+
task.WithDir("testdata/with_taskfile/build"),
1030+
task.WithDry(true),
1031+
),
1032+
WithCallback(func(e *task.Executor) {
1033+
executor = e
1034+
}),
1035+
)
1036+
require.NotNil(t, executor)
1037+
1038+
// Run executor using the pre-built ast.Taskfile.
1039+
NewExecutorTest(t,
1040+
WithName("WithTaskfile run"),
1041+
WithExecutorOptions(
1042+
task.WithDir("testdata/with_taskfile/run"),
1043+
task.WithTaskfile(executor.Taskfile),
1044+
),
1045+
)
1046+
1047+
}

setup.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ import (
2626

2727
func (e *Executor) Setup() error {
2828
e.setupLogger()
29-
node, err := e.getRootNode()
30-
if err != nil {
31-
return err
32-
}
3329
if err := e.setupTempDir(); err != nil {
3430
return err
3531
}
36-
if err := e.readTaskfile(node); err != nil {
37-
return err
32+
if e.Taskfile == nil {
33+
node, err := e.getRootNode()
34+
if err != nil {
35+
return err
36+
}
37+
if err := e.readTaskfile(node); err != nil {
38+
return err
39+
}
3840
}
3941
e.setupFuzzyModel()
4042
e.setupStdFiles()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "3"
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- echo "Fubar"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
task: [default] echo "Fubar"

testdata/with_taskfile/run/.gitkeep

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
task: [default] echo "Fubar"
2+
Fubar

0 commit comments

Comments
 (0)