Skip to content

Commit c141c4e

Browse files
committed
[skip changelog] Add unit test for lib install from git url
1 parent 1db5944 commit c141c4e

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

Diff for: arduino/libraries/librariesmanager/install.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,17 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
240240
return nil
241241
}
242242

243+
// parseGitURL tries to recover a library name from a git URL.
244+
// Returns an error in case the URL is not a valid git URL.
243245
func parseGitURL(gitURL string) (string, error) {
244246
var res string
245247
if strings.HasPrefix(gitURL, "git@") {
246248
// We can't parse these as URLs
247249
i := strings.LastIndex(gitURL, "/")
248250
res = strings.TrimSuffix(gitURL[i+1:], ".git")
249-
} else if path := paths.New(gitURL); path.Exist() {
251+
} else if path := paths.New(gitURL); path != nil && path.Exist() {
250252
res = path.Base()
251-
} else if parsed, err := url.Parse(gitURL); err == nil {
253+
} else if parsed, err := url.Parse(gitURL); parsed.String() != "" && err == nil {
252254
i := strings.LastIndex(parsed.Path, "/")
253255
res = strings.TrimSuffix(parsed.Path[i+1:], ".git")
254256
} else {

Diff for: arduino/libraries/librariesmanager/install_test.go

+64
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+
}

0 commit comments

Comments
 (0)