Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Jan 10, 2024
1 parent 48ff47f commit 4d40f81
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
30 changes: 14 additions & 16 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,32 +593,22 @@ 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, but got %v", err)
}
if pluginName != "my-plugin" {
t.Fatalf("expected plugin name my-plugin, but got %s", pluginName)
}

if runtime.GOOS == "windows" {
pluginName, err = parsePluginName("notation-my-plugin.exe")
pluginName, err := parsePluginName("notation-my-plugin.exe")
if err != nil {
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 notation-com.plugin"
_, err = parsePluginName("notation-com.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}.exe, but got my-plugin.exe"
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)
Expand All @@ -636,6 +626,14 @@ func TestParsePluginName(t *testing.T) {
t.Fatalf("expected %s, but got %v", expectedErrorMsg, err)
}
} else {
pluginName, err := parsePluginName("notation-my-plugin")
if err != nil {
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.example.plugin")
if err != nil {
t.Fatalf("expected nil err, but got %v", err)
Expand Down
6 changes: 3 additions & 3 deletions plugin/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ 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 := fileName
if strings.EqualFold(filepath.Ext(fileName), ".exe") {
fname = file.TrimFileExtension(fileName)
if !strings.EqualFold(filepath.Ext(fileName), ".exe") {
return "", fmt.Errorf("invalid plugin executable file name. Plugin file name requires format notation-{plugin-name}.exe, but got %s", fileName)
}
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}.exe, but got %s", fileName)
Expand Down

0 comments on commit 4d40f81

Please sign in to comment.