2024-04-27 12:54:30 -07:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2024-07-27 14:20:59 -07:00
|
|
|
"asdf/repotest"
|
|
|
|
|
2024-04-27 12:54:30 -07:00
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
"github.com/go-git/go-git/v5/plumbing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPluginClone(t *testing.T) {
|
|
|
|
t.Run("when plugin name is valid but URL is invalid prints an error", func(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
plugin := NewRepo(t.TempDir())
|
2024-04-27 12:54:30 -07:00
|
|
|
err := plugin.Clone("foobar")
|
|
|
|
|
|
|
|
assert.ErrorContains(t, err, "unable to clone plugin: repository not found")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("clones provided Git URL to plugin directory when URL is valid", func(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
repoDir := generateRepo(t)
|
|
|
|
directory := t.TempDir()
|
2024-06-01 18:24:56 -07:00
|
|
|
plugin := NewRepo(directory)
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-07-27 14:20:59 -07:00
|
|
|
err := plugin.Clone(repoDir)
|
2024-04-27 12:54:30 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
// Assert plugin directory contains Git repo with bin directory
|
|
|
|
_, err = os.ReadDir(directory + "/.git")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
entries, err := os.ReadDir(directory + "/bin")
|
|
|
|
assert.Nil(t, err)
|
2024-07-27 14:20:59 -07:00
|
|
|
assert.Equal(t, 12, len(entries))
|
2024-04-27 12:54:30 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginHead(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
repoDir := generateRepo(t)
|
|
|
|
directory := t.TempDir()
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-06-01 18:24:56 -07:00
|
|
|
plugin := NewRepo(directory)
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-07-27 14:20:59 -07:00
|
|
|
err := plugin.Clone(repoDir)
|
2024-04-27 12:54:30 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
head, err := plugin.Head()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, head)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginRemoteURL(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
repoDir := generateRepo(t)
|
|
|
|
directory := t.TempDir()
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-06-01 18:24:56 -07:00
|
|
|
plugin := NewRepo(directory)
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-07-27 14:20:59 -07:00
|
|
|
err := plugin.Clone(repoDir)
|
2024-04-27 12:54:30 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
url, err := plugin.RemoteURL()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, url)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginUpdate(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
repoDir := generateRepo(t)
|
|
|
|
directory := t.TempDir()
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-06-01 18:24:56 -07:00
|
|
|
plugin := NewRepo(directory)
|
2024-04-27 12:54:30 -07:00
|
|
|
|
2024-07-27 14:20:59 -07:00
|
|
|
err := plugin.Clone(repoDir)
|
2024-04-27 12:54:30 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
t.Run("returns error when plugin with name does not exist", func(t *testing.T) {
|
2024-07-27 14:20:59 -07:00
|
|
|
nonexistantPath := filepath.Join(directory, "nonexistant")
|
2024-06-01 18:24:56 -07:00
|
|
|
nonexistantPlugin := NewRepo(nonexistantPath)
|
2024-04-27 12:54:30 -07:00
|
|
|
updatedToRef, err := nonexistantPlugin.Update("")
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, updatedToRef, "")
|
|
|
|
expectedErrMsg := "unable to open plugin Git repository: repository does not exist"
|
|
|
|
assert.ErrorContains(t, err, expectedErrMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("returns error when plugin repo does not exist", func(t *testing.T) {
|
|
|
|
badPluginName := "badplugin"
|
2024-07-27 14:20:59 -07:00
|
|
|
badPluginDir := filepath.Join(directory, badPluginName)
|
2024-04-30 07:13:05 -07:00
|
|
|
err := os.MkdirAll(badPluginDir, 0o777)
|
2024-04-27 12:54:30 -07:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2024-06-01 18:24:56 -07:00
|
|
|
badPlugin := NewRepo(badPluginDir)
|
2024-04-27 12:54:30 -07:00
|
|
|
|
|
|
|
updatedToRef, err := badPlugin.Update("")
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Equal(t, updatedToRef, "")
|
|
|
|
expectedErrMsg := "unable to open plugin Git repository: repository does not exist"
|
|
|
|
assert.ErrorContains(t, err, expectedErrMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("does not return error when plugin is already updated", func(t *testing.T) {
|
|
|
|
// update plugin twice to test already updated case
|
|
|
|
updatedToRef, err := plugin.Update("")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
updatedToRef2, err := plugin.Update("")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, updatedToRef, updatedToRef2)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("updates plugin when plugin when plugin exists", func(t *testing.T) {
|
|
|
|
latestHash, err := getCurrentCommit(directory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
_, err = checkoutPreviousCommit(directory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
updatedToRef, err := plugin.Update("")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, latestHash, updatedToRef)
|
|
|
|
|
|
|
|
currentHash, err := getCurrentCommit(directory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, latestHash, currentHash)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Returns error when specified ref does not exist", func(t *testing.T) {
|
|
|
|
ref := "non-existant"
|
|
|
|
updatedToRef, err := plugin.Update(ref)
|
|
|
|
assert.Equal(t, updatedToRef, "")
|
|
|
|
expectedErrMsg := "couldn't find remote ref \"non-existant\""
|
|
|
|
assert.ErrorContains(t, err, expectedErrMsg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("updates plugin to ref when plugin with name and ref exist", func(t *testing.T) {
|
|
|
|
ref := "master"
|
|
|
|
|
|
|
|
hash, err := getCommit(directory, ref)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
updatedToRef, err := plugin.Update(ref)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, hash, updatedToRef)
|
|
|
|
|
|
|
|
// Check that plugin was updated to ref
|
|
|
|
latestHash, err := getCurrentCommit(directory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, hash, latestHash)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCurrentCommit(path string) (string, error) {
|
|
|
|
return getCommit(path, "HEAD")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCommit(path, revision string) (string, error) {
|
|
|
|
repo, err := git.PlainOpen(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err := repo.ResolveRevision(plumbing.Revision(revision))
|
|
|
|
|
|
|
|
return hash.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkoutPreviousCommit(path string) (string, error) {
|
|
|
|
repo, err := git.PlainOpen(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
previousHash, err := repo.ResolveRevision(plumbing.Revision("HEAD~"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
worktree, err := repo.Worktree()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = worktree.Reset(&git.ResetOptions{Commit: *previousHash})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return previousHash.String(), nil
|
|
|
|
}
|
2024-07-27 14:20:59 -07:00
|
|
|
|
|
|
|
func generateRepo(t *testing.T) string {
|
|
|
|
t.Helper()
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
path, err := repotest.GeneratePlugin("dummy_plugin", tempDir, "lua")
|
|
|
|
|
|
|
|
assert.Nil(t, err)
|
|
|
|
return path
|
|
|
|
}
|