Skip to content

Commit

Permalink
Fixed library install from git-url when reference points to a git bra…
Browse files Browse the repository at this point in the history
…nch (#2833)

* Added integration test

* Fixed library install from git when ref points to a branch

* Praise linter
  • Loading branch information
cmaglie authored Feb 11, 2025
1 parent a2eebcd commit bb88dc2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
26 changes: 8 additions & 18 deletions internal/arduino/libraries/librariesmanager/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,15 @@ func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {
if ref != "" {
depth = 0
}
repo, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
URL: gitURL,
Depth: depth,
Progress: os.Stdout,
})
if err != nil {
if _, err := git.PlainClone(tmpInstallPath.String(), false, &git.CloneOptions{
URL: gitURL,
Depth: depth,
Progress: os.Stdout,
ReferenceName: ref,
}); err != nil {
return err
}

if ref != "" {
if h, err := repo.ResolveRevision(ref); err != nil {
return err
} else if w, err := repo.Worktree(); err != nil {
return err
} else if err := w.Checkout(&git.CheckoutOptions{Hash: plumbing.NewHash(h.String())}); err != nil {
return err
}
}

// We don't want the installed library to be a git repository thus we delete this folder
tmpInstallPath.Join(".git").RemoveAll()

Expand All @@ -251,7 +241,7 @@ func (lmi *Installer) InstallGitLib(argURL string, overwrite bool) error {

// parseGitArgURL tries to recover a library name from a git URL.
// Returns an error in case the URL is not a valid git URL.
func parseGitArgURL(argURL string) (string, string, plumbing.Revision, error) {
func parseGitArgURL(argURL string) (string, string, plumbing.ReferenceName, error) {
// On Windows handle paths with backslashes in the form C:\Path\to\library
if path := paths.New(argURL); path != nil && path.Exist() {
return path.Base(), argURL, "", nil
Expand Down Expand Up @@ -289,7 +279,7 @@ func parseGitArgURL(argURL string) (string, string, plumbing.Revision, error) {
return "", "", "", errors.New(i18n.Tr("invalid git url"))
}
// fragment == "1.0.3"
rev := plumbing.Revision(parsedURL.Fragment)
rev := plumbing.ReferenceName(parsedURL.Fragment)
// gitURL == "https://github.com/arduino-libraries/SigFox.git"
parsedURL.Fragment = ""
gitURL := parsedURL.String()
Expand Down
52 changes: 36 additions & 16 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,27 +659,47 @@ func TestInstallWithGitUrlFragmentAsBranch(t *testing.T) {
_, _, err := cli.RunWithCustomEnv(envVar, "config", "init", "--dest-dir", ".")
require.NoError(t, err)

libInstallDir := cli.SketchbookDir().Join("libraries", "WiFi101")
// Verifies library is not already installed
require.NoDirExists(t, libInstallDir.String())
t.Run("InvalidRef", func(t *testing.T) {
// Test that a bad ref fails
_, _, err = cli.Run("lib", "install", "--git-url", "https://github.com/arduino-libraries/WiFi101.git#x-ref-does-not-exist", "--config-file", "arduino-cli.yaml")
require.Error(t, err)
})

gitUrl := "https://github.com/arduino-libraries/WiFi101.git"
t.Run("RefPointingToATag", func(t *testing.T) {
gitUrl := "https://github.com/arduino-libraries/WiFi101.git"
libInstallDir := cli.SketchbookDir().Join("libraries", "WiFi101").String()

// Test that a bad ref fails
_, _, err = cli.Run("lib", "install", "--git-url", gitUrl+"#x-ref-does-not-exist", "--config-file", "arduino-cli.yaml")
require.Error(t, err)
// Verifies library is not already installed
require.NoDirExists(t, libInstallDir)

// Verifies library is installed in expected path
_, _, err = cli.Run("lib", "install", "--git-url", gitUrl+"#0.16.0", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
require.DirExists(t, libInstallDir.String())
// Verifies library is installed in expected path
_, _, err = cli.Run("lib", "install", "--git-url", gitUrl+"#0.16.0", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
require.DirExists(t, libInstallDir)

// Reinstall library at an existing ref
_, _, err = cli.Run("lib", "install", "--git-url", gitUrl+"#master", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
// Reinstall library at an existing ref
_, _, err = cli.Run("lib", "install", "--git-url", gitUrl+"#master", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

// Verifies library remains installed
require.DirExists(t, libInstallDir.String())
// Verifies library remains installed
require.DirExists(t, libInstallDir)
})

t.Run("RefPointingToBranch", func(t *testing.T) {
libInstallDir := cli.SketchbookDir().Join("libraries", "ArduinoCloud")

// Verify install with ref pointing to a branch
require.NoDirExists(t, libInstallDir.String())
_, _, err = cli.Run("lib", "install", "--git-url", "https://github.com/arduino-libraries/ArduinoCloud.git#revert-2-typos", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
require.DirExists(t, libInstallDir.String())

// Verify that the correct branch is checked out
// https://github.com/arduino-libraries/ArduinoCloud/commit/d098d4647967b3aeb4520e7baf279e4225254dd2
fileToTest, err := libInstallDir.Join("src", "ArduinoCloudThingBase.h").ReadFile()
require.NoError(t, err)
require.Contains(t, string(fileToTest), `#define LENGHT_M "meters"`) // nolint:misspell
})
}

func TestUpdateIndex(t *testing.T) {
Expand Down

0 comments on commit bb88dc2

Please sign in to comment.