Skip to content
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

feat: plugin install iteration 2 #369

Merged
merged 31 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func parsePluginFromDir(ctx context.Context, path string) (string, string, error
if err := setExecutable(candidate); err != nil {
return "", "", fmt.Errorf("no plugin executable file was found: %w", err)
}
logger.Warnf("Found candidate plugin executable file %q without executable permission. Setting user executable bit and try install.", filepath.Base(candidate))
logger.Warnf("Found candidate plugin executable file %q without executable permission. Setting user executable bit and trying to install.", filepath.Base(candidate))
return candidate, candidatePluginName, nil
}
return "", "", errors.New("no plugin executable file was found")
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
62 changes: 47 additions & 15 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,40 +595,72 @@ func TestManager_Uninstall(t *testing.T) {
func TestParsePluginName(t *testing.T) {
pluginName, err := parsePluginName("notation-my-plugin")
if err != nil {
t.Fatalf("expected nil err, got %v", err)
t.Fatalf("expected nil err, but got %v", err)
}
if pluginName != "my-plugin" {
t.Fatalf("expected plugin name my-plugin, but got %s", pluginName)
}

_, err = parsePluginName("myPlugin")
expectedErrorMsg := "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got myPlugin"
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, got %v", expectedErrorMsg, err)
}

_, err = parsePluginName("my-plugin")
expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got my-plugin"
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, got %v", expectedErrorMsg, err)
}

if runtime.GOOS == "windows" {
pluginName, err = parsePluginName("notation-my-plugin.exe")
if err != nil {
t.Fatalf("expected nil err, got %v", err)
t.Fatalf("expected nil err, but got %v", err)
}
if pluginName != "my-plugin" {
t.Fatalf("expected plugin name my-plugin, but got %s", pluginName)
}

pluginName, err = parsePluginName("notation-com.plugin")
if err != nil {
t.Fatalf("expected nil err, but got %v", err)
}
if pluginName != "com.plugin" {
t.Fatalf("expected plugin name com.plugin, but got %s", pluginName)
}

expectedErrorMsg := "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got my-plugin.exe"
_, err = parsePluginName("my-plugin.exe")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}

expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got notation-.exe"
_, err = parsePluginName("notation-.exe")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}

expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got my-plugin"
_, err = parsePluginName("my-plugin")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}
} else {
pluginName, err = parsePluginName("notation-com.example.plugin")
if err != nil {
t.Fatalf("expected nil err, got %v", err)
t.Fatalf("expected nil err, but got %v", err)
}
if pluginName != "com.example.plugin" {
t.Fatalf("expected plugin name com.example.plugin, but got %s", pluginName)
}

expectedErrorMsg := "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got myPlugin"
_, err = parsePluginName("myPlugin")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}

expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got my-plugin"
_, err = parsePluginName("my-plugin")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}

expectedErrorMsg = "invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got notation-"
_, err = parsePluginName("notation-")
if err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions plugin/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ func isExecutableFile(filePath string) (bool, error) {
// parsePluginName checks if fileName is a valid plugin file name
// and gets plugin name from it based on spec: https://github.com/notaryproject/specifications/blob/main/specs/plugin-extensibility.md#installation
func parsePluginName(fileName string) (string, error) {
fname := file.TrimFileExtension(fileName)
fname := fileName
if strings.EqualFold(filepath.Ext(fileName), ".exe") {
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
fname = file.TrimFileExtension(fileName)
}
pluginName, found := strings.CutPrefix(fname, proto.Prefix)
if !found || pluginName == "" {
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}, but got %s", fname)
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName)
}
return pluginName, nil
}

// setExecutable returns error on Windows. User needs to install the correct
// plugin file.
func setExecutable(filePath string) error {
return fmt.Errorf("on Windows, plugin executable file must have file extension '.exe', but got %s", filepath.Base(filePath))
return fmt.Errorf(`plugin executable file must have file extension ".exe", but got %q`, filepath.Base(filePath))
}
Loading