Before, for plugin commands, only the plugin name (such as `buildx`) would be both included as `RootCmd` when passed to the hook plugin, which isn't enough information for a plugin to decide whether to execute a hook or not since plugins implement multiple varied commands (`buildx build`, `buildx prune`, etc.). This commit changes the hook logic to account for this situation, so that the the entire configured hook is passed, i.e., if a user has a hook configured for `buildx imagetools inspect` and the command `docker buildx imagetools inspect alpine` is called, then the plugin hooks will be passed `buildx imagetools inspect`. This logic works for aliased commands too, so whether `docker build ...` or `docker buildx build` is executed (unless Buildx is disabled) the hook will be invoked with `buildx build`. Signed-off-by: Laura Brehm <laurabrehm@hey.com> hooks: include full match when invoking plugins Signed-off-by: Laura Brehm <laurabrehm@hey.com>
111 lines
2.2 KiB
Go
111 lines
2.2 KiB
Go
package manager
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"gotest.tools/v3/assert"
|
|
)
|
|
|
|
func TestGetNaiveFlags(t *testing.T) {
|
|
testCases := []struct {
|
|
args []string
|
|
expectedFlags map[string]string
|
|
}{
|
|
{
|
|
args: []string{"docker"},
|
|
expectedFlags: map[string]string{},
|
|
},
|
|
{
|
|
args: []string{"docker", "build", "-q", "--file", "test.Dockerfile", "."},
|
|
expectedFlags: map[string]string{
|
|
"q": "",
|
|
"file": "",
|
|
},
|
|
},
|
|
{
|
|
args: []string{"docker", "--context", "a-context", "pull", "-q", "--progress", "auto", "alpine"},
|
|
expectedFlags: map[string]string{
|
|
"context": "",
|
|
"q": "",
|
|
"progress": "",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
assert.DeepEqual(t, getNaiveFlags(tc.args), tc.expectedFlags)
|
|
}
|
|
}
|
|
|
|
func TestPluginMatch(t *testing.T) {
|
|
testCases := []struct {
|
|
commandString string
|
|
pluginConfig map[string]string
|
|
expectedMatch string
|
|
expectedOk bool
|
|
}{
|
|
{
|
|
commandString: "image ls",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "image",
|
|
},
|
|
expectedMatch: "image",
|
|
expectedOk: true,
|
|
},
|
|
{
|
|
commandString: "context ls",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "build",
|
|
},
|
|
expectedMatch: "",
|
|
expectedOk: false,
|
|
},
|
|
{
|
|
commandString: "context ls",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "context ls",
|
|
},
|
|
expectedMatch: "context ls",
|
|
expectedOk: true,
|
|
},
|
|
{
|
|
commandString: "image ls",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "image ls,image",
|
|
},
|
|
expectedMatch: "image ls",
|
|
expectedOk: true,
|
|
},
|
|
{
|
|
commandString: "image ls",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "",
|
|
},
|
|
expectedMatch: "",
|
|
expectedOk: false,
|
|
},
|
|
{
|
|
commandString: "image inspect",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "image i",
|
|
},
|
|
expectedMatch: "",
|
|
expectedOk: false,
|
|
},
|
|
{
|
|
commandString: "image inspect",
|
|
pluginConfig: map[string]string{
|
|
"hooks": "image",
|
|
},
|
|
expectedMatch: "image",
|
|
expectedOk: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
match, ok := pluginMatch(tc.pluginConfig, tc.commandString)
|
|
assert.Equal(t, ok, tc.expectedOk)
|
|
assert.Equal(t, match, tc.expectedMatch)
|
|
}
|
|
}
|