Skip to content

Commit 6b69e27

Browse files
committed
[skip changelog] Add unit test for lib install from git url (#1464)
1 parent 2a93ac9 commit 6b69e27

File tree

4 files changed

+78
-12
lines changed

4 files changed

+78
-12
lines changed

arduino/libraries/librariesmanager/install.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,17 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
235235
return nil
236236
}
237237

238+
// parseGitURL tries to recover a library name from a git URL.
239+
// Returns an error in case the URL is not a valid git URL.
238240
func parseGitURL(gitURL string) (string, error) {
239241
var res string
240242
if strings.HasPrefix(gitURL, "git@") {
241243
// We can't parse these as URLs
242244
i := strings.LastIndex(gitURL, "/")
243245
res = strings.TrimSuffix(gitURL[i+1:], ".git")
244-
} else if path := paths.New(gitURL); path.Exist() {
246+
} else if path := paths.New(gitURL); path != nil && path.Exist() {
245247
res = path.Base()
246-
} else if parsed, err := url.Parse(gitURL); err == nil {
248+
} else if parsed, err := url.Parse(gitURL); parsed.String() != "" && err == nil {
247249
i := strings.LastIndex(parsed.Path, "/")
248250
res = strings.TrimSuffix(parsed.Path[i+1:], ".git")
249251
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package librariesmanager
17+
18+
import (
19+
"testing"
20+
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestParseGitURL(t *testing.T) {
25+
gitURL := ""
26+
libraryName, err := parseGitURL(gitURL)
27+
require.Equal(t, "", libraryName)
28+
require.Errorf(t, err, "invalid git url")
29+
30+
gitURL = "https://github.com/arduino/arduino-lib.git"
31+
libraryName, err = parseGitURL(gitURL)
32+
require.Equal(t, "arduino-lib", libraryName)
33+
require.NoError(t, err)
34+
35+
gitURL = "[email protected]:arduino/arduino-lib.git"
36+
libraryName, err = parseGitURL(gitURL)
37+
require.Equal(t, "arduino-lib", libraryName)
38+
require.NoError(t, err)
39+
40+
gitURL = "file:///path/to/arduino-lib"
41+
libraryName, err = parseGitURL(gitURL)
42+
require.Equal(t, "arduino-lib", libraryName)
43+
require.NoError(t, err)
44+
45+
gitURL = "file:///path/to/arduino-lib.git"
46+
libraryName, err = parseGitURL(gitURL)
47+
require.Equal(t, "arduino-lib", libraryName)
48+
require.NoError(t, err)
49+
50+
gitURL = "/path/to/arduino-lib"
51+
libraryName, err = parseGitURL(gitURL)
52+
require.Equal(t, "arduino-lib", libraryName)
53+
require.NoError(t, err)
54+
55+
gitURL = "/path/to/arduino-lib.git"
56+
libraryName, err = parseGitURL(gitURL)
57+
require.Equal(t, "arduino-lib", libraryName)
58+
require.NoError(t, err)
59+
60+
gitURL = "file:///path/to/arduino-lib"
61+
libraryName, err = parseGitURL(gitURL)
62+
require.Equal(t, "arduino-lib", libraryName)
63+
require.NoError(t, err)
64+
}

i18n/data/en.po

+3-3
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,7 @@ msgstr "invalid empty library version: %s"
27752775
msgid "invalid empty option found"
27762776
msgstr "invalid empty option found"
27772777

2778-
#: arduino/libraries/librariesmanager/install.go:250
2778+
#: arduino/libraries/librariesmanager/install.go:252
27792779
msgid "invalid git url"
27802780
msgstr "invalid git url"
27812781

@@ -2848,11 +2848,11 @@ msgstr "library %s already installed"
28482848
msgid "library already installed"
28492849
msgstr "library already installed"
28502850

2851-
#: arduino/libraries/librariesmanager/install.go:269
2851+
#: arduino/libraries/librariesmanager/install.go:271
28522852
msgid "library is not valid: missing file \"library.properties\""
28532853
msgstr "library is not valid: missing file \"library.properties\""
28542854

2855-
#: arduino/libraries/librariesmanager/install.go:264
2855+
#: arduino/libraries/librariesmanager/install.go:266
28562856
msgid "library is not valid: missing header file \"%s\""
28572857
msgstr "library is not valid: missing header file \"%s\""
28582858

i18n/rice-box.go

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)