diff --git a/README.md b/README.md index 0280901..84b7d30 100644 --- a/README.md +++ b/README.md @@ -50,3 +50,34 @@ tasks: - echo "value of key3 is '$key3'" - echo "value of key4 is '$key4'" # assuming key4 is defined in .secrets/env ``` + +## Updates with example runfile with all the features + +```yaml +version: 0.0.1 + +tasks: + test: + env: + key1: value1 + key2: value2 + key3: + sh: echo -n "hello" + key4: + required: true + dotenv: + - .secrets/env # load dotenv file + cmd: + - echo "value of key1 is '$key1'" + - echo "value of key2 is '$key2'" + - echo "value of key3 is '$key3'" + - echo "value of key4 is '$key4'" # assuming key4 is defined in .secrets/env + build: + dir: cmd/app + cmd: + - go build -o app + run: + dir: cmd/app + cmd: + - go run . +``` diff --git a/Runfile b/Runfile index f12eed6..1585b7d 100644 --- a/Runfile +++ b/Runfile @@ -19,3 +19,7 @@ tasks: test: cmd: - go test -json ./pkg/runfile | gotestfmt + + test:only-failing: + cmd: + - go test -json ./pkg/runfile | gotestfmt --hide successful-tests diff --git a/pkg/runfile/run.go b/pkg/runfile/run.go index fb7994e..5c46382 100644 --- a/pkg/runfile/run.go +++ b/pkg/runfile/run.go @@ -151,102 +151,6 @@ func runTask(ctx Context, rf *Runfile, args runTaskArgs) *Error { return nil } -// func (rf *Runfile) runTask(ctx Context, args runTaskArgs) *Error { -// runfilePath := fn.Must(filepath.Rel(rf.attrs.RootRunfilePath, rf.attrs.RunfilePath)) -// -// trail := append(args.taskTrail, args.taskName) -// -// formatErr := func(err *Error) *Error { -// if runfilePath != "." { -// return err.WithTask(strings.Join(trail, "/")).WithRunfile(runfilePath) -// } -// return err.WithTask(strings.Join(trail, "/")) -// } -// -// logger := ctx.With("task", args.taskName, "runfile", runfilePath) -// logger.Debug("running task") -// task, ok := rf.Tasks[args.taskName] -// if !ok { -// return formatErr(TaskNotFound) -// } -// -// task.Name = args.taskName -// if task.Env == nil { -// task.Env = make(EnvVar) -// } -// -// for k, v := range args.envOverrides { -// task.Env[k] = v -// } -// -// pt, err := ParseTask(ctx, rf, task) -// if err != nil { -// return formatErr(err) -// } -// -// logger.Debug("debugging env", "pt.environ", pt.Env, "overrides", args.envOverrides) -// for _, command := range pt.Commands { -// logger.Debug("running command task", "command.run", command.Run, "parent.task", args.taskName) -// if command.Run != "" { -// if err := rf.runTask(ctx, runTaskArgs{ -// taskTrail: trail, -// taskName: command.Run, -// envOverrides: pt.Env, -// }); err != nil { -// return err -// // return NewError("", "").WithTask(fmt.Sprintf("%s/%s", err.TaskName, command.Run)).WithRunfile(rf.attrs.RunfilePath).WithErr(err.WithMetadata()) -// // e := formatErr(err).WithTask(fmt.Sprintf("%s/%s", err.TaskName, command.Run)) -// // return e -// } -// continue -// } -// -// stdoutR, stdoutW := io.Pipe() -// stderrR, stderrW := io.Pipe() -// -// go func() { -// r := bufio.NewReader(stdoutR) -// for { -// b, err := r.ReadBytes('\n') -// if err != nil { -// logger.Info("stdout", "msg", string(b), "err", err) -// // return -// break -// } -// fmt.Fprintf(os.Stdout, "%s %s", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", args.taskName)), b) -// } -// }() -// -// go func() { -// r := bufio.NewReader(stderrR) -// for { -// b, err := r.ReadBytes('\n') -// if err != nil { -// fmt.Printf("hello err: %+v\n", err) -// logger.Info("stderr", "err", err) -// // return -// break -// } -// fmt.Fprintf(os.Stderr, "%s %s", ctx.theme.TaskPrefixStyle.Render(fmt.Sprintf("[%s]", args.taskName)), b) -// } -// }() -// -// cmd := createCommand(ctx, cmdArgs{ -// shell: pt.Shell, -// env: ToEnviron(pt.Env), -// cmd: command.Command, -// workingDir: pt.WorkingDir, -// stdout: stdoutW, -// stderr: stderrW, -// }) -// if err := cmd.Run(); err != nil { -// return formatErr(CommandFailed).WithErr(err) -// } -// } -// -// return nil -// } - type RunArgs struct { Tasks []string ExecuteInParallel bool diff --git a/pkg/runfile/run_test.go b/pkg/runfile/run_test.go index 00929b5..1252a44 100644 --- a/pkg/runfile/run_test.go +++ b/pkg/runfile/run_test.go @@ -1,13 +1,15 @@ package runfile import ( + "context" + "log/slog" "reflect" "testing" + "time" ) func Test_runTask(t *testing.T) { type args struct { - ctx Context rf *Runfile args runTaskArgs } @@ -16,12 +18,40 @@ func Test_runTask(t *testing.T) { args args want *Error }{ - // TODO: Add test cases. + { + name: "1. Task Not Found", + args: args{ + rf: &Runfile{ + Tasks: map[string]Task{}, + }, + args: runTaskArgs{ + taskName: "sample", + }, + }, + want: TaskNotFound, + }, + + { + name: "1. Task Not Found", + args: args{ + rf: &Runfile{ + Tasks: map[string]Task{}, + }, + args: runTaskArgs{ + taskName: "sample", + }, + }, + want: TaskNotFound, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := runTask(tt.args.ctx, tt.args.rf, tt.args.args); !reflect.DeepEqual(got, tt.want) { - t.Errorf("runTask() = %v, want %v", got, tt.want) + ctx, cf := context.WithTimeout(context.TODO(), 2*time.Second) + defer cf() + + err := runTask(NewContext(ctx, slog.Default()), tt.args.rf, tt.args.args) + if !reflect.DeepEqual(err, tt.want) { + t.Errorf("runTask() = %v, want %v", err, tt.want) } }) } diff --git a/pkg/runfile/task-parser.go b/pkg/runfile/task-parser.go index 682e5b8..46700cb 100644 --- a/pkg/runfile/task-parser.go +++ b/pkg/runfile/task-parser.go @@ -19,7 +19,6 @@ type ParsedTask struct { Commands []CommandJson `json:"commands"` } -// func ParseTask(ctx Context, rf *Runfile, taskName string) (*ParsedTask, error) { func ParseTask(ctx Context, rf *Runfile, task Task) (*ParsedTask, *Error) { globalEnv := make(map[string]string)