diff --git a/plugin/manager.go b/plugin/manager.go index 6570c805..71d8ec31 100644 --- a/plugin/manager.go +++ b/plugin/manager.go @@ -255,11 +255,13 @@ func parsePluginFromDir(ctx context.Context, path string) (string, string, error } // only take regular files if info.Mode().IsRegular() { - if candidatePluginName, err = parsePluginName(d.Name()); err != nil { + name, err := parsePluginName(d.Name()) + if err != nil { // file name does not follow the notation-{plugin-name} format, // continue return nil } + candidatePluginName = name filesWithValidNameFormat = append(filesWithValidNameFormat, p) isExec, err := isExecutableFile(p) if err != nil { diff --git a/plugin/manager_test.go b/plugin/manager_test.go index 4dd9aa14..6ff73713 100644 --- a/plugin/manager_test.go +++ b/plugin/manager_test.go @@ -465,6 +465,44 @@ func TestManager_Install(t *testing.T) { } }) + t.Run("success to install from plugin dir with one valid candidate file followed by other files", func(t *testing.T) { + existedPluginFilePath := "testdata/plugins/foo/notation-foo" + newPluginFilePath := "testdata/foo/notation-foo" + // the walk visits files in lexical order, so this file is visited + // after the candidate plugin executable file + newPluginDocPath := "testdata/foo/readme.md" + newPluginDir := filepath.Dir(newPluginFilePath) + if err := os.MkdirAll(newPluginDir, 0777); err != nil { + t.Fatalf("failed to create %s: %v", newPluginDir, err) + } + defer os.RemoveAll(newPluginDir) + if err := createFileAndChmod(newPluginFilePath, 0600); err != nil { + t.Fatal(err) + } + if err := createFileAndChmod(newPluginDocPath, 0600); err != nil { + t.Fatal(err) + } + executor = testInstallCommander{ + existedPluginFilePath: existedPluginFilePath, + newPluginFilePath: newPluginFilePath, + existedPluginStdout: metadataJSON(validMetadata), + newPluginStdout: metadataJSON(validMetadataHigherVersion), + } + installOpts := CLIInstallOptions{ + PluginPath: newPluginDir, + } + existingPluginMetadata, newPluginMetadata, err := mgr.Install(context.Background(), installOpts) + if err != nil { + t.Fatalf("expecting nil error, but got %v", err) + } + if existingPluginMetadata.Version != "1.0.0" { + t.Fatalf("expecting existing plugin metadata to be 1.0.0, but got %s", existingPluginMetadata.Version) + } + if newPluginMetadata.Version != "1.1.0" { + t.Fatalf("expecting new plugin metadata to be 1.1.0, but got %s", newPluginMetadata.Version) + } + }) + t.Run("success to install from plugin dir with no executable file and one valid candidate file", func(t *testing.T) { existedPluginFilePath := "testdata/plugins/foo/notation-foo" newPluginFilePath := "testdata/foo/notation-foo"