From c0963a38a62e61586ef0dd34ea568d23345e3739 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 22 Aug 2024 09:16:28 -0400 Subject: [PATCH] feat(golang-rewrite): more work to get `install_command.bats` test passing * Correct `logger.Printf` call * Get `plugin_remove_command.bats` tests passing * Enable plugin_remove_command BATS tests * Create `versions.NoVersionSetError` struct * Set `ASDF_CONCURRENCY` for install callbacks --- cmd/cmd.go | 5 ++-- config/config.go | 12 +++++++++ internal/versions/versions.go | 39 ++++++++++++++++++++++++++---- internal/versions/versions_test.go | 2 +- main_test.go | 6 ++--- plugins/plugins.go | 2 +- plugins/plugins_test.go | 2 +- test/plugin_remove_command.bats | 4 +-- 8 files changed, 57 insertions(+), 15 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 74eaa74b..6306dc90 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -184,7 +184,8 @@ func pluginRemoveCommand(_ *cli.Context, logger *log.Logger, pluginName string) err = plugins.Remove(conf, pluginName) if err != nil { - logger.Printf("error removing plugin: %s", err) + // Needed to match output of old version + logger.Printf("%s", err) } return err } @@ -348,7 +349,7 @@ func latestCommand(logger *log.Logger, all bool, toolName, pattern string) (err plugins, err := plugins.List(conf, false, false) if err != nil { - logger.Printf("error loading plugin list: %s", err, false) + logger.Printf("error loading plugin list: %s", err) return err } diff --git a/config/config.go b/config/config.go index c4694ff7..b1545861 100644 --- a/config/config.go +++ b/config/config.go @@ -53,6 +53,7 @@ type Settings struct { AlwaysKeepDownload bool PluginRepositoryLastCheckDuration PluginRepoCheckDuration DisablePluginShortNameRepository bool + Concurrency string } func defaultConfig(dataDir, configFile string) *Config { @@ -154,6 +155,16 @@ func (c *Config) DisablePluginShortNameRepository() (bool, error) { return c.Settings.DisablePluginShortNameRepository, nil } +// Concurrency returns concurrency setting from asdfrc file +func (c *Config) Concurrency() (string, error) { + err := c.loadSettings() + if err != nil { + return "", err + } + + return c.Settings.Concurrency, nil +} + // GetHook returns a hook command from config if it is there func (c *Config) GetHook(hook string) (string, error) { err := c.loadSettings() @@ -227,6 +238,7 @@ func loadSettings(asdfrcPath string) (Settings, error) { boolOverride(&settings.LegacyVersionFile, mainConf, "legacy_version_file") boolOverride(&settings.AlwaysKeepDownload, mainConf, "always_keep_download") boolOverride(&settings.DisablePluginShortNameRepository, mainConf, "disable_plugin_short_name_repository") + settings.Concurrency = strings.ToLower(mainConf.Key("concurrency").String()) return *settings, nil } diff --git a/internal/versions/versions.go b/internal/versions/versions.go index 09f5043a..2237d719 100644 --- a/internal/versions/versions.go +++ b/internal/versions/versions.go @@ -27,14 +27,27 @@ const ( noLatestVersionErrMsg = "no latest version found" ) -// UninstallableVersion is an error returned if someone tries to install the +// UninstallableVersionError is an error returned if someone tries to install the // system version. -type UninstallableVersion struct{} +type UninstallableVersionError struct{} -func (e UninstallableVersion) Error() string { +func (e UninstallableVersionError) Error() string { return fmt.Sprint(uninstallableVersionMsg) } +// NoVersionSetError is returned whenever an operation that requires a version +// is not able to resolve one. +type NoVersionSetError struct { + toolName string +} + +func (e NoVersionSetError) Error() string { + // Eventually switch this to a more friendly error message, BATS tests fail + // with this improvement + // return fmt.Sprintf("no version set for plugin %s", e.toolName) + return "no version set" +} + // InstallAll installs all specified versions of every tool for the current // directory. Typically this will just be a single version, if not already // installed, but it may be multiple versions if multiple versions for the tool @@ -74,7 +87,7 @@ func Install(conf config.Config, plugin plugins.Plugin, dir string, stdOut io.Wr } if !found || len(versions.Versions) == 0 { - return errors.New("no version set") + return NoVersionSetError{toolName: plugin.Name} } for _, version := range versions.Versions { @@ -114,7 +127,7 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, version string } if version == systemVersion { - return UninstallableVersion{} + return UninstallableVersionError{} } downloadDir := downloadPath(conf, plugin, version) @@ -130,6 +143,7 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, version string "ASDF_INSTALL_VERSION": version, "ASDF_INSTALL_PATH": installDir, "ASDF_DOWNLOAD_PATH": downloadDir, + "ASDF_CONCURRENCY": asdfConcurrency(conf), } err = os.MkdirAll(downloadDir, 0o777) @@ -169,6 +183,21 @@ func InstallOneVersion(conf config.Config, plugin plugins.Plugin, version string return nil } +func asdfConcurrency(conf config.Config) string { + val, ok := os.LookupEnv("ASDF_CONCURRENCY") + + if !ok { + val, err := conf.Concurrency() + if err != nil { + return "1" + } + + return val + } + + return val +} + // Installed checks if a specific version of a tool is installed func Installed(conf config.Config, plugin plugins.Plugin, version string) bool { installDir := installPath(conf, plugin, version) diff --git a/internal/versions/versions_test.go b/internal/versions/versions_test.go index 564dcd73..ce2eeb76 100644 --- a/internal/versions/versions_test.go +++ b/internal/versions/versions_test.go @@ -168,7 +168,7 @@ func TestInstallOneVersion(t *testing.T) { conf, plugin := generateConfig(t) stdout, stderr := buildOutputs() err := InstallOneVersion(conf, plugin, "system", &stdout, &stderr) - assert.IsType(t, UninstallableVersion{}, err) + assert.IsType(t, UninstallableVersionError{}, err) }) t.Run("returns error when version doesn't exist", func(t *testing.T) { diff --git a/main_test.go b/main_test.go index 0a93c8c3..916d89d1 100644 --- a/main_test.go +++ b/main_test.go @@ -55,9 +55,9 @@ func TestBatsTests(t *testing.T) { // runBatsFile(t, dir, "plugin_list_all_command.bats") //}) - //t.Run("plugin_remove_command", func(t *testing.T) { - // runBatsFile(t, dir, "plugin_remove_command.bats") - //}) + t.Run("plugin_remove_command", func(t *testing.T) { + runBatsFile(t, dir, "plugin_remove_command.bats") + }) //t.Run("plugin_test_command", func(t *testing.T) { // runBatsFile(t, dir, "plugin_test_command.bats") diff --git a/plugins/plugins.go b/plugins/plugins.go index c13e46dd..19c54acd 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -305,7 +305,7 @@ func Remove(config config.Config, pluginName string) error { } if !exists { - return fmt.Errorf("no such plugin: %s", pluginName) + return fmt.Errorf("No such plugin: %s", pluginName) } pluginDir := PluginDirectory(config.DataDir, pluginName) diff --git a/plugins/plugins_test.go b/plugins/plugins_test.go index 29fd0763..abfbfc71 100644 --- a/plugins/plugins_test.go +++ b/plugins/plugins_test.go @@ -182,7 +182,7 @@ func TestRemove(t *testing.T) { t.Run("returns error when plugin with name does not exist", func(t *testing.T) { err := Remove(conf, "nonexistant") assert.NotNil(t, err) - assert.ErrorContains(t, err, "no such plugin") + assert.ErrorContains(t, err, "No such plugin") }) t.Run("returns error when invalid plugin name is given", func(t *testing.T) { diff --git a/test/plugin_remove_command.bats b/test/plugin_remove_command.bats index 911ec374..b688ac66 100644 --- a/test/plugin_remove_command.bats +++ b/test/plugin_remove_command.bats @@ -16,13 +16,13 @@ teardown() { [ "$status" -eq 0 ] [ -d "$ASDF_DIR/downloads/dummy" ] - run asdf plugin-remove "dummy" + run asdf plugin remove "dummy" [ "$status" -eq 0 ] [ ! -d "$ASDF_DIR/downloads/dummy" ] } @test "plugin_remove command fails if the plugin doesn't exist" { - run asdf plugin-remove "does-not-exist" + run asdf plugin remove "does-not-exist" [ "$status" -eq 1 ] echo "$output" | grep "No such plugin: does-not-exist" }