Skip to content

Commit 55d99f1

Browse files
authored
feat: [DEVEX-1088] add warn when executor param has no default value (#203)
* feat: warn instead of err when executor param has no default * test: add test for missing default executor parameter
1 parent ebac506 commit 55d99f1

2 files changed

Lines changed: 79 additions & 33 deletions

File tree

pkg/parser/validate/jobs.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ func (val Validate) validateSingleJob(job ast.Job) {
3939

4040
checkParam := func(executorDefault string, rng protocol.Range) {
4141
isOrbExecutor, err := val.doesOrbExecutorExist(executorDefault, rng)
42-
if val.Context.Api.UseDefaultInstance() && !val.Doc.DoesExecutorExist(executorDefault) &&
42+
if !param.IsOptional() {
43+
val.addDiagnostic(
44+
protocol.Diagnostic{
45+
Range: rng,
46+
Message: fmt.Sprintf(
47+
"No default value specified for parameter `%s`.",
48+
paramName,
49+
),
50+
Severity: protocol.DiagnosticSeverityWarning,
51+
},
52+
)
53+
} else if val.Context.Api.UseDefaultInstance() && !val.Doc.DoesExecutorExist(executorDefault) &&
4354
(!isOrbExecutor && err == nil) {
4455
// Error on the default value
4556
val.addDiagnostic(

pkg/parser/validate/jobs_test.go

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,84 @@ import (
1111
)
1212

1313
func TestExecutorParam(t *testing.T) {
14-
yamlData := `jobs:
14+
testCases := []struct {
15+
label string
16+
yamlData string
17+
expectedDiag protocol.Diagnostic
18+
}{
19+
{
20+
label: "without default",
21+
yamlData: `jobs:
22+
test:
23+
parameters:
24+
os:
25+
type: executor
26+
executor: << parameters.os >>
27+
steps:
28+
- checkout`,
29+
expectedDiag: protocol.Diagnostic{
30+
Range: protocol.Range{
31+
Start: protocol.Position{Line: 5, Character: 4},
32+
End: protocol.Position{Line: 5, Character: 33},
33+
},
34+
Severity: protocol.DiagnosticSeverityWarning,
35+
},
36+
},
37+
{
38+
label: "with unknown default",
39+
yamlData: `jobs:
1540
test:
1641
parameters:
1742
os:
1843
type: executor
44+
default: unknown
1945
executor: << parameters.os >>
2046
steps:
21-
- checkout`
22-
ctx := &utils.LsContext{
23-
Api: utils.ApiContext{
24-
Token: "XXXXXXXXXXXX",
25-
HostUrl: "https://circleci.com",
47+
- checkout`,
48+
expectedDiag: protocol.Diagnostic{
49+
Range: protocol.Range{
50+
Start: protocol.Position{Line: 6, Character: 4},
51+
End: protocol.Position{Line: 6, Character: 33},
52+
},
53+
Severity: protocol.DiagnosticSeverityError,
54+
},
2655
},
2756
}
28-
doc, err := parser.ParseFromContent(
29-
[]byte(yamlData),
30-
ctx,
31-
uri.URI(""),
32-
protocol.Position{},
33-
)
34-
assert.NoError(t, err, "invalid YAML data")
35-
assert.Contains(t, doc.Jobs, "test")
3657

37-
val := Validate{
38-
Context: ctx,
39-
Doc: doc,
40-
Diagnostics: &[]protocol.Diagnostic{},
41-
}
42-
val.validateSingleJob(doc.Jobs["test"])
58+
for _, testCase := range testCases {
59+
t.Run("executor parameter: "+testCase.label, func(t *testing.T) {
4360

44-
expectedDiag := protocol.Diagnostic{
45-
Range: protocol.Range{
46-
Start: protocol.Position{Line: 5, Character: 4},
47-
End: protocol.Position{Line: 5, Character: 33},
48-
},
49-
Severity: protocol.DiagnosticSeverityError,
50-
}
51-
for _, diag := range *val.Diagnostics {
52-
if diag.Range == expectedDiag.Range && diag.Severity == expectedDiag.Severity {
53-
return
54-
}
61+
ctx := &utils.LsContext{
62+
Api: utils.ApiContext{
63+
Token: "XXXXXXXXXXXX",
64+
HostUrl: "https://circleci.com",
65+
},
66+
}
67+
doc, err := parser.ParseFromContent(
68+
[]byte(testCase.yamlData),
69+
ctx,
70+
uri.URI(""),
71+
protocol.Position{},
72+
)
73+
assert.NoError(t, err, "invalid YAML data")
74+
assert.Contains(t, doc.Jobs, "test")
75+
76+
val := Validate{
77+
Context: ctx,
78+
Doc: doc,
79+
Diagnostics: &[]protocol.Diagnostic{},
80+
}
81+
val.validateSingleJob(doc.Jobs["test"])
82+
83+
for _, diag := range *val.Diagnostics {
84+
if diag.Range == testCase.expectedDiag.Range &&
85+
diag.Severity == testCase.expectedDiag.Severity {
86+
return
87+
}
88+
}
89+
t.Fatalf(`missing "parameter as executor" diagnostic`)
90+
})
5591
}
56-
t.Fatalf(`missing "parameter as executor" diagnostic`)
5792
}
5893

5994
func TestResourceClass(t *testing.T) {

0 commit comments

Comments
 (0)