Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
arduino-cli
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Operations
Operations
Metrics
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
arduino-cli
Commits
ad14f711
Commit
ad14f711
authored
Aug 13, 2018
by
Cristian Maglie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented core args test
parent
1ad4ee78
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
20 deletions
+78
-20
commands/core/args.go
commands/core/args.go
+29
-20
commands/core/args_test.go
commands/core/args_test.go
+49
-0
No files found.
commands/core/args.go
View file @
ad14f711
...
@@ -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
{
split
:=
strings
.
SplitN
(
arg
,
"@"
,
2
)
formatter
.
PrintError
(
err
,
"Invalid item "
+
arg
)
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
))
os
.
Exit
(
commands
.
ErrBadArgument
)
os
.
Exit
(
commands
.
ErrBadArgument
)
}
}
ret
=
append
(
ret
,
&
packagemanager
.
PlatformReference
{
ret
=
append
(
ret
,
reference
)
Package
:
split
[
0
],
PlatformArchitecture
:
split
[
1
],
PlatformVersion
:
version
,
})
}
}
return
ret
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
}
commands/core/args_test.go
0 → 100644
View file @
ad14f711
/*
* 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"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment