Skip to content
Merged
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
11 changes: 7 additions & 4 deletions internal/cli/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func newLanguageCommand(language plugins.Language, deps Dependencies) *cobra.Com
}
info, err := plugins.Describe(ctx, deps.Runner, found.Path)
if err != nil {
if runtime := deps.Finder.MissingRuntime(language); runtime != "" {
return plugins.RuntimeMissingError{Language: language, Runtime: runtime}
}
return fmt.Errorf("the %s profiler at %s: %w", language.Display, found.Path, err)
}
if !info.Supports(coreSchemaVersion) {
Expand Down Expand Up @@ -104,21 +107,21 @@ func inspectLanguage(ctx context.Context, language plugins.Language, deps Depend
row := pluginRow{Language: language.ID}
found, err := deps.Finder.Find(language)
var missing plugins.NotInstalledError
var runtime plugins.RuntimeMissingError
switch {
case errors.As(err, &missing):
row.Status, row.Detail = "missing", "install with: "+language.InstallHint
return row
case errors.As(err, &runtime):
row.Status, row.Detail = "runtime-missing", err.Error()
return row
case err != nil:
row.Status, row.Detail = "unusable", err.Error()
return row
}
row.Origin, row.Path = found.Origin, found.Path
info, err := plugins.Describe(ctx, deps.Runner, found.Path)
if err != nil {
if runtime := deps.Finder.MissingRuntime(language); runtime != "" {
row.Status, row.Detail = "runtime-missing", plugins.RuntimeMissingError{Language: language, Runtime: runtime}.Error()
return row
}
row.Status, row.Detail = "unreadable", err.Error()
return row
}
Expand Down
31 changes: 26 additions & 5 deletions internal/cli/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,39 @@ func TestAddOnExitStatusIsTheExitStatusOfRcWithoutExtraMessages(t *testing.T) {
}
}

func TestMissingAddOnAndMissingRuntimeAreReportedOnceWithWhatToInstall(t *testing.T) {
func TestMissingAddOnIsReportedOnceWithWhatToInstall(t *testing.T) {
runner := &fakeRunner{version: readyVersion}
code, _, errOut := run(t, deps(runner, "node"), "profile", "typescript", "generate")
if code != 1 || strings.Count(errOut, "rc:") != 1 || !strings.Contains(errOut, "TypeScript profiler is not installed") ||
!strings.Contains(errOut, "npm install --global @runtimeconditions/typescript-profiler") || strings.Contains(errOut, "Usage:") {
t.Fatalf("code=%d stderr=%q", code, errOut)
}
code, _, errOut = run(t, deps(runner, "rc-profiler-typescript"), "profile", "typescript", "generate")
if code != 1 || !strings.Contains(errOut, "needs node") {
t.Fatalf("code=%d stderr=%q", code, errOut)
}
if len(runner.calls) != 0 {
t.Fatalf("nothing may run: %+v", runner.calls)
}
}

func TestAnAddOnThatCarriesItsOwnRuntimeRunsWithoutNodeOnPath(t *testing.T) {
runner := &fakeRunner{version: readyVersion, output: "profile: yaml\n"}
code, out, errOut := run(t, deps(runner, "rc-profiler-typescript"), "profile", "typescript", "generate")
if code != 0 || out != "profile: yaml\n" || errOut != "" || len(runner.calls) != 1 {
t.Fatalf("code=%d stdout=%q stderr=%q calls=%d", code, out, errOut, len(runner.calls))
}
}

func TestAnAddOnThatCannotRunIsBlamedOnNodeOnlyWhenNodeIsMissing(t *testing.T) {
runner := &fakeRunner{}
code, _, errOut := run(t, deps(runner, "rc-profiler-typescript"), "profile", "typescript", "generate")
if code != 1 || strings.Count(errOut, "rc:") != 1 || !strings.Contains(errOut, "needs node") || len(runner.calls) != 0 {
t.Fatalf("code=%d stderr=%q", code, errOut)
}
runner = &fakeRunner{}
code, _, errOut = run(t, deps(runner, "rc-profiler-typescript", "node"), "profile", "typescript", "generate")
if code != 1 || strings.Contains(errOut, "needs node") || !strings.Contains(errOut, "did not print the expected JSON") {
t.Fatalf("with node present the real error must show: code=%d stderr=%q", code, errOut)
}
}

func TestAnAddOnForAnotherCoreSchemaIsRejectedBeforeItRuns(t *testing.T) {
runner := &fakeRunner{version: `{"name":"x","version":"0.1.0","coreSchemas":["0.3.0"]}`}
code, _, errOut := run(t, deps(runner, "rc-profiler-typescript", "node"), "profile", "typescript", "generate")
Expand Down Expand Up @@ -137,6 +154,10 @@ func TestPluginsListReportsEveryStatus(t *testing.T) {
}
return document.Plugins
}
packaged := read(deps(&fakeRunner{version: readyVersion}, "rc-profiler-typescript"))
if packaged[1].Status != "ready" {
t.Fatalf("an add-on with its own runtime is ready without node: %+v", packaged[1])
}
ready := read(deps(&fakeRunner{version: readyVersion}, "rc-profiler-typescript", "node"))
if len(ready) != 2 || ready[0].Language != "go" || ready[0].Status != "built-in" ||
ready[1].Language != "typescript" || ready[1].Status != "ready" || ready[1].Version != "0.2.0" ||
Expand Down
19 changes: 13 additions & 6 deletions internal/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ type Language struct {
Display string
// Executables are looked up on PATH, in order.
Executables []string
// Runtime lists the programs the add-on needs on PATH, such as node.
// Runtime lists the programs an add-on installed from the language's own tooling needs on PATH,
// such as node. A system package that carries its own runtime does not need them.
Runtime []string
// OverrideEnv names an environment variable that holds an absolute executable path.
OverrideEnv string
Expand Down Expand Up @@ -70,7 +71,7 @@ func (e NotInstalledError) Error() string {
l.Display, strings.Join(l.Executables, " or "), l.InstallHint, l.OverrideEnv)
}

// RuntimeMissingError reports that the add-on is installed but a program it needs is not.
// RuntimeMissingError reports that the add-on could not run and a program it needs is not on PATH.
type RuntimeMissingError struct {
Language Language
Runtime string
Expand All @@ -80,8 +81,7 @@ func (e RuntimeMissingError) Error() string {
return fmt.Sprintf("the %s profiler needs %s, which was not found on PATH", e.Language.Display, e.Runtime)
}

// Find returns the add-on for a language. An explicit override wins over PATH. The runtime
// check comes last so a missing add-on and a missing runtime are reported separately.
// Find returns the add-on for a language. An explicit override wins over PATH.
func (f Finder) Find(language Language) (Found, error) {
found := Found{Language: language}
if override := f.Getenv(language.OverrideEnv); override != "" {
Expand All @@ -104,12 +104,19 @@ func (f Finder) Find(language Language) (Found, error) {
return Found{}, NotInstalledError{language}
}
}
return found, nil
}

// MissingRuntime returns the first program the add-on needs that is not on PATH, or "" when
// they are all there. An add-on that ships its own runtime, such as the system package of the
// TypeScript profiler, runs without them, so this is only asked after an add-on failed to run.
func (f Finder) MissingRuntime(language Language) string {
for _, runtime := range language.Runtime {
if _, err := f.LookPath(runtime); err != nil {
return Found{}, RuntimeMissingError{Language: language, Runtime: runtime}
return runtime
}
}
return found, nil
return ""
}

// Info is what an add-on reports about itself for `--version --json`.
Expand Down
16 changes: 11 additions & 5 deletions internal/plugins/plugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ func TestFindReportsAMissingAddOnWithHowToInstallIt(t *testing.T) {
}
}

func TestFindReportsAMissingRuntimeSeparately(t *testing.T) {
_, err := finder(map[string]string{"rc-profiler-typescript": "/bin/short"}, nil).Find(TypeScript)
var runtime RuntimeMissingError
if !errors.As(err, &runtime) || runtime.Runtime != "node" || !strings.Contains(err.Error(), "needs node") {
t.Fatalf("got %v", err)
func TestFindDoesNotRequireTheRuntimeBecauseAPackagedAddOnCarriesItsOwn(t *testing.T) {
withoutNode := finder(map[string]string{"rc-profiler-typescript": "/bin/short"}, nil)
if found, err := withoutNode.Find(TypeScript); err != nil || found.Path != "/bin/short" {
t.Fatalf("%+v %v", found, err)
}
if got := withoutNode.MissingRuntime(TypeScript); got != "node" {
t.Fatalf("missing runtime %q, want node", got)
}
withNode := finder(map[string]string{"rc-profiler-typescript": "/bin/short", "node": "/bin/node"}, nil)
if got := withNode.MissingRuntime(TypeScript); got != "" {
t.Fatalf("missing runtime %q, want none", got)
}
}

Expand Down
Loading