Skip to content

fix: install should report missing plugins #1964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions internal/versions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"path"
"regexp"
"strings"

Expand All @@ -26,6 +27,7 @@ const (
uninstallableVersionMsg = "uninstallable version: %s"
latestFilterRegex = "(?i)(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)"
noLatestVersionErrMsg = "no latest version found"
missingPluginErrMsg = "missing plugin for %s"
)

// UninstallableVersionError is an error returned if someone tries to install the
Expand All @@ -44,6 +46,16 @@ type NoVersionSetError struct {
toolName string
}

// MissingPluginError is returned whenever an operation expects a plugin,
// but it is not installed.
type MissingPluginError struct {
toolName string
}

func (e MissingPluginError) Error() string {
return fmt.Sprintf(missingPluginErrMsg, e.toolName)
}

func (e NoVersionSetError) Error() string {
// Eventually switch this to a more friendly error message, BATS tests fail
// with this improvement
Expand All @@ -61,16 +73,31 @@ func InstallAll(conf config.Config, dir string, stdOut io.Writer, stdErr io.Writ
return []error{fmt.Errorf("unable to list plugins: %w", err)}
}

toolNames := map[string]bool{}
filepath := path.Join(dir, conf.DefaultToolVersionsFilename)
toolVersions, err := toolversions.GetAllToolsAndVersions(filepath)
if err == nil {
for _, version := range toolVersions {
toolNames[version.Name] = true
}
}

// Ideally we should install these in the order they are specified in the
// closest .tool-versions file, but for now that is too complicated to
// implement.
for _, plugin := range plugins {
delete(toolNames, plugin.Name)
err := Install(conf, plugin, dir, stdOut, stdErr)
if err != nil {
failures = append(failures, err)
}
}

for toolName := range toolNames {
err = MissingPluginError{toolName: toolName}
failures = append(failures, err)
}

return failures
}

Expand Down
18 changes: 18 additions & 0 deletions internal/versions/versions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestInstallAll(t *testing.T) {
writeVersionFile(t, currentDir, content)

err := InstallAll(conf, currentDir, &stdout, &stderr)
assert.Len(t, err, 1, "expected 1 install error")
assert.ErrorContains(t, err[0], "no version set")

assertVersionInstalled(t, conf.DataDir, plugin.Name, version)
Expand All @@ -70,6 +71,23 @@ func TestInstallAll(t *testing.T) {
assertNotInstalled(t, conf.DataDir, secondPlugin.Name, version)
assertVersionInstalled(t, conf.DataDir, plugin.Name, version)
})

t.Run("reports skipped tools due to missing plugin", func(t *testing.T) {
conf, plugin := generateConfig(t)
stdout, stderr := buildOutputs()
currentDir := t.TempDir()
version := "1.0.0"

// write a version file
content := fmt.Sprintf("%s %s\n%s %s", plugin.Name, version, "non-existant-tool", version)
writeVersionFile(t, currentDir, content)

err := InstallAll(conf, currentDir, &stdout, &stderr)
assert.Len(t, err, 1, "expected 1 install error")
assert.ErrorContains(t, err[0], "missing plugin for")

assertVersionInstalled(t, conf.DataDir, plugin.Name, version)
})
}

func TestInstall(t *testing.T) {
Expand Down