Skip to content

Commit 01a027b

Browse files
committed
Functionalize library clone folder deletion code
Both the `libraries-repository-engine remove` and `libraries-repository-engine modify --repo-url` commands delete the library's "Git clone folder". Previously, the code for doing this was repeated verbatim for each command. At the time the code was written, this was considered reasonable since it was not a lot of code and the architectural structure of the module does not provide an obvious location for a shared function containing that code. However, it is now planned to add additional code, which tips the balance toward a function. This also enables increasing the unit test coverage of the code.
1 parent 4fd9cf7 commit 01a027b

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

internal/command/modify/modify.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,9 @@ func modifyRepositoryURL(newRepositoryURL string) error {
164164
fmt.Printf("Changing URL of library %s from %s to %s\n", libraryName, oldRepositoryURL, newRepositoryURL)
165165

166166
// Remove the library Git clone folder. It will be cloned from the new URL on the next sync.
167-
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
168-
gitCloneSubfolder, err := libraryRegistration.AsFolder()
169-
if err != nil {
167+
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
170168
return err
171169
}
172-
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
173-
if err := backup.Backup(gitClonePath); err != nil {
174-
return fmt.Errorf("While backing up library's Git clone: %w", err)
175-
}
176-
if err := gitClonePath.RemoveAll(); err != nil {
177-
return fmt.Errorf("While removing library's Git clone: %w", err)
178-
}
179170

180171
// Update the library repository URL in the database.
181172
libraryData.Repository = newRepositoryURL

internal/command/remove/remove.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,9 @@ func removeLibrary(libraryName string) error {
160160
}
161161

162162
// Remove the library Git clone folder.
163-
libraryRegistration := libraries.Repo{URL: libraryData.Repository}
164-
gitCloneSubfolder, err := libraryRegistration.AsFolder()
165-
if err != nil {
163+
if err := libraries.BackupAndDeleteGitClone(config, &libraries.Repo{URL: libraryData.Repository}); err != nil {
166164
return err
167165
}
168-
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
169-
if err := backup.Backup(gitClonePath); err != nil {
170-
return fmt.Errorf("While backing up library's Git clone: %w", err)
171-
}
172-
if err := gitClonePath.RemoveAll(); err != nil {
173-
return fmt.Errorf("While removing library Git clone: %s", err)
174-
}
175166

176167
return nil
177168
}

internal/libraries/repoclone.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
"os"
2929
"path/filepath"
3030

31+
"github.com/arduino/go-paths-helper"
32+
"github.com/arduino/libraries-repository-engine/internal/backup"
33+
"github.com/arduino/libraries-repository-engine/internal/configuration"
3134
"github.com/arduino/libraries-repository-engine/internal/libraries/db"
3235

3336
"fmt"
@@ -134,3 +137,21 @@ func UpdateLibrary(release *db.Release, repoURL string, libraryDb *db.DB) error
134137

135138
return nil
136139
}
140+
141+
// BackupAndDeleteGitClone backs up and then deletes the library's Git clone folder.
142+
func BackupAndDeleteGitClone(config *configuration.Config, repoMeta *Repo) error {
143+
gitCloneSubfolder, err := repoMeta.AsFolder()
144+
if err != nil {
145+
return err
146+
}
147+
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
148+
149+
if err := backup.Backup(gitClonePath); err != nil {
150+
return fmt.Errorf("While backing up library's Git clone: %w", err)
151+
}
152+
if err := gitClonePath.RemoveAll(); err != nil {
153+
return fmt.Errorf("While removing library Git clone: %s", err)
154+
}
155+
156+
return nil
157+
}

internal/libraries/repoclone_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
"path/filepath"
2929
"testing"
3030

31+
"github.com/arduino/go-paths-helper"
32+
"github.com/arduino/libraries-repository-engine/internal/backup"
33+
"github.com/arduino/libraries-repository-engine/internal/configuration"
34+
"github.com/stretchr/testify/assert"
3135
"github.com/stretchr/testify/require"
3236
)
3337

@@ -48,3 +52,38 @@ func TestCloneRepos(t *testing.T) {
4852
_, err = os.Stat(repo.FolderPath)
4953
require.NoError(t, err)
5054
}
55+
56+
func TestBackupAndDeleteGitClone(t *testing.T) {
57+
var err error
58+
59+
gitClonesFolder, err := paths.MkTempDir("", "libraries-test-testbackupanddeletegitclone")
60+
require.NoError(t, err)
61+
config := configuration.Config{
62+
GitClonesFolder: gitClonesFolder.String(),
63+
}
64+
repoMeta := Repo{
65+
URL: "https://github.com/Foo/Bar.git",
66+
}
67+
68+
assert.Error(t, BackupAndDeleteGitClone(&config, &repoMeta), "Error if library clone folder did not exist.")
69+
70+
gitCloneSubfolder, err := repoMeta.AsFolder()
71+
require.NoError(t, err)
72+
gitClonePath := paths.New(config.GitClonesFolder, gitCloneSubfolder)
73+
err = gitClonePath.MkdirAll()
74+
require.NoError(t, err)
75+
76+
assert.Nil(t, BackupAndDeleteGitClone(&config, &repoMeta), "Return nil if library clone folder did exist.")
77+
78+
exist, err := gitClonePath.ExistCheck()
79+
require.NoError(t, err)
80+
81+
assert.False(t, exist, "Library clone folder was deleted.")
82+
83+
err = backup.Restore()
84+
require.NoError(t, err)
85+
exist, err = gitClonePath.ExistCheck()
86+
require.NoError(t, err)
87+
88+
assert.True(t, exist, "Library clone folder was backed up.")
89+
}

0 commit comments

Comments
 (0)