Skip to content

Commit 90e8f0b

Browse files
committed
Added check for missing core/arch/version in core arg parsing
1 parent bc18e91 commit 90e8f0b

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

cli/globals/args.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,18 @@ func ParseReferenceArgs(args []string, parseArch bool) ([]*ReferenceArg, error)
5454
// "packager:arch@version", useful to represent a platform (or core) name.
5555
func ParseReferenceArg(arg string, parseArch bool) (*ReferenceArg, error) {
5656
ret := &ReferenceArg{}
57-
57+
if arg == "" {
58+
return nil, fmt.Errorf("invalid empry core argument")
59+
}
5860
toks := strings.SplitN(arg, "@", 2)
61+
if toks[0] == "" {
62+
return nil, fmt.Errorf("invalid empty core reference '%s'", arg)
63+
}
5964
ret.PackageName = toks[0]
6065
if len(toks) > 1 {
66+
if toks[1] == "" {
67+
return nil, fmt.Errorf("invalid empty core version: '%s'", arg)
68+
}
6169
ret.Version = toks[1]
6270
}
6371

@@ -66,7 +74,13 @@ func ParseReferenceArg(arg string, parseArch bool) (*ReferenceArg, error) {
6674
if len(toks) != 2 {
6775
return nil, fmt.Errorf("invalid item %s", arg)
6876
}
77+
if toks[0] == "" {
78+
return nil, fmt.Errorf("invalid empty core name '%s'", arg)
79+
}
6980
ret.PackageName = toks[0]
81+
if toks[1] == "" {
82+
return nil, fmt.Errorf("invalid empty core architecture '%s'", arg)
83+
}
7084
ret.Architecture = toks[1]
7185
}
7286

@@ -100,7 +114,7 @@ func ParseLibraryReferenceArg(arg string) (*LibraryReferenceArg, error) {
100114
ret.Name = tokens[0]
101115
if len(tokens) > 1 {
102116
if tokens[1] == "" {
103-
return nil, fmt.Errorf("invalid empty library version")
117+
return nil, fmt.Errorf("invalid empty library version: %s", arg)
104118
}
105119
ret.Version = tokens[1]
106120
}

cli/globals/args_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ var goodCores = []struct {
3131
}{
3232
{"arduino:avr", &globals.ReferenceArg{"arduino", "avr", ""}},
3333
{"arduino:avr@1.6.20", &globals.ReferenceArg{"arduino", "avr", "1.6.20"}},
34-
{"arduino:avr@", &globals.ReferenceArg{"arduino", "avr", ""}},
3534
}
3635

3736
var goodLibs = []struct {
@@ -49,6 +48,11 @@ var badCores = []struct {
4948
{"arduino:avr:avr", nil},
5049
{"arduino@1.6.20:avr", nil},
5150
{"arduino:avr:avr@1.6.20", nil},
51+
{"arduino:@1.6.20", nil},
52+
{":avr@1.5.0", nil},
53+
{"@1.5.0", nil},
54+
{"arduino:avr@", nil},
55+
{"", nil},
5256
}
5357

5458
var badLibs = []struct {
@@ -63,6 +67,9 @@ func TestArgsStringify(t *testing.T) {
6367
for _, lib := range goodLibs {
6468
require.Equal(t, lib.in, lib.expected.String())
6569
}
70+
for _, core := range goodCores {
71+
require.Equal(t, core.in, core.expected.String())
72+
}
6673
}
6774

6875
func TestParseReferenceArgCores(t *testing.T) {
@@ -74,8 +81,8 @@ func TestParseReferenceArgCores(t *testing.T) {
7481

7582
for _, tt := range badCores {
7683
actual, err := globals.ParseReferenceArg(tt.in, true)
77-
assert.NotNil(t, err)
78-
assert.Equal(t, tt.expected, actual)
84+
require.NotNil(t, err, "Testing bad core '%s'", tt.in)
85+
require.Equal(t, tt.expected, actual, "Testing bad core '%s'", tt.in)
7986
}
8087
}
8188

0 commit comments

Comments
 (0)