Skip to content

Commit

Permalink
Implemented core args test
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Aug 13, 2018
1 parent 1ad4ee7 commit ad14f71
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
49 changes: 29 additions & 20 deletions commands/core/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,46 @@ import (
"os"
"strings"

"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/commands"

"github.com/arduino/arduino-cli/common/formatter"

"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"go.bug.st/relaxed-semver"
)

// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice.
func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference {
ret := []*packagemanager.PlatformReference{}

for _, arg := range args {
var version *semver.Version
if strings.Contains(arg, "@") {
split := strings.SplitN(arg, "@", 2)
arg = split[0]
if ver, err := semver.Parse(split[1]); err != nil {
formatter.PrintErrorMessage(fmt.Sprintf("invalid item '%s': %s", arg, err))
} else {
version = ver
}
}
split := strings.Split(arg, ":")
if len(split) != 2 {
formatter.PrintErrorMessage(fmt.Sprintf("'%s' is an invalid item (does not match the syntax 'PACKAGER:ARCH[@VERSION]')", arg))
reference, err := parsePlatformReferenceArg(arg)
if err != nil {
formatter.PrintError(err, "Invalid item "+arg)
os.Exit(commands.ErrBadArgument)
}
ret = append(ret, &packagemanager.PlatformReference{
Package: split[0],
PlatformArchitecture: split[1],
PlatformVersion: version,
})
ret = append(ret, reference)
}
return ret
}

func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
split := strings.SplitN(arg, "@", 2)
arg = split[0]
var version *semver.Version
if len(split) > 1 {
if ver, err := semver.Parse(split[1]); err == nil {
version = ver
} else {
return nil, fmt.Errorf("invalid version: %s", err)
}
}
split = strings.Split(arg, ":")
if len(split) != 2 {
return nil, fmt.Errorf("invalid item %s", arg)
}
return &packagemanager.PlatformReference{
Package: split[0],
PlatformArchitecture: split[1],
PlatformVersion: version,
}, nil
}
49 changes: 49 additions & 0 deletions commands/core/args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to [email protected].
*/

package core

import (
"testing"

"github.com/stretchr/testify/require"
semver "go.bug.st/relaxed-semver"
)

func TestParsePlatformReferenceArgs(t *testing.T) {
valid := func(arg, pack, arch, ver string) {
version, _ := semver.Parse(ver) // use nil in case of error

ref, err := parsePlatformReferenceArg(arg)
require.NoError(t, err)
require.Equal(t, pack, ref.Package)
require.Equal(t, arch, ref.PlatformArchitecture)
require.Equal(t, version, ref.PlatformVersion)
}
invalid := func(arg string) {
_, err := parsePlatformReferenceArg(arg)
require.Error(t, err)
}
valid("arduino:avr", "arduino", "avr", "-")
valid("arduino:[email protected]", "arduino", "avr", "1.6.20")
valid("arduino:avr@", "arduino", "avr", "")
invalid("avr")
invalid("arduino:avr:avr")
invalid("[email protected]:avr")
invalid("[email protected]")
invalid("arduino:avr:[email protected]")
}

0 comments on commit ad14f71

Please sign in to comment.