Skip to content

Don't error maintenance commands on missing library clone folder #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions internal/command/modify/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,9 @@ func modifyRepositoryURL(newRepositoryURL string) error {
fmt.Printf("Changing URL of library %s from %s to %s\n", libraryName, oldRepositoryURL, newRepositoryURL)

// Remove the library Git clone folder. It will be cloned from the new URL on the next sync.
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
gitCloneSubfolder, err := libraryRegistration.AsFolder()
if err != nil {
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
return err
}
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
if err := backup.Backup(gitClonePath); err != nil {
return fmt.Errorf("While backing up library's Git clone: %w", err)
}
if err := gitClonePath.RemoveAll(); err != nil {
return fmt.Errorf("While removing library's Git clone: %w", err)
}

// Update the library repository URL in the database.
libraryData.Repository = newRepositoryURL
Expand Down
11 changes: 1 addition & 10 deletions internal/command/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,9 @@ func removeLibrary(libraryName string) error {
}

// Remove the library Git clone folder.
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
gitCloneSubfolder, err := libraryRegistration.AsFolder()
if err != nil {
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
return err
}
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
if err := backup.Backup(gitClonePath); err != nil {
return fmt.Errorf("While backing up library's Git clone: %w", err)
}
if err := gitClonePath.RemoveAll(); err != nil {
return fmt.Errorf("While removing library Git clone: %s", err)
}

return nil
}
Expand Down
31 changes: 31 additions & 0 deletions internal/libraries/repoclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"os"
"path/filepath"

"github.com/arduino/go-paths-helper"
"github.com/arduino/libraries-repository-engine/internal/backup"
"github.com/arduino/libraries-repository-engine/internal/configuration"
"github.com/arduino/libraries-repository-engine/internal/feedback"
"github.com/arduino/libraries-repository-engine/internal/libraries/db"

"fmt"
Expand Down Expand Up @@ -134,3 +138,30 @@ func UpdateLibrary(release *db.Release, repoURL string, libraryDb *db.DB) error

return nil
}

// BackupAndDeleteGitClone backs up and then deletes the library's Git clone folder.
func BackupAndDeleteGitClone(config *configuration.Config, repoMeta *Repo) error {
gitCloneSubfolder, err := repoMeta.AsFolder()
if err != nil {
return err
}
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
// The library's clone folder may be removed by the sync process under expected circumstances.
// So its absence does not necessarily imply a problem.
gitClonePathExists, err := gitClonePath.ExistCheck()
if err != nil {
return err
}
if gitClonePathExists {
if err := backup.Backup(gitClonePath); err != nil {
return fmt.Errorf("While backing up library's Git clone: %w", err)
}
if err := gitClonePath.RemoveAll(); err != nil {
return fmt.Errorf("While removing library Git clone: %s", err)
}
} else {
feedback.Warningf("Library Git clone folder %s not present", gitClonePath)
}

return nil
}
39 changes: 39 additions & 0 deletions internal/libraries/repoclone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
"path/filepath"
"testing"

"github.com/arduino/go-paths-helper"
"github.com/arduino/libraries-repository-engine/internal/backup"
"github.com/arduino/libraries-repository-engine/internal/configuration"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -48,3 +52,38 @@ func TestCloneRepos(t *testing.T) {
_, err = os.Stat(repo.FolderPath)
require.NoError(t, err)
}

func TestBackupAndDeleteGitClone(t *testing.T) {
var err error

gitClonesFolder, err := paths.MkTempDir("", "libraries-test-testbackupanddeletegitclone")
require.NoError(t, err)
config := configuration.Config{
GitClonesFolder: gitClonesFolder.String(),
}
repoMeta := Repo{
URL: "https://github.com/Foo/Bar.git",
}

assert.Nil(t, BackupAndDeleteGitClone(&config, &repoMeta), "Return nil if library clone folder did not exist.")

gitCloneSubfolder, err := repoMeta.AsFolder()
require.NoError(t, err)
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
err = gitClonePath.MkdirAll()
require.NoError(t, err)

assert.Nil(t, BackupAndDeleteGitClone(&config, &repoMeta), "Return nil if library clone folder did exist.")

exist, err := gitClonePath.ExistCheck()
require.NoError(t, err)

assert.False(t, exist, "Library clone folder was deleted.")

err = backup.Restore()
require.NoError(t, err)
exist, err = gitClonePath.ExistCheck()
require.NoError(t, err)

assert.True(t, exist, "Library clone folder was backed up.")
}