Skip to content

Commit

Permalink
feat(golang-rewrite): write test for PluginAdd happy path
Browse files Browse the repository at this point in the history
  • Loading branch information
Stratus3D committed Feb 28, 2024
1 parent 6b68d7b commit 7775663
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
1 change: 0 additions & 1 deletion plugins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func PluginAdd(config config.Config, pluginName, pluginUrl string) error {
return fmt.Errorf("Unable to clone plugin: %w", err)

Check failure on line 46 in plugins/main.go

View workflow job for this annotation

GitHub Actions / asdf-golang

error strings should not be capitalized (ST1005)
}

// TODO: Implement this function
return nil
}

Expand Down
36 changes: 29 additions & 7 deletions plugins/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/stretchr/testify/assert"
)

// TODO: Switch to local repo so tests don't go over the network
const (
testRepo = "https://github.com/Stratus3D/asdf-lua"
testPluginName = "lua"
)

func TestPluginAdd(t *testing.T) {
testDataDir := t.TempDir()

Expand All @@ -17,7 +23,7 @@ func TestPluginAdd(t *testing.T) {

for _, invalid := range invalids {
t.Run(invalid, func(t *testing.T) {
err := PluginAdd(config.Config{}, invalid, "https://github.com/Stratus3D/asdf-lua")
err := PluginAdd(config.Config{}, invalid, testRepo)

expectedErrMsg := "is invalid. Name may only contain lowercase letters, numbers, '_', and '-'"
if !strings.Contains(err.Error(), expectedErrMsg) {
Expand All @@ -31,14 +37,14 @@ func TestPluginAdd(t *testing.T) {
conf := config.Config{DataDir: testDataDir}

// Add plugin
err := PluginAdd(conf, "lua", "https://github.com/Stratus3D/asdf-lua")
err := PluginAdd(conf, testPluginName, testRepo)

if err != nil {
t.Fatal("Expected to be able to add plugin")
}

// Add it again to trigger error
err = PluginAdd(conf, "lua", "https://github.com/Stratus3D/asdf-lua")
err = PluginAdd(conf, testPluginName, testRepo)

if err == nil {
t.Fatal("expected error got nil")
Expand All @@ -59,19 +65,35 @@ func TestPluginAdd(t *testing.T) {
})

t.Run("when plugin name and URL are valid installs plugin", func(t *testing.T) {
testDataDir := t.TempDir()
conf := config.Config{DataDir: testDataDir}

err := PluginAdd(conf, testPluginName, testRepo)

assert.Nil(t, err, "Expected to be able to add plugin")

// Assert plugin directory contains Git repo with bin directory
pluginDir := PluginDirectory(testDataDir, testPluginName)

_, err = os.ReadDir(pluginDir + "/.git")
assert.Nil(t, err)

entries, err := os.ReadDir(pluginDir + "/bin")
assert.Nil(t, err)
assert.Equal(t, 5, len(entries))
})
}

func TestPluginExists(t *testing.T) {
testDataDir := t.TempDir()
pluginDir := PluginDirectory(testDataDir, "lua")
pluginDir := PluginDirectory(testDataDir, testPluginName)
err := os.MkdirAll(pluginDir, 0777)
if err != nil {
t.Errorf("got %v, expected nil", err)
}

t.Run("returns true when plugin exists", func(t *testing.T) {
exists, err := PluginExists(testDataDir, "lua")
exists, err := PluginExists(testDataDir, testPluginName)

if err != nil {
t.Errorf("got %v, expected nil", err)
Expand Down Expand Up @@ -116,7 +138,7 @@ func TestPluginExists(t *testing.T) {

func TestPluginDirectory(t *testing.T) {
t.Run("returns new path with plugin name as last segment", func(t *testing.T) {
pluginDir := PluginDirectory("~/.asdf/", "lua")
pluginDir := PluginDirectory("~/.asdf/", testPluginName)
expected := "~/.asdf/plugins/lua"
if pluginDir != expected {
t.Errorf("got %v, expected %v", pluginDir, expected)
Expand All @@ -126,7 +148,7 @@ func TestPluginDirectory(t *testing.T) {

func TestValidatePluginName(t *testing.T) {
t.Run("returns no error when plugin name is valid", func(t *testing.T) {
err := validatePluginName("lua")
err := validatePluginName(testPluginName)
refuteError(t, err)
})

Expand Down

0 comments on commit 7775663

Please sign in to comment.