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
18 changes: 17 additions & 1 deletion executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ type (
Stdout io.Writer
Stderr io.Writer

// Taskfile
Taskfile *ast.Taskfile

// Internal
Taskfile *ast.Taskfile
Logger *logger.Logger
Compiler *Compiler
Output output.Output
Expand Down Expand Up @@ -86,6 +88,7 @@ func NewExecutor(opts ...ExecutorOption) *Executor {
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Taskfile: nil,
Logger: nil,
Compiler: nil,
Output: nil,
Expand Down Expand Up @@ -502,3 +505,16 @@ type versionCheckOption struct {
func (o *versionCheckOption) ApplyToExecutor(e *Executor) {
e.EnableVersionCheck = o.enableVersionCheck
}

// WithTaskfile set the [Executor]'s Taskfile to the provided [ast.Taskfile].
func WithTaskfile(taskfile *ast.Taskfile) ExecutorOption {
return &taskfileOption{taskfile}
}

type taskfileOption struct {
taskfile *ast.Taskfile
}

func (o *taskfileOption) ApplyToExecutor(e *Executor) {
e.Taskfile = o.taskfile
}
48 changes: 48 additions & 0 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type (
wantSetupError bool
wantRunError bool
wantStatusError bool
callbackFunc func(e *task.Executor)
}
)

Expand Down Expand Up @@ -113,6 +114,21 @@ func (opt *statusErrorTestOption) applyToExecutorTest(t *ExecutorTest) {
t.wantStatusError = true
}

// WithCallback calls the provided function after the the test execution.
func WithCallback(f func(e *task.Executor)) ExecutorTestOption {
return &callbackTestOption{
callback: f,
}
}

type callbackTestOption struct {
callback func(e *task.Executor)
}

func (opt *callbackTestOption) applyToExecutorTest(t *ExecutorTest) {
t.callbackFunc = opt.callback
}

// Helpers

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

// Set up the task executor
e := task.NewExecutor(opts...)
if tt.callbackFunc != nil {
defer func() {
tt.callbackFunc(e)
}()
}

// Create a golden fixture file for the output
g := goldie.New(t,
Expand Down Expand Up @@ -996,3 +1017,30 @@ func TestIncludeChecksum(t *testing.T) {
WithFixtureTemplating(),
)
}

func TestWithTaskfile(t *testing.T) {
t.Parallel()

// Build an ast.Taskfile (using an executor).
var executor *task.Executor
NewExecutorTest(t,
WithName("WithTaskfile build"),
WithExecutorOptions(
task.WithDir("testdata/with_taskfile/build"),
task.WithDry(true),
),
WithCallback(func(e *task.Executor) {
executor = e
}),
)
require.NotNil(t, executor)

// Run executor using the pre-built ast.Taskfile.
NewExecutorTest(t,
WithName("WithTaskfile run"),
WithExecutorOptions(
task.WithDir("testdata/with_taskfile/run"),
task.WithTaskfile(executor.Taskfile),
),
)
}
14 changes: 8 additions & 6 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ import (

func (e *Executor) Setup() error {
e.setupLogger()
node, err := e.getRootNode()
if err != nil {
return err
}
if err := e.setupTempDir(); err != nil {
return err
}
if err := e.readTaskfile(node); err != nil {
return err
if e.Taskfile == nil {
node, err := e.getRootNode()
if err != nil {
return err
}
if err := e.readTaskfile(node); err != nil {
return err
}
}
e.setupFuzzyModel()
e.setupStdFiles()
Expand Down
49 changes: 49 additions & 0 deletions taskfile/node_byte.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package taskfile

import (
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
)

// A ByteNode is a node that reads a taskfile direct from a []byte object.
type ByteNode struct {
*baseNode
data []byte
}

func NewByteNode(data []byte, dir string) (*ByteNode, error) {
return &ByteNode{
baseNode: NewBaseNode(dir),
data: data,
}, nil
}

func (node *ByteNode) Location() string {
return "__bytes__"
}

func (node *ByteNode) Remote() bool {
return true
}

func (node *ByteNode) Read() ([]byte, error) {
return node.data, nil
}

func (node *ByteNode) ResolveEntrypoint(entrypoint string) (string, error) {
// A ByteNode has no presence on the local file system.
return entrypoint, nil
}

func (node *ByteNode) ResolveDir(dir string) (string, error) {
path, err := execext.ExpandLiteral(dir)
if err != nil {
return "", err
}

if filepathext.IsAbs(path) {
return path, nil
}

return filepathext.SmartJoin(node.Dir(), path), nil
}
25 changes: 25 additions & 0 deletions taskfile/node_byte_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package taskfile

import (
_ "embed"
"testing"

"github.com/stretchr/testify/assert"
)

//go:embed testdata/node_byte_taskfile.yaml
var taskfileYamlBytes []byte

func TestByteNode(t *testing.T) {
t.Parallel()
workingDir := t.TempDir()

node, err := NewByteNode(taskfileYamlBytes, workingDir)
assert.NoError(t, err)
assert.Equal(t, "__bytes__", node.Location())
assert.Equal(t, workingDir, node.Dir())
assert.True(t, node.Remote())
data, err := node.Read()
assert.NoError(t, err)
assert.Equal(t, taskfileYamlBytes, data)
}
10 changes: 10 additions & 0 deletions taskfile/testdata/node_byte_taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

vars:
GREETING: Hello, World!

tasks:
default:
cmds:
- echo "{{.GREETING}}"
silent: true
6 changes: 6 additions & 0 deletions testdata/with_taskfile/build/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3"

tasks:
default:
cmds:
- echo "Fubar"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
task: [default] echo "Fubar"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
task: [default] echo "Fubar"
Fubar
Loading