Commit 6b69e274 authored by Silvano Cerza's avatar Silvano Cerza

[skip changelog] Add unit test for lib install from git url (#1464)

parent 2a93ac9e
......@@ -235,15 +235,17 @@ func (lm *LibrariesManager) InstallGitLib(gitURL string, overwrite bool) error {
return nil
}
// parseGitURL tries to recover a library name from a git URL.
// Returns an error in case the URL is not a valid git URL.
func parseGitURL(gitURL string) (string, error) {
var res string
if strings.HasPrefix(gitURL, "git@") {
// We can't parse these as URLs
i := strings.LastIndex(gitURL, "/")
res = strings.TrimSuffix(gitURL[i+1:], ".git")
} else if path := paths.New(gitURL); path.Exist() {
} else if path := paths.New(gitURL); path != nil && path.Exist() {
res = path.Base()
} else if parsed, err := url.Parse(gitURL); err == nil {
} else if parsed, err := url.Parse(gitURL); parsed.String() != "" && err == nil {
i := strings.LastIndex(parsed.Path, "/")
res = strings.TrimSuffix(parsed.Path[i+1:], ".git")
} else {
......
// This file is part of arduino-cli.
//
// Copyright 2020 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 librariesmanager
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestParseGitURL(t *testing.T) {
gitURL := ""
libraryName, err := parseGitURL(gitURL)
require.Equal(t, "", libraryName)
require.Errorf(t, err, "invalid git url")
gitURL = "https://github.com/arduino/arduino-lib.git"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "git@github.com:arduino/arduino-lib.git"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "file:///path/to/arduino-lib"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "file:///path/to/arduino-lib.git"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "/path/to/arduino-lib"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "/path/to/arduino-lib.git"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
gitURL = "file:///path/to/arduino-lib"
libraryName, err = parseGitURL(gitURL)
require.Equal(t, "arduino-lib", libraryName)
require.NoError(t, err)
}
......@@ -2775,7 +2775,7 @@ msgstr "invalid empty library version: %s"
msgid "invalid empty option found"
msgstr "invalid empty option found"
#: arduino/libraries/librariesmanager/install.go:250
#: arduino/libraries/librariesmanager/install.go:252
msgid "invalid git url"
msgstr "invalid git url"
......@@ -2848,11 +2848,11 @@ msgstr "library %s already installed"
msgid "library already installed"
msgstr "library already installed"
#: arduino/libraries/librariesmanager/install.go:269
#: arduino/libraries/librariesmanager/install.go:271
msgid "library is not valid: missing file \"library.properties\""
msgstr "library is not valid: missing file \"library.properties\""
#: arduino/libraries/librariesmanager/install.go:264
#: arduino/libraries/librariesmanager/install.go:266
msgid "library is not valid: missing header file \"%s\""
msgstr "library is not valid: missing header file \"%s\""
......
This source diff could not be displayed because it is too large. You can view the blob instead.
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