Skip to content

Commit 7878a2c

Browse files
committed
refactor: rename validation functions and update test cases for clarity and consistency
1 parent 58a4227 commit 7878a2c

File tree

2 files changed

+56
-42
lines changed

2 files changed

+56
-42
lines changed

internal/orchestrator/app/validator.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@ import (
66
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
77
)
88

9-
func ValidateAppDescriptor(a AppDescriptor, index *bricksindex.BricksIndex) error {
10-
return validatebricks(a, index)
11-
}
12-
13-
func validatebricks(a AppDescriptor, index *bricksindex.BricksIndex) error {
9+
// ValidateBricks checks that all bricks referenced in the given AppDescriptor exist in the provided BricksIndex,
10+
// and that all required variables for each brick are present and valid. It returns an error if any brick is missing,
11+
// if any variable referenced by the app does not exist in the corresponding brick, or if any required variable is missing.
12+
// If the index is nil, validation is skipped and nil is returned.
13+
func ValidateBricks(a AppDescriptor, index *bricksindex.BricksIndex) error {
14+
if index == nil {
15+
return nil
16+
}
1417
for _, appBrick := range a.Bricks {
1518
indexBrick, found := index.FindBrickByID(appBrick.ID)
1619
if !found {
1720
return fmt.Errorf("brick %q not found", appBrick.ID)
1821
}
1922

20-
// check the bricks variables inside the app.yaml are valid given a brick definition
2123
for appBrickName := range appBrick.Variables {
2224
_, exist := indexBrick.GetVariable(appBrickName)
2325
if !exist {
2426
return fmt.Errorf("variable %q does not exist on brick %q", appBrickName, indexBrick.ID)
2527
}
2628
}
2729

28-
// check all required variables has a value
2930
for _, indexBrickVariable := range indexBrick.Variables {
3031
if indexBrickVariable.IsRequired() {
3132
if _, exist := appBrick.Variables[indexBrickVariable.Name]; !exist {

internal/orchestrator/app/validator_test.go

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,62 @@ import (
55

66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/require"
8+
"go.bug.st/f"
89

910
"github.com/arduino/go-paths-helper"
1011

1112
"github.com/arduino/arduino-app-cli/internal/orchestrator/bricksindex"
1213
)
1314

14-
func TestValidateAppDescriptor(t *testing.T) {
15+
func TestValidateBricksOnAppDescriptor(t *testing.T) {
1516
bricksIndex, err := bricksindex.GenerateBricksIndexFromFile(paths.New("testdata/validator"))
1617
require.Nil(t, err)
1718

18-
t.Run("valid app descriptor with no bricks", func(t *testing.T) {
19-
app, err := ParseDescriptorFile(paths.New("testdata/validator/no-bricks-app.yaml"))
20-
require.NoError(t, err)
21-
err = ValidateAppDescriptor(app, bricksIndex)
22-
assert.NoError(t, err)
23-
})
24-
25-
t.Run("valid app descriptor with empty list of bricks", func(t *testing.T) {
26-
app, err := ParseDescriptorFile(paths.New("testdata/validator/empty-bricks-app.yaml"))
27-
require.NoError(t, err)
28-
err = ValidateAppDescriptor(app, bricksIndex)
29-
assert.NoError(t, err)
30-
})
31-
32-
t.Run("invalid app descriptor with missing required variable", func(t *testing.T) {
33-
app, err := ParseDescriptorFile(paths.New("testdata/validator/missing-required-app.yaml"))
34-
require.NoError(t, err)
35-
err = ValidateAppDescriptor(app, bricksIndex)
36-
assert.Equal(t, "variable \"ARDUINO_DEVICE_ID\" is required by brick \"arduino:arduino_cloud\"", err.Error())
37-
})
38-
39-
t.Run("invalid app descriptor with a missing required variable among two", func(t *testing.T) {
40-
app, err := ParseDescriptorFile(paths.New("testdata/validator/mixed-required-app.yaml"))
41-
require.NoError(t, err)
42-
err = ValidateAppDescriptor(app, bricksIndex)
43-
assert.Equal(t, "variable \"ARDUINO_SECRET\" is required by brick \"arduino:arduino_cloud\"", err.Error())
44-
})
45-
46-
t.Run("invalid app descriptor with not found brick id", func(t *testing.T) {
47-
app, err := ParseDescriptorFile(paths.New("testdata/validator/not-found-brick-app.yaml"))
48-
require.NoError(t, err)
49-
err = ValidateAppDescriptor(app, bricksIndex)
50-
assert.Equal(t, "brick \"arduino:not_existing_brick\" not found", err.Error())
51-
})
19+
testCases := []struct {
20+
name string
21+
filename string
22+
expectedErr *string
23+
}{
24+
{
25+
name: "valid app descriptor with no bricks",
26+
filename: "no-bricks-app.yaml",
27+
expectedErr: nil,
28+
},
29+
{
30+
name: "valid app descriptor with empty list of bricks",
31+
filename: "empty-bricks-app.yaml",
32+
expectedErr: nil,
33+
},
34+
{
35+
name: "invalid app descriptor with missing required variable",
36+
filename: "missing-required-app.yaml",
37+
expectedErr: f.Ptr("variable \"ARDUINO_DEVICE_ID\" is required by brick \"arduino:arduino_cloud\""),
38+
},
39+
{
40+
name: "invalid app descriptor with a missing required variable among two",
41+
filename: "mixed-required-app.yaml",
42+
expectedErr: f.Ptr("variable \"ARDUINO_SECRET\" is required by brick \"arduino:arduino_cloud\""),
43+
},
44+
{
45+
name: "invalid app descriptor with not found brick id",
46+
filename: "not-found-brick-app.yaml",
47+
expectedErr: f.Ptr("brick \"arduino:not_existing_brick\" not found"),
48+
},
49+
}
5250

51+
for _, tc := range testCases {
52+
t.Run(tc.name, func(t *testing.T) {
53+
app, err := ParseDescriptorFile(paths.New("testdata/validator/" + tc.filename))
54+
require.NoError(t, err)
55+
56+
err = ValidateBricks(app, bricksIndex)
57+
58+
if tc.expectedErr == nil {
59+
assert.NoError(t, err)
60+
} else {
61+
require.Error(t, err)
62+
assert.Equal(t, *tc.expectedErr, err.Error())
63+
}
64+
})
65+
}
5366
}

0 commit comments

Comments
 (0)