-
Notifications
You must be signed in to change notification settings - Fork 7
fix(list): list a namespace's commands the same way everywhere #137
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
Open
vrobert78
wants to merge
3
commits into
main
Choose a base branch
from
fix/namespace-listing-consistency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "slices" | ||
| "sort" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| var commandNamePattern = regexp.MustCompile(`(?m)^\s{2}(project:[a-z0-9:-]+)`) | ||
|
|
||
| // commandNames returns the command names listed in a command listing. | ||
| func commandNames(listing string) []string { | ||
| matches := commandNamePattern.FindAllStringSubmatch(listing, -1) | ||
| names := make([]string, 0, len(matches)) | ||
| for _, m := range matches { | ||
| names = append(names, m[1]) | ||
| } | ||
| sort.Strings(names) | ||
| return names | ||
| } | ||
|
|
||
| // TestListNamespace checks that naming a namespace lists the same commands as | ||
| // "list <namespace>". Symfony lists the namespace itself when the name is not a | ||
| // command, but it builds its own descriptor, which describes lazily-loaded | ||
| // commands without resolving them: hidden and disabled commands were listed, | ||
| // and the formatting differed from the rest of the CLI. | ||
| func TestListNamespace(t *testing.T) { | ||
| f := newCommandFactory(t, "", "") | ||
|
|
||
| viaList, _, err := f.RunCombinedOutput("list", "project") | ||
| require.NoError(t, err) | ||
|
|
||
| // A namespace is not a command, so the CLI reports a failure, as before. | ||
| viaNamespace, viaNamespaceErr, _ := f.RunCombinedOutput("project") | ||
| // Symfony writes this listing to stderr. | ||
| namespaceListing := viaNamespace + viaNamespaceErr | ||
|
|
||
| // The listing comes from the legacy CLI, which does not know the commands | ||
| // implemented in Go: the list command adds those to its own output. So the | ||
| // namespace listing is the list output minus the Go commands. | ||
| goCommands := []string{"project:init"} | ||
| var expected []string | ||
| for _, name := range commandNames(viaList) { | ||
| if !slices.Contains(goCommands, name) { | ||
| expected = append(expected, name) | ||
| } | ||
| } | ||
| assert.Equal(t, expected, commandNames(namespaceListing), | ||
| "naming a namespace must list the same commands as 'list <namespace>'") | ||
| for _, name := range goCommands { | ||
| assert.Contains(t, commandNames(viaList), name, | ||
| "the list command must still add the commands implemented in Go") | ||
| } | ||
|
|
||
| // Asking for help on a namespace lists it too, rather than reporting an | ||
| // ambiguous command name. Unlike the bare namespace, this was asked for | ||
| // explicitly, so it succeeds and writes to stdout. | ||
| viaHelp, _, err := f.RunCombinedOutput("help", "project") | ||
| require.NoError(t, err) | ||
| assert.Equal(t, commandNames(namespaceListing), commandNames(viaHelp), | ||
| "'help <namespace>' must list the same commands as the namespace itself") | ||
| assert.NotContains(t, viaHelp, "is ambiguous") | ||
|
|
||
| // Hidden commands stay hidden: project:curl, and the deprecated | ||
| // project:variable:* commands, are only listed by 'list --all'. | ||
| for _, listing := range []string{namespaceListing, viaHelp} { | ||
| for _, hidden := range []string{"project:curl", "project:variable:get"} { | ||
| assert.NotContains(t, listing, hidden) | ||
| } | ||
| } | ||
| viaListAll, _, err := f.RunCombinedOutput("list", "project", "--all") | ||
| require.NoError(t, err) | ||
| assert.Contains(t, viaListAll, "project:curl", "--all must still show hidden commands") | ||
|
|
||
| // The listing uses the CLI's own descriptor: aliases in parentheses, not | ||
| // Symfony's "[alias|alias]" form. | ||
| assert.Contains(t, namespaceListing, "(projects, pro)") | ||
| assert.NotContains(t, namespaceListing, "[projects|pro]") | ||
|
|
||
| // An abbreviated namespace resolves as well. It used to be reported as | ||
| // ambiguous, because the hidden project:variable:* commands made | ||
| // "project:variable" count as a second namespace matching "proj". | ||
| viaAbbreviation, viaAbbreviationErr, _ := f.RunCombinedOutput("proj") | ||
| assert.Equal(t, commandNames(namespaceListing), commandNames(viaAbbreviation+viaAbbreviationErr)) | ||
| assert.NotContains(t, viaAbbreviation+viaAbbreviationErr, "is ambiguous") | ||
| } | ||
|
|
||
| // TestCompletionHidesHiddenCommands checks that command name completion does not | ||
| // suggest hidden commands. Symfony skips them, but it read the hidden state from | ||
| // the LazyCommand wrapper, which does not have the one this CLI decides on. | ||
| func TestCompletionHidesHiddenCommands(t *testing.T) { | ||
| f := newCommandFactory(t, "", "") | ||
|
|
||
| // The long options are what the completion scripts send. | ||
| suggestions, _, err := f.RunCombinedOutput("_complete", "--no-interaction", | ||
| "--shell=zsh", "--api-version=1", "--current=1", "--input=platform-test", "--input=") | ||
| require.NoError(t, err) | ||
|
|
||
| // Each suggestion is a name and a description, separated by a tab. | ||
| lines := strings.Split(strings.TrimSpace(suggestions), "\n") | ||
| names := make([]string, 0, len(lines)) | ||
| for _, line := range lines { | ||
| names = append(names, strings.SplitN(line, "\t", 2)[0]) | ||
| } | ||
| require.Greater(t, len(names), 100, "the whole command list should be suggested") | ||
|
|
||
| // Hidden commands: project:curl and its siblings, and the deprecated | ||
| // project:variable:* commands. | ||
| for _, hidden := range []string{"project:curl", "api:curl", "project:variable:get"} { | ||
| assert.NotContains(t, names, hidden, "a hidden command must not be suggested") | ||
| } | ||
| // Ordinary commands are still suggested. | ||
| for _, visible := range []string{"project:list", "environment:list"} { | ||
| assert.Contains(t, names, visible) | ||
| } | ||
| } | ||
|
|
||
| // TestListNamespaceDisabledCommand checks that a command disabled by | ||
| // configuration, as a vendor distribution does, is not listed by any of the | ||
| // listing paths. | ||
| func TestListNamespaceDisabledCommand(t *testing.T) { | ||
| baseConfig, err := os.ReadFile("config.yaml") | ||
| require.NoError(t, err) | ||
|
|
||
| // Disable project:create, the way a vendor configuration does. | ||
| var cnf map[string]any | ||
| require.NoError(t, yaml.Unmarshal(baseConfig, &cnf)) | ||
| application, ok := cnf["application"].(map[string]any) | ||
| require.True(t, ok, "the test config must have an application section") | ||
| application["disabled_commands"] = []string{"project:create"} | ||
|
|
||
| out, err := yaml.Marshal(cnf) | ||
| require.NoError(t, err) | ||
| configPath := filepath.Join(t.TempDir(), "config.yaml") | ||
| require.NoError(t, os.WriteFile(configPath, out, 0o600)) | ||
|
|
||
| f := newCommandFactory(t, "", "") | ||
| // A later CLI_CONFIG_FILE wins over the one testEnv sets. | ||
| f.extraEnv = []string{"CLI_CONFIG_FILE=" + configPath} | ||
|
|
||
| viaList, _, err := f.RunCombinedOutput("list", "project") | ||
| require.NoError(t, err) | ||
| assert.NotContains(t, viaList, "project:create") | ||
|
|
||
| viaNamespace, viaNamespaceErr, _ := f.RunCombinedOutput("project") | ||
| assert.NotContains(t, viaNamespace+viaNamespaceErr, "project:create", | ||
| "a disabled command must not be listed for its namespace") | ||
|
|
||
| viaHelp, _, err := f.RunCombinedOutput("help", "project") | ||
| require.NoError(t, err) | ||
| assert.NotContains(t, viaHelp, "project:create", | ||
| "a disabled command must not be listed by 'help <namespace>'") | ||
|
|
||
| // It is not listed even with --all: it cannot be run at all. | ||
| viaListAll, _, err := f.RunCombinedOutput("list", "project", "--all") | ||
| require.NoError(t, err) | ||
| assert.NotContains(t, viaListAll, "project:create") | ||
|
|
||
| // Sanity check: the same listing without the override does show it. | ||
| f.extraEnv = nil | ||
| plain, _, err := f.RunCombinedOutput("list", "project") | ||
| require.NoError(t, err) | ||
| assert.Contains(t, plain, "project:create") | ||
| assert.True(t, strings.Contains(plain, "project:list")) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.