| @@ -1,10 +1,15 @@ |
| 1 | 1 | package main |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | "bytes" |
| 4 | 5 | "slices" |
| 6 | "strings" |
| 5 | 7 | "testing" |
| 6 | 8 | |
| 7 | 9 | "github.com/spf13/cobra" |
| 10 | |
| 11 | "gitbay.org/gitbay/internal/control" |
| 12 | "gitbay.org/gitbay/internal/protocol" |
| 8 | 13 | ) |
| 9 | 14 | |
| 10 | 15 | // TestServerPathMismatches pins the commands whose CLI path differs from |
| @@ -79,3 +84,50 @@ func TestHelpArgvSendsThePathForAMismatchedGroup(t *testing.T) { |
| 79 | 84 | t.Errorf("helpArgv(keys, auth keys) = %v, want %v", got, want) |
| 80 | 85 | } |
| 81 | 86 | } |
| 87 | |
| 88 | // TestRenamedCommandHelpNamesTheCLIPath runs the server's help for every |
| 89 | // command whose CLI path differs from its registered path, sent as the |
| 90 | // CLI sends it, and fails if the text names the registered path where the |
| 91 | // CLI's belongs (#304). |
| 92 | func TestRenamedCommandHelpNamesTheCLIPath(t *testing.T) { |
| 93 | type renamed struct{ cli, server string } |
| 94 | var cmds []renamed |
| 95 | var walk func(*cobra.Command) |
| 96 | walk = func(c *cobra.Command) { |
| 97 | if p := c.Annotations[serverPath]; p != "" && p != "help" { |
| 98 | if cli := cliPathOf(c); cli != p { |
| 99 | cmds = append(cmds, renamed{cli, p}) |
| 100 | } |
| 101 | } |
| 102 | for _, sub := range c.Commands() { |
| 103 | walk(sub) |
| 104 | } |
| 105 | } |
| 106 | root := newRoot() |
| 107 | root.InitDefaultHelpCmd() |
| 108 | walk(root) |
| 109 | if len(cmds) == 0 { |
| 110 | t.Fatal("no renamed commands found") |
| 111 | } |
| 112 | for _, rc := range cmds { |
| 113 | argv := withCLIPath(rc.cli, rc.server, append([]string{"help"}, strings.Fields(rc.server)...)) |
| 114 | var out, errOut bytes.Buffer |
| 115 | c := &control.Ctx{Stdout: &out, Stderr: &errOut, Scope: "full"} |
| 116 | c.Cfg.Server.SiteURL = "https://forge.test" |
| 117 | if code := control.Dispatch(c, argv); code != protocol.ExitOK { |
| 118 | t.Errorf("%v: exit %d: %s", argv, code, errOut.String()) |
| 119 | continue |
| 120 | } |
| 121 | // What the CLI path itself contains (repo topics list holds the |
| 122 | // registered repo topics) is not a mismatch. |
| 123 | // SEE ALSO names sibling commands, which keep their own paths. |
| 124 | text, _, _ := strings.Cut(out.String(), "SEE ALSO") |
| 125 | text = strings.ReplaceAll(text, "gitbay "+rc.cli, "") |
| 126 | for _, line := range strings.Split(text, "\n") { |
| 127 | if strings.Contains(line, "gitbay "+rc.server+" ") || strings.HasSuffix(line, "gitbay "+rc.server) { |
| 128 | t.Errorf("%s: help names the registered path %q:\n%s", rc.cli, rc.server, out.String()) |
| 129 | break |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |