|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-git/go-git/v5" |
| 9 | + "github.com/go-git/go-git/v5/plumbing" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +// TODO: Switch to local repo so tests don't go over the network |
| 14 | +const ( |
| 15 | + testRepo = "https://github.com/Stratus3D/asdf-lua" |
| 16 | + testPluginName = "lua" |
| 17 | +) |
| 18 | + |
| 19 | +func TestPluginClone(t *testing.T) { |
| 20 | + t.Run("when plugin name is valid but URL is invalid prints an error", func(t *testing.T) { |
| 21 | + tempDir := t.TempDir() |
| 22 | + directory := filepath.Join(tempDir, testPluginName) |
| 23 | + |
| 24 | + plugin := NewPlugin(directory) |
| 25 | + err := plugin.Clone("foobar") |
| 26 | + |
| 27 | + assert.ErrorContains(t, err, "unable to clone plugin: repository not found") |
| 28 | + }) |
| 29 | + |
| 30 | + t.Run("clones provided Git URL to plugin directory when URL is valid", func(t *testing.T) { |
| 31 | + tempDir := t.TempDir() |
| 32 | + directory := filepath.Join(tempDir, testPluginName) |
| 33 | + |
| 34 | + plugin := NewPlugin(directory) |
| 35 | + err := plugin.Clone(testRepo) |
| 36 | + |
| 37 | + assert.Nil(t, err) |
| 38 | + |
| 39 | + // Assert plugin directory contains Git repo with bin directory |
| 40 | + _, err = os.ReadDir(directory + "/.git") |
| 41 | + assert.Nil(t, err) |
| 42 | + |
| 43 | + entries, err := os.ReadDir(directory + "/bin") |
| 44 | + assert.Nil(t, err) |
| 45 | + assert.Equal(t, 5, len(entries)) |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func TestPluginHead(t *testing.T) { |
| 50 | + tempDir := t.TempDir() |
| 51 | + directory := filepath.Join(tempDir, testPluginName) |
| 52 | + |
| 53 | + plugin := NewPlugin(directory) |
| 54 | + |
| 55 | + err := plugin.Clone(testRepo) |
| 56 | + assert.Nil(t, err) |
| 57 | + |
| 58 | + head, err := plugin.Head() |
| 59 | + assert.Nil(t, err) |
| 60 | + assert.NotZero(t, head) |
| 61 | +} |
| 62 | + |
| 63 | +func TestPluginRemoteURL(t *testing.T) { |
| 64 | + tempDir := t.TempDir() |
| 65 | + directory := filepath.Join(tempDir, testPluginName) |
| 66 | + |
| 67 | + plugin := NewPlugin(directory) |
| 68 | + |
| 69 | + err := plugin.Clone(testRepo) |
| 70 | + assert.Nil(t, err) |
| 71 | + |
| 72 | + url, err := plugin.RemoteURL() |
| 73 | + assert.Nil(t, err) |
| 74 | + assert.NotZero(t, url) |
| 75 | +} |
| 76 | + |
| 77 | +func TestPluginUpdate(t *testing.T) { |
| 78 | + tempDir := t.TempDir() |
| 79 | + directory := filepath.Join(tempDir, testPluginName) |
| 80 | + |
| 81 | + plugin := NewPlugin(directory) |
| 82 | + |
| 83 | + err := plugin.Clone(testRepo) |
| 84 | + assert.Nil(t, err) |
| 85 | + |
| 86 | + t.Run("returns error when plugin with name does not exist", func(t *testing.T) { |
| 87 | + nonexistantPath := filepath.Join(tempDir, "nonexistant") |
| 88 | + nonexistantPlugin := NewPlugin(nonexistantPath) |
| 89 | + updatedToRef, err := nonexistantPlugin.Update("") |
| 90 | + |
| 91 | + assert.NotNil(t, err) |
| 92 | + assert.Equal(t, updatedToRef, "") |
| 93 | + expectedErrMsg := "unable to open plugin Git repository: repository does not exist" |
| 94 | + assert.ErrorContains(t, err, expectedErrMsg) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("returns error when plugin repo does not exist", func(t *testing.T) { |
| 98 | + badPluginName := "badplugin" |
| 99 | + badPluginDir := filepath.Join(tempDir, badPluginName) |
| 100 | + err := os.MkdirAll(badPluginDir, 0777) |
| 101 | + assert.Nil(t, err) |
| 102 | + |
| 103 | + badPlugin := NewPlugin(badPluginDir) |
| 104 | + |
| 105 | + updatedToRef, err := badPlugin.Update("") |
| 106 | + |
| 107 | + assert.NotNil(t, err) |
| 108 | + assert.Equal(t, updatedToRef, "") |
| 109 | + expectedErrMsg := "unable to open plugin Git repository: repository does not exist" |
| 110 | + assert.ErrorContains(t, err, expectedErrMsg) |
| 111 | + }) |
| 112 | + |
| 113 | + t.Run("does not return error when plugin is already updated", func(t *testing.T) { |
| 114 | + // update plugin twice to test already updated case |
| 115 | + updatedToRef, err := plugin.Update("") |
| 116 | + assert.Nil(t, err) |
| 117 | + updatedToRef2, err := plugin.Update("") |
| 118 | + assert.Nil(t, err) |
| 119 | + assert.Equal(t, updatedToRef, updatedToRef2) |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("updates plugin when plugin when plugin exists", func(t *testing.T) { |
| 123 | + latestHash, err := getCurrentCommit(directory) |
| 124 | + assert.Nil(t, err) |
| 125 | + |
| 126 | + _, err = checkoutPreviousCommit(directory) |
| 127 | + assert.Nil(t, err) |
| 128 | + |
| 129 | + updatedToRef, err := plugin.Update("") |
| 130 | + assert.Nil(t, err) |
| 131 | + assert.Equal(t, latestHash, updatedToRef) |
| 132 | + |
| 133 | + currentHash, err := getCurrentCommit(directory) |
| 134 | + assert.Nil(t, err) |
| 135 | + assert.Equal(t, latestHash, currentHash) |
| 136 | + }) |
| 137 | + |
| 138 | + t.Run("Returns error when specified ref does not exist", func(t *testing.T) { |
| 139 | + ref := "non-existant" |
| 140 | + updatedToRef, err := plugin.Update(ref) |
| 141 | + assert.Equal(t, updatedToRef, "") |
| 142 | + expectedErrMsg := "couldn't find remote ref \"non-existant\"" |
| 143 | + assert.ErrorContains(t, err, expectedErrMsg) |
| 144 | + |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("updates plugin to ref when plugin with name and ref exist", func(t *testing.T) { |
| 148 | + ref := "master" |
| 149 | + |
| 150 | + hash, err := getCommit(directory, ref) |
| 151 | + assert.Nil(t, err) |
| 152 | + |
| 153 | + updatedToRef, err := plugin.Update(ref) |
| 154 | + assert.Nil(t, err) |
| 155 | + assert.Equal(t, hash, updatedToRef) |
| 156 | + |
| 157 | + // Check that plugin was updated to ref |
| 158 | + latestHash, err := getCurrentCommit(directory) |
| 159 | + assert.Nil(t, err) |
| 160 | + assert.Equal(t, hash, latestHash) |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +func getCurrentCommit(path string) (string, error) { |
| 165 | + return getCommit(path, "HEAD") |
| 166 | +} |
| 167 | + |
| 168 | +func getCommit(path, revision string) (string, error) { |
| 169 | + repo, err := git.PlainOpen(path) |
| 170 | + |
| 171 | + if err != nil { |
| 172 | + return "", err |
| 173 | + } |
| 174 | + |
| 175 | + hash, err := repo.ResolveRevision(plumbing.Revision(revision)) |
| 176 | + |
| 177 | + return hash.String(), err |
| 178 | +} |
| 179 | + |
| 180 | +func checkoutPreviousCommit(path string) (string, error) { |
| 181 | + repo, err := git.PlainOpen(path) |
| 182 | + |
| 183 | + if err != nil { |
| 184 | + return "", err |
| 185 | + } |
| 186 | + |
| 187 | + previousHash, err := repo.ResolveRevision(plumbing.Revision("HEAD~")) |
| 188 | + |
| 189 | + if err != nil { |
| 190 | + return "", err |
| 191 | + } |
| 192 | + |
| 193 | + worktree, err := repo.Worktree() |
| 194 | + |
| 195 | + if err != nil { |
| 196 | + return "", err |
| 197 | + } |
| 198 | + |
| 199 | + err = worktree.Reset(&git.ResetOptions{Commit: *previousHash}) |
| 200 | + |
| 201 | + if err != nil { |
| 202 | + return "", err |
| 203 | + } |
| 204 | + |
| 205 | + return previousHash.String(), nil |
| 206 | +} |
0 commit comments