diff --git a/plugins/main.go b/plugins/main.go index ea3ef771..b1e5b3c3 100644 --- a/plugins/main.go +++ b/plugins/main.go @@ -46,7 +46,6 @@ func PluginAdd(config config.Config, pluginName, pluginUrl string) error { return fmt.Errorf("Unable to clone plugin: %w", err) } - // TODO: Implement this function return nil } diff --git a/plugins/main_test.go b/plugins/main_test.go index c17e4a6e..54c8ba53 100644 --- a/plugins/main_test.go +++ b/plugins/main_test.go @@ -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() @@ -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) { @@ -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") @@ -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) @@ -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) @@ -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) })