Skip to content

Commit acd2ef1

Browse files
committed
test(parser): fixes tests for all the parsers
task, runfile, command, env etc.
1 parent 3968a84 commit acd2ef1

File tree

6 files changed

+156
-69
lines changed

6 files changed

+156
-69
lines changed

parser/parse-command.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/nxtcoder17/runfile/types"
99
)
1010

11-
func parseCommand(prf *types.ParsedRunfile, command any) (*types.ParsedCommandJson, error) {
11+
func parseCommand(ctx types.Context, prf *types.ParsedRunfile, taskEnv map[string]string, command any) (*types.ParsedCommandJson, error) {
1212
ferr := func(err error) error {
1313
return errors.ErrTaskInvalidCommand.Wrap(err).KV("command", command)
1414
}
@@ -30,14 +30,25 @@ func parseCommand(prf *types.ParsedRunfile, command any) (*types.ParsedCommandJs
3030
return nil, ferr(err)
3131
}
3232

33+
parsedEnv, err := parseEnvVars(ctx, cj.Env, evaluationParams{
34+
Env: taskEnv,
35+
})
36+
if err != nil {
37+
return nil, ferr(err)
38+
}
39+
3340
pcj := types.ParsedCommandJson{
34-
Env: cj.Env,
41+
Env: parsedEnv,
3542
}
3643

3744
switch {
3845
case cj.Run != nil:
3946
{
4047
pcj.Run = cj.Run
48+
if _, ok := prf.Tasks[*cj.Run]; !ok {
49+
err := errors.ErrTaskNotFound.Wrap(fmt.Errorf("run target, not found")).KV("command", command, "run-target", cj.Run)
50+
return nil, err
51+
}
4152
}
4253
case cj.Command != nil:
4354
{
@@ -49,10 +60,6 @@ func parseCommand(prf *types.ParsedRunfile, command any) (*types.ParsedCommandJs
4960
}
5061
}
5162

52-
if _, ok := prf.Tasks[*cj.Run]; !ok {
53-
return nil, errors.ErrTaskNotFound.Wrap(fmt.Errorf("run target, not found")).KV("command", command, "run-target", cj.Run)
54-
}
55-
5663
return &pcj, nil
5764
}
5865
default:

parser/parse-command_test.go

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,39 @@
11
package parser
22

33
import (
4+
"context"
5+
"fmt"
46
"reflect"
57
"testing"
68

9+
"github.com/nxtcoder17/go.pkgs/log"
10+
fn "github.com/nxtcoder17/runfile/functions"
711
"github.com/nxtcoder17/runfile/types"
812
)
913

14+
func testParseCommandJsonEqual(t *testing.T, got, want *types.ParsedCommandJson) {
15+
if got == nil && want != nil || got != nil && want == nil {
16+
t.Errorf("parseCommand(),\n[.command] \n\tgot = %v\n\twant = %v", got, want)
17+
return
18+
}
19+
20+
// t.Log("first", first, "err", err, "secondErr", secondErr, "condition", secondErr != (err != nil))
21+
22+
if !reflect.DeepEqual(got.Command, want.Command) {
23+
t.Errorf("parseCommand(),\n[.command] \n\tgot = %v\n\twant = %v", fn.DefaultIfNil(got.Command, ""), fn.DefaultIfNil(want.Command, ""))
24+
return
25+
}
26+
27+
if fmt.Sprint(got.Env) != fmt.Sprint(want.Env) {
28+
t.Errorf("parseCommand(),\n[.env] \n\tgot = %+v\n\twant = %+v", got.Env, want.Env)
29+
return
30+
}
31+
}
32+
1033
func Test_parseCommand(t *testing.T) {
1134
type args struct {
1235
prf *types.ParsedRunfile
36+
taskEnv map[string]string
1337
command any
1438
}
1539
tests := []struct {
@@ -18,18 +42,84 @@ func Test_parseCommand(t *testing.T) {
1842
want *types.ParsedCommandJson
1943
wantErr bool
2044
}{
21-
// TODO: Add test cases.
45+
{
46+
name: "1. must pass with only command",
47+
args: args{
48+
prf: &types.ParsedRunfile{},
49+
taskEnv: map[string]string{},
50+
command: "echo hi hello",
51+
},
52+
want: &types.ParsedCommandJson{
53+
Command: fn.New("echo hi hello"),
54+
Run: nil,
55+
Env: map[string]string{},
56+
If: nil,
57+
},
58+
wantErr: false,
59+
},
60+
{
61+
name: "2. must fail with only run command with run target not found",
62+
args: args{
63+
prf: &types.ParsedRunfile{},
64+
taskEnv: map[string]string{},
65+
command: map[string]any{
66+
"run": "build",
67+
},
68+
},
69+
want: nil,
70+
wantErr: true,
71+
},
72+
{
73+
name: "3. must pass with run command, and target exists in runfile tasks",
74+
args: args{
75+
prf: &types.ParsedRunfile{
76+
Tasks: map[string]types.Task{
77+
"build": {
78+
Commands: []any{
79+
"echo from build",
80+
},
81+
},
82+
},
83+
},
84+
taskEnv: map[string]string{},
85+
command: map[string]any{
86+
"run": "build",
87+
"env": map[string]string{
88+
"k1": "v1",
89+
},
90+
},
91+
},
92+
want: &types.ParsedCommandJson{
93+
Command: nil,
94+
Run: fn.New("build"),
95+
Env: map[string]string{
96+
"k1": "v1",
97+
},
98+
If: nil,
99+
},
100+
wantErr: false,
101+
},
22102
}
23-
for _, tt := range tests {
103+
104+
for i := range tests {
105+
tt := tests[i]
24106
t.Run(tt.name, func(t *testing.T) {
25-
got, err := parseCommand(tt.args.prf, tt.args.command)
26-
if (err != nil) != tt.wantErr {
107+
ctx := types.Context{
108+
Context: context.TODO(),
109+
Logger: log.New(),
110+
}
111+
112+
got, err := parseCommand(ctx, tt.args.prf, tt.args.taskEnv, tt.args.command)
113+
if tt.wantErr != (err != nil) {
27114
t.Errorf("parseCommand() error = %v, wantErr %v", err, tt.wantErr)
28115
return
29116
}
30-
if !reflect.DeepEqual(got, tt.want) {
31-
t.Errorf("parseCommand() = %v, want %v", got, tt.want)
117+
118+
if tt.wantErr {
119+
return
32120
}
121+
122+
testParseCommandJsonEqual(t, got, tt.want)
33123
})
34124
}
35125
}

parser/parse-env.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,20 @@ func parseEnvVars(ctx types.Context, ev types.EnvVar, params evaluationParams) (
102102
case specials.Sh != nil:
103103
{
104104
*specials.Sh = strings.TrimSpace(*specials.Sh)
105-
value := new(bytes.Buffer)
106105
cmd := exec.CommandContext(ctx, "sh", "-c", *specials.Sh)
107106
cmd.Env = fn.ToEnviron(params.Env)
108-
cmd.Stdout = value
107+
108+
stdoutB := new(bytes.Buffer)
109+
cmd.Stdout = stdoutB
110+
109111
stderrB := new(bytes.Buffer)
110112
cmd.Stderr = stderrB
111113
if err := cmd.Run(); err != nil {
112114
return nil, errors.ErrEvalEnvVarSh.WithCtx(ctx).WrapStr(stderrB.String()).KV()
113115
// return nil, errors.ErrEvalEnvVarSh.WithCtx(ctx).KV(attr...)
114116
}
115117

116-
env[k] = strings.TrimSpace(value.String())
118+
env[k] = strings.TrimSpace(stdoutB.String())
117119
}
118120
default:
119121
{

parser/parse-task.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func ParseTask(ctx types.Context, prf *types.ParsedRunfile, task types.Task) (*t
102102

103103
commands := make([]types.ParsedCommandJson, 0, len(task.Commands))
104104
for i := range task.Commands {
105-
c2, err := parseCommand(prf, task.Commands[i])
105+
c2, err := parseCommand(ctx, prf, taskEnv, task.Commands[i])
106106
if err != nil {
107107
return nil, err
108108
}
@@ -116,11 +116,6 @@ func ParseTask(ctx types.Context, prf *types.ParsedRunfile, task types.Task) (*t
116116
watch.Dirs[i] = filepath.Join(*task.Dir, watch.Dirs[i])
117117
}
118118
}
119-
// for i := range watch.ExcludeDirs {
120-
// if !isAbsPath(watch.ExcludeDirs[i]) {
121-
// watch.ExcludeDirs[i] = filepath.Join(*task.Dir, watch.ExcludeDirs[i])
122-
// }
123-
// }
124119
}
125120

126121
return &types.ParsedTask{

parser/parse-task_test.go

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8-
"reflect"
98
"strings"
109
"testing"
1110

1211
fn "github.com/nxtcoder17/runfile/functions"
12+
"github.com/nxtcoder17/runfile/types"
1313
. "github.com/nxtcoder17/runfile/types"
1414
)
1515

@@ -20,47 +20,41 @@ func Test_ParseTask(t *testing.T) {
2020
taskName string
2121
}
2222

23-
areEqual := func(t *testing.T, got, want *ParsedTask) bool {
24-
if want == nil {
25-
return false
23+
compareParsedTasks := func(t *testing.T, got, want *ParsedTask) {
24+
if got == nil && want != nil || got != nil && want == nil {
25+
t.Errorf("ParseTask(), \n\tgot = %v\n\twant = %v", got, want)
26+
return
2627
}
2728

2829
if strings.Join(got.Shell, ",") != strings.Join(want.Shell, ",") {
29-
t.Logf("shell not equal")
30-
return false
30+
t.Errorf("ParseTask(),\n[shell not equal]\n\tgot = %+v\n\twant = %+v", got.Shell, want.Shell)
31+
return
3132
}
3233

3334
if got.Interactive != want.Interactive {
34-
t.Logf("interactive not equal")
35-
return false
35+
t.Errorf("ParseTask(),\n[.interactive not equal]\n\tgot = %+v\n\twant = %+v", got.Interactive, want.Interactive)
36+
return
3637
}
3738

38-
if len(got.Env) != len(want.Env) {
39-
t.Logf("environments not equal")
40-
return false
41-
}
42-
43-
gkeys := fn.MapKeys(got.Env)
44-
45-
for _, k := range gkeys {
46-
v, ok := want.Env[k]
47-
if !ok || v != got.Env[k] {
48-
t.Logf("environments not equal")
49-
return false
50-
}
39+
if fmt.Sprint(got.Env) != fmt.Sprint(want.Env) {
40+
t.Errorf("ParseTask(),\n[.Env not equal]\n\tgot = %+v\n\twant = %+v", got.Env, want.Env)
41+
return
5142
}
5243

5344
if got.WorkingDir != want.WorkingDir {
54-
t.Logf("working dir not equal")
55-
return false
45+
t.Errorf("ParseTask(),\n[.WorkingDir not equal]\n\tgot = %+v\n\twant = %+v", got.WorkingDir, want.WorkingDir)
46+
return
5647
}
5748

58-
if fmt.Sprintf("%#v", got.Commands) != fmt.Sprintf("%#v", want.Commands) {
59-
t.Logf("commands not equal:\n got:\t%#v\nwant:\t%#v", got.Commands, want.Commands)
60-
return false
49+
if len(got.Commands) != len(want.Commands) {
50+
t.Errorf("ParseTask(),\n[len(.Commands) not equal]\n\tgot = %+v\n\twant = %+v", len(got.Commands), len(want.WorkingDir))
51+
return
6152
}
6253

63-
return true
54+
for i := 0; i < len(got.Commands); i++ {
55+
testParseCommandJsonEqual(t, &got.Commands[i], &want.Commands[i])
56+
return
57+
}
6458
}
6559

6660
// for dotenv test
@@ -266,7 +260,7 @@ func Test_ParseTask(t *testing.T) {
266260
rf: &ParsedRunfile{
267261
Tasks: map[string]Task{
268262
"test": {
269-
Env: map[string]any{
263+
Env: EnvVar{
270264
"hello": map[string]any{
271265
"sh": "echo hi",
272266
},
@@ -469,9 +463,7 @@ func Test_ParseTask(t *testing.T) {
469463
WorkingDir: fn.Must(os.Getwd()),
470464
Commands: []ParsedCommandJson{
471465
{
472-
Command: []string{
473-
"echo hello",
474-
},
466+
Command: fn.New("echo hello"),
475467
},
476468
},
477469
},
@@ -501,12 +493,10 @@ echo "hi"
501493
WorkingDir: fn.Must(os.Getwd()),
502494
Commands: []ParsedCommandJson{
503495
{
504-
Command: []string{
505-
`
496+
Command: fn.New(`
506497
echo "hello"
507498
echo "hi"
508-
`,
509-
},
499+
`),
510500
},
511501
},
512502
},
@@ -541,10 +531,10 @@ echo "hi"
541531
WorkingDir: fn.Must(os.Getwd()),
542532
Commands: []ParsedCommandJson{
543533
{
544-
Command: []string{"echo i will call hello, now"},
534+
Command: fn.New("echo i will call hello, now"),
545535
},
546536
{
547-
Run: []string{"hello"},
537+
Run: fn.New("hello"),
548538
},
549539
},
550540
},
@@ -599,8 +589,8 @@ echo "hi"
599589
WorkingDir: fn.Must(os.Getwd()),
600590
Interactive: true,
601591
Commands: []ParsedCommandJson{
602-
{Command: []string{"echo i will call hello, now"}},
603-
{Run: []string{"hello"}},
592+
{Command: fn.New("echo i will call hello, now")},
593+
{Run: fn.New("hello")},
604594
},
605595
},
606596
wantErr: false,
@@ -609,17 +599,17 @@ echo "hi"
609599

610600
for _, tt := range tests {
611601
t.Run(tt.name, func(t *testing.T) {
612-
got, err := ParseTask(context.TODO(), tt.args.rf, tt.args.rf.Tasks[tt.args.taskName])
602+
got, err := ParseTask(types.Context{Context: t.Context()}, tt.args.rf, tt.args.rf.Tasks[tt.args.taskName])
613603
if (err != nil) != tt.wantErr {
614604
t.Errorf("ParseTask(), got = %v, error = %v, wantErr %v", got, err, tt.wantErr)
615605
return
616606
}
617607

618-
if !reflect.DeepEqual(got, tt.want) {
619-
if !areEqual(t, got, tt.want) {
620-
t.Errorf("ParseTask():> \n\tgot:\t%v,\n\twant:\t%v", got, tt.want)
621-
}
608+
if tt.wantErr {
609+
return
622610
}
611+
612+
compareParsedTasks(t, got, tt.want)
623613
})
624614
}
625615
}

0 commit comments

Comments
 (0)