Skip to content

Commit ad14f71

Browse files
committed
Implemented core args test
1 parent 1ad4ee7 commit ad14f71

File tree

2 files changed

+78
-20
lines changed

2 files changed

+78
-20
lines changed

commands/core/args.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,46 @@ import (
2222
"os"
2323
"strings"
2424

25-
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2625
"github.com/arduino/arduino-cli/commands"
26+
2727
"github.com/arduino/arduino-cli/common/formatter"
28+
29+
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
2830
"go.bug.st/relaxed-semver"
2931
)
3032

3133
// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice.
3234
func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference {
3335
ret := []*packagemanager.PlatformReference{}
34-
3536
for _, arg := range args {
36-
var version *semver.Version
37-
if strings.Contains(arg, "@") {
38-
split := strings.SplitN(arg, "@", 2)
39-
arg = split[0]
40-
if ver, err := semver.Parse(split[1]); err != nil {
41-
formatter.PrintErrorMessage(fmt.Sprintf("invalid item '%s': %s", arg, err))
42-
} else {
43-
version = ver
44-
}
45-
}
46-
split := strings.Split(arg, ":")
47-
if len(split) != 2 {
48-
formatter.PrintErrorMessage(fmt.Sprintf("'%s' is an invalid item (does not match the syntax 'PACKAGER:ARCH[@VERSION]')", arg))
37+
reference, err := parsePlatformReferenceArg(arg)
38+
if err != nil {
39+
formatter.PrintError(err, "Invalid item "+arg)
4940
os.Exit(commands.ErrBadArgument)
5041
}
51-
ret = append(ret, &packagemanager.PlatformReference{
52-
Package: split[0],
53-
PlatformArchitecture: split[1],
54-
PlatformVersion: version,
55-
})
42+
ret = append(ret, reference)
5643
}
5744
return ret
5845
}
46+
47+
func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
48+
split := strings.SplitN(arg, "@", 2)
49+
arg = split[0]
50+
var version *semver.Version
51+
if len(split) > 1 {
52+
if ver, err := semver.Parse(split[1]); err == nil {
53+
version = ver
54+
} else {
55+
return nil, fmt.Errorf("invalid version: %s", err)
56+
}
57+
}
58+
split = strings.Split(arg, ":")
59+
if len(split) != 2 {
60+
return nil, fmt.Errorf("invalid item %s", arg)
61+
}
62+
return &packagemanager.PlatformReference{
63+
Package: split[0],
64+
PlatformArchitecture: split[1],
65+
PlatformVersion: version,
66+
}, nil
67+
}

commands/core/args_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to [email protected].
16+
*/
17+
18+
package core
19+
20+
import (
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
semver "go.bug.st/relaxed-semver"
25+
)
26+
27+
func TestParsePlatformReferenceArgs(t *testing.T) {
28+
valid := func(arg, pack, arch, ver string) {
29+
version, _ := semver.Parse(ver) // use nil in case of error
30+
31+
ref, err := parsePlatformReferenceArg(arg)
32+
require.NoError(t, err)
33+
require.Equal(t, pack, ref.Package)
34+
require.Equal(t, arch, ref.PlatformArchitecture)
35+
require.Equal(t, version, ref.PlatformVersion)
36+
}
37+
invalid := func(arg string) {
38+
_, err := parsePlatformReferenceArg(arg)
39+
require.Error(t, err)
40+
}
41+
valid("arduino:avr", "arduino", "avr", "-")
42+
valid("arduino:[email protected]", "arduino", "avr", "1.6.20")
43+
valid("arduino:avr@", "arduino", "avr", "")
44+
invalid("avr")
45+
invalid("arduino:avr:avr")
46+
invalid("[email protected]:avr")
47+
invalid("[email protected]")
48+
invalid("arduino:avr:[email protected]")
49+
}

0 commit comments

Comments
 (0)