(t *testing.T)
| 30 | } |
| 31 | |
| 32 | func TestValidateCandidate(t *testing.T) { |
| 33 | const ( |
| 34 | goodPluginName = metadata.NamePrefix + "goodplugin" |
| 35 | builtinName = metadata.NamePrefix + "builtin" |
| 36 | builtinAlias = metadata.NamePrefix + "alias" |
| 37 | |
| 38 | badPrefixPath = "/usr/local/libexec/cli-plugins/wobble" |
| 39 | badNamePath = "/usr/local/libexec/cli-plugins/docker-123456" |
| 40 | goodPluginPath = "/usr/local/libexec/cli-plugins/" + goodPluginName |
| 41 | ) |
| 42 | |
| 43 | fakeroot := &cobra.Command{Use: "docker"} |
| 44 | fakeroot.AddCommand(&cobra.Command{ |
| 45 | Use: strings.TrimPrefix(builtinName, metadata.NamePrefix), |
| 46 | Aliases: []string{ |
| 47 | strings.TrimPrefix(builtinAlias, metadata.NamePrefix), |
| 48 | }, |
| 49 | }) |
| 50 | |
| 51 | for _, tc := range []struct { |
| 52 | name string |
| 53 | plugin *fakeCandidate |
| 54 | |
| 55 | // Either err or invalid may be non-empty, but not both (both can be empty for a good plugin). |
| 56 | err string |
| 57 | invalid string |
| 58 | expVer string |
| 59 | }{ |
| 60 | // Invalid cases. |
| 61 | { |
| 62 | name: "empty path", |
| 63 | plugin: &fakeCandidate{path: ""}, |
| 64 | err: "plugin candidate path cannot be empty", |
| 65 | }, |
| 66 | { |
| 67 | name: "bad prefix", |
| 68 | plugin: &fakeCandidate{path: badPrefixPath}, |
| 69 | err: fmt.Sprintf("does not have %q prefix", metadata.NamePrefix), |
| 70 | }, |
| 71 | { |
| 72 | name: "bad path", |
| 73 | plugin: &fakeCandidate{path: badNamePath}, |
| 74 | invalid: "did not match", |
| 75 | }, |
| 76 | { |
| 77 | name: "builtin command", |
| 78 | plugin: &fakeCandidate{path: builtinName}, |
| 79 | invalid: `plugin "builtin" duplicates builtin command`, |
| 80 | }, |
| 81 | { |
| 82 | name: "builtin alias", |
| 83 | plugin: &fakeCandidate{path: builtinAlias}, |
| 84 | invalid: `plugin "alias" duplicates an alias of builtin command "builtin"`, |
| 85 | }, |
| 86 | { |
| 87 | name: "fetch failure", |
| 88 | plugin: &fakeCandidate{path: goodPluginPath, exec: false}, |
| 89 | invalid: fmt.Sprintf("failed to fetch metadata: faked a failure to exec %q", goodPluginPath), |
nothing calls this directly
no test coverage detected
searching dependent graphs…