Commit ad14f711 authored by Cristian Maglie's avatar Cristian Maglie

Implemented core args test

parent 1ad4ee78
...@@ -22,37 +22,46 @@ import ( ...@@ -22,37 +22,46 @@ import (
"os" "os"
"strings" "strings"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/common/formatter" "github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/arduino/cores/packagemanager"
"go.bug.st/relaxed-semver" "go.bug.st/relaxed-semver"
) )
// parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice. // parsePlatformReferenceArgs parses a sequence of "packager:arch@version" tokens and returns a platformReference slice.
func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference { func parsePlatformReferenceArgs(args []string) []*packagemanager.PlatformReference {
ret := []*packagemanager.PlatformReference{} ret := []*packagemanager.PlatformReference{}
for _, arg := range args { for _, arg := range args {
var version *semver.Version reference, err := parsePlatformReferenceArg(arg)
if strings.Contains(arg, "@") { if err != nil {
formatter.PrintError(err, "Invalid item "+arg)
os.Exit(commands.ErrBadArgument)
}
ret = append(ret, reference)
}
return ret
}
func parsePlatformReferenceArg(arg string) (*packagemanager.PlatformReference, error) {
split := strings.SplitN(arg, "@", 2) split := strings.SplitN(arg, "@", 2)
arg = split[0] arg = split[0]
if ver, err := semver.Parse(split[1]); err != nil { var version *semver.Version
formatter.PrintErrorMessage(fmt.Sprintf("invalid item '%s': %s", arg, err)) if len(split) > 1 {
} else { if ver, err := semver.Parse(split[1]); err == nil {
version = ver version = ver
} else {
return nil, fmt.Errorf("invalid version: %s", err)
} }
} }
split := strings.Split(arg, ":") split = strings.Split(arg, ":")
if len(split) != 2 { if len(split) != 2 {
formatter.PrintErrorMessage(fmt.Sprintf("'%s' is an invalid item (does not match the syntax 'PACKAGER:ARCH[@VERSION]')", arg)) return nil, fmt.Errorf("invalid item %s", arg)
os.Exit(commands.ErrBadArgument)
} }
ret = append(ret, &packagemanager.PlatformReference{ return &packagemanager.PlatformReference{
Package: split[0], Package: split[0],
PlatformArchitecture: split[1], PlatformArchitecture: split[1],
PlatformVersion: version, PlatformVersion: version,
}) }, nil
}
return ret
} }
/*
* 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 license@arduino.cc.
*/
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:avr@1.6.20", "arduino", "avr", "1.6.20")
valid("arduino:avr@", "arduino", "avr", "")
invalid("avr")
invalid("arduino:avr:avr")
invalid("arduino@1.6.20:avr")
invalid("avr@1.6.20")
invalid("arduino:avr:avr@1.6.20")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment