(t *testing.T)
| 126 | } |
| 127 | |
| 128 | func TestWriteHelp_aliasResolution(t *testing.T) { |
| 129 | newRoot := func() (*cobra.Command, *iostreams.IOStreams) { |
| 130 | ios, _, _, _ := iostreams.Test() |
| 131 | rootCmd := &cobra.Command{Use: "gh"} |
| 132 | prCmd := &cobra.Command{Use: "pr", Short: "Manage pull requests"} |
| 133 | listCmd := &cobra.Command{Use: "list", Short: "List pull requests", Run: func(*cobra.Command, []string) {}} |
| 134 | listCmd.Flags().Int("limit", 30, "Maximum number of items to fetch") |
| 135 | prCmd.AddCommand(listCmd) |
| 136 | rootCmd.AddCommand(prCmd) |
| 137 | return rootCmd, ios |
| 138 | } |
| 139 | |
| 140 | t.Run("configured alias renders the target command help", func(t *testing.T) { |
| 141 | rootCmd, ios := newRoot() |
| 142 | aliasCmd := NewCmdAlias(ios, "prs", "pr list") |
| 143 | rootCmd.AddCommand(aliasCmd) |
| 144 | |
| 145 | var buf bytes.Buffer |
| 146 | WriteHelp(&buf, ios.ColorScheme(), aliasCmd) |
| 147 | |
| 148 | out := buf.String() |
| 149 | require.Contains(t, out, "gh pr list") |
| 150 | require.Contains(t, out, "--limit") |
| 151 | require.NotContains(t, out, `Alias for "pr list"`) |
| 152 | }) |
| 153 | |
| 154 | t.Run("alias expansion carrying flags still resolves to the target", func(t *testing.T) { |
| 155 | rootCmd, ios := newRoot() |
| 156 | aliasCmd := NewCmdAlias(ios, "prs", "pr list --limit 5") |
| 157 | rootCmd.AddCommand(aliasCmd) |
| 158 | |
| 159 | var buf bytes.Buffer |
| 160 | WriteHelp(&buf, ios.ColorScheme(), aliasCmd) |
| 161 | |
| 162 | require.Contains(t, buf.String(), "gh pr list") |
| 163 | }) |
| 164 | |
| 165 | t.Run("shell alias has no target and renders its own help", func(t *testing.T) { |
| 166 | rootCmd, ios := newRoot() |
| 167 | aliasCmd := NewCmdShellAlias(ios, "sh", "!echo hi") |
| 168 | rootCmd.AddCommand(aliasCmd) |
| 169 | |
| 170 | var buf bytes.Buffer |
| 171 | WriteHelp(&buf, ios.ColorScheme(), aliasCmd) |
| 172 | |
| 173 | require.Contains(t, buf.String(), "gh sh") |
| 174 | }) |
| 175 | |
| 176 | t.Run("alias expanding to an unknown command falls back to the alias", func(t *testing.T) { |
| 177 | rootCmd, ios := newRoot() |
| 178 | aliasCmd := NewCmdAlias(ios, "zz", "nope missing") |
| 179 | rootCmd.AddCommand(aliasCmd) |
| 180 | |
| 181 | var buf bytes.Buffer |
| 182 | WriteHelp(&buf, ios.ColorScheme(), aliasCmd) |
| 183 | |
| 184 | require.Contains(t, buf.String(), "gh zz") |
| 185 | }) |
nothing calls this directly
no test coverage detected