Skip to content

Commit 8a23bee

Browse files
committed
Add more context to run error
So that we get more descriptive error message in cases like #39
1 parent e700bc8 commit 8a23bee

File tree

6 files changed

+42
-18
lines changed

6 files changed

+42
-18
lines changed

examples/concurrency/concurrency_test.variant

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ test "test" {
3333

3434
case "ng1" {
3535
concurrency = 0
36-
err = "concurrency less than 1 can not be set. If you wanted 0 for a concurrency equals to the number of steps, is isn't a good idea. Some system has a relatively lower fd limit that can make your command fail only when there are too many steps. Always use static number of concurrency"
36+
err = "job \"test\": concurrency less than 1 can not be set. If you wanted 0 for a concurrency equals to the number of steps, is isn't a good idea. Some system has a relatively lower fd limit that can make your command fail only when there are too many steps. Always use static number of concurrency"
3737
stdout = ""
3838
delayone = 0
3939
delaytwo = 1

examples/simple/simple_test.variant

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ test "app deploy" {
99
namespace = "foo"
1010
exitstatus = 1
1111
err = trimspace(<<EOS
12-
command "bash -c kubectl -n foo apply -f examples/simple/manifests/
12+
job "app deploy": job "shell": command "bash -c kubectl -n foo apply -f examples/simple/manifests/
1313
": exit status 1
1414
EOS
1515
)

examples/testing-with-expectations/example_test.variant

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ test "example" {
3131
tenant = "wrongclient"
3232

3333
err = <<EOS
34-
1 error occurred:
34+
step "init": job "init": 1 error occurred:
3535
* unexpected exec 1: expected args [-c cd account && terraform init && (terraform workspace select client || terraform workspace new client], got [-c cd account && terraform init && (terraform workspace select wrongclient || terraform workspace new wrongclient)]
3636

3737
EOS

main_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestExamples(t *testing.T) {
3939
variantName: "simple",
4040
args: []string{"variant", "app", "deploy", "--namespace", "ns1"},
4141
variantDir: "./examples/simple",
42-
expectErr: "command \"bash -c kubectl -n ns1 apply -f examples/simple/manifests/\n\": exit status 1",
42+
expectErr: "job \"shell\": command \"bash -c kubectl -n ns1 apply -f examples/simple/manifests/\n\": exit status 1",
4343
},
4444
{
4545
variantName: "simple",
@@ -50,7 +50,7 @@ func TestExamples(t *testing.T) {
5050
subject: "variant run app deploy on simple example w/ ns1",
5151
args: []string{"variant", "run", "app", "deploy", "--namespace", "ns1"},
5252
variantDir: "./examples/simple",
53-
expectErr: "command \"bash -c kubectl -n ns1 apply -f examples/simple/manifests/\n\": exit status 1",
53+
expectErr: "job \"shell\": command \"bash -c kubectl -n ns1 apply -f examples/simple/manifests/\n\": exit status 1",
5454
},
5555
{
5656
subject: "variant run app deploy on simple example w/ default",

pkg/app/app.go

+35-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,12 @@ func (app *App) Run(cmd string, args map[string]interface{}, opts map[string]int
5454
return nil, err
5555
}
5656

57-
return jr()
57+
res, err := jr()
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
return res, nil
5863
}
5964

6065
func (app *App) run(l *EventLogger, cmd string, args map[string]interface{}, streamOutput bool) (*Result, error) {
@@ -66,17 +71,30 @@ func (app *App) run(l *EventLogger, cmd string, args map[string]interface{}, str
6671

6772
jr, err := app.Job(l, cmd, args, args, nil, streamOutput)
6873
if err != nil {
74+
if cmd != "" {
75+
return nil, xerrors.Errorf("job %q: %w", cmd, err)
76+
}
77+
return nil, err
78+
}
79+
80+
res, err := jr()
81+
if err != nil {
82+
if cmd != "" {
83+
return nil, xerrors.Errorf("job %q: %w", cmd, err)
84+
}
6985
return nil, err
7086
}
7187

72-
return jr()
88+
return res, nil
7389
}
7490

7591
func (app *App) Job(l *EventLogger, cmd string, args map[string]interface{}, opts map[string]interface{}, f SetOptsFunc, streamOutput bool) (func() (*Result, error), error) {
7692
jobByName := app.JobByName
7793

78-
j, ok := jobByName[cmd]
79-
if !ok {
94+
j, cmdDefined := jobByName[cmd]
95+
if !cmdDefined {
96+
var ok bool
97+
8098
j, ok = jobByName[""]
8199
if !ok {
82100
return nil, fmt.Errorf("command %q not found", cmd)
@@ -213,7 +231,11 @@ func (app *App) Job(l *EventLogger, cmd string, args map[string]interface{}, opt
213231
}
214232

215233
if err == nil {
216-
return nil, fmt.Errorf(NoRunMessage)
234+
if !cmdDefined {
235+
return nil, xerrors.Errorf("job %q is not defined", cmd)
236+
}
237+
238+
return nil, errors.New(NoRunMessage)
217239
}
218240
} else {
219241
// The job contained job or step(s).
@@ -602,7 +624,7 @@ func (app *App) execTest(t *testing.T, test Test) *Result {
602624
res, err = testApp.execTestCase(test, c)
603625
if err != nil {
604626
app.PrintError(err)
605-
t.Fatalf("%v", err)
627+
t.Fatalf("%s: %v", c.SourceLocator.Range(), err)
606628
}
607629
})
608630
}
@@ -967,7 +989,7 @@ func (app *App) execJobSteps(l *EventLogger, jobCtx *JobContext, results map[str
967989
f := func() (*Result, error) {
968990
res, err := app.runJobAndUpdateContext(l, &stepCtx, eitherJobRun{static: &s.Run}, m, streamOutput)
969991
if err != nil {
970-
return res, err
992+
return res, xerrors.Errorf("step %q: %w", s.Name, err)
971993
}
972994

973995
m.Lock()
@@ -1248,12 +1270,12 @@ func (app *App) createJobContext(cc *HCL2Config, j JobSpec, givenParams map[stri
12481270

12491271
globalParams, err := setParameterValues("global parameter", ctx, cc.Parameters, givenParams)
12501272
if err != nil {
1251-
return nil, fmt.Errorf("job %q: %w", j.Name, err)
1273+
return nil, err
12521274
}
12531275

12541276
localParams, err := setParameterValues("parameter", ctx, j.Parameters, givenParams)
12551277
if err != nil {
1256-
return nil, fmt.Errorf("job %q: %w", j.Name, err)
1278+
return nil, err
12571279
}
12581280

12591281
params := map[string]cty.Value{}
@@ -1275,12 +1297,12 @@ func (app *App) createJobContext(cc *HCL2Config, j JobSpec, givenParams map[stri
12751297

12761298
globalOpts, err := setOptionValues("global option", ctx, cc.Options, givenOpts, f)
12771299
if err != nil {
1278-
return nil, fmt.Errorf("job %q: %w", j.Name, err)
1300+
return nil, err
12791301
}
12801302

12811303
localOpts, err := setOptionValues("option", ctx, j.Options, givenOpts, f)
12821304
if err != nil {
1283-
return nil, fmt.Errorf("job %q: %w", j.Name, err)
1305+
return nil, err
12841306
}
12851307

12861308
opts := map[string]cty.Value{}
@@ -1410,7 +1432,7 @@ func (app *App) getConfigs(jobCtx *JobContext, cc *HCL2Config, j JobSpec, confTy
14101432
yamlData, err = ioutil.ReadFile(source.Path)
14111433
if err != nil {
14121434
if source.Default == nil {
1413-
return cty.NilVal, fmt.Errorf("job %q: %s %q: source %d: %w", j.Name, confType, confSpec.Name, sourceIdx, err)
1435+
return cty.NilVal, fmt.Errorf("%s %q: source %d: %w", confType, confSpec.Name, sourceIdx, err)
14141436
}
14151437

14161438
yamlData = []byte(*source.Default)
@@ -1434,7 +1456,7 @@ func (app *App) getConfigs(jobCtx *JobContext, cc *HCL2Config, j JobSpec, confTy
14341456

14351457
res, err := app.run(nil, source.Name, args, false)
14361458
if err != nil {
1437-
return cty.NilVal, err
1459+
return cty.NilVal, xerrors.Errorf("%s %q: source %d: %w", confType, confSpec.Name, sourceIdx, err)
14381460
}
14391461

14401462
yamlData = []byte(res.Stdout)

pkg/app/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ type expectedExec struct {
201201
}
202202

203203
type Case struct {
204+
SourceLocator hcl.Expression `hcl:"__source_locator,attr"`
205+
204206
Name string `hcl:"name,label"`
205207

206208
Args map[string]hcl.Expression `hcl:",remain"`

0 commit comments

Comments
 (0)