|
1 | 1 | package browse |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "io/ioutil" |
6 | 4 | "net/http" |
7 | 5 | "testing" |
8 | 6 |
|
9 | 7 | "github.com/cli/cli/internal/ghrepo" |
10 | | - "github.com/cli/cli/internal/run" |
11 | 8 | "github.com/cli/cli/pkg/cmdutil" |
12 | 9 | "github.com/cli/cli/pkg/httpmock" |
13 | 10 | "github.com/cli/cli/pkg/iostreams" |
14 | | - "github.com/cli/cli/test" |
15 | | - "github.com/google/shlex" |
16 | 11 | "github.com/stretchr/testify/assert" |
17 | 12 | ) |
18 | 13 |
|
19 | | -func runCommand(rt http.RoundTripper, repo ghrepo.Interface, cli string) (*test.CmdOut, error) { |
20 | | - io, _, stdout, stderr := iostreams.Test() |
21 | | - |
22 | | - browser := &cmdutil.TestBrowser{} |
23 | | - factory := &cmdutil.Factory{ |
24 | | - IOStreams: io, |
25 | | - Browser: browser, |
| 14 | +func TestNewCmdBrowse(t *testing.T) { |
| 15 | + f := cmdutil.Factory{} |
| 16 | + var opts *BrowseOptions |
| 17 | + // pass a stub implementation of `runBrowse` for testing to avoid having real `runBrowse` called |
| 18 | + cmd := NewCmdBrowse(&f, func(o *BrowseOptions) error { |
| 19 | + opts = o |
| 20 | + return nil |
| 21 | + }) |
| 22 | + |
| 23 | + cmd.SetArgs([]string{"--branch", "main"}) |
| 24 | + _, err := cmd.ExecuteC() |
| 25 | + assert.NoError(t, err) |
| 26 | + |
| 27 | + assert.Equal(t, "main", opts.Branch) |
| 28 | + assert.Equal(t, "", opts.SelectorArg) |
| 29 | + assert.Equal(t, false, opts.ProjectsFlag) |
| 30 | + assert.Equal(t, false, opts.WikiFlag) |
| 31 | + assert.Equal(t, false, opts.SettingsFlag) |
| 32 | + assert.Equal(t, 1, opts.FlagAmount) |
| 33 | +} |
26 | 34 |
|
27 | | - HttpClient: func() (*http.Client, error) { |
28 | | - return &http.Client{Transport: rt}, nil |
| 35 | +func Test_runBrowse(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + opts BrowseOptions |
| 39 | + baseRepo ghrepo.Interface |
| 40 | + defaultBranch string |
| 41 | + expectedURL string |
| 42 | + wantsErr bool |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "no arguments", |
| 46 | + opts: BrowseOptions{ |
| 47 | + SelectorArg: "", |
| 48 | + FlagAmount: 0, |
| 49 | + }, |
| 50 | + baseRepo: ghrepo.New("jessica", "cli"), |
| 51 | + expectedURL: "https://github.com/jessica/cli", |
29 | 52 | }, |
30 | | - BaseRepo: func() (ghrepo.Interface, error) { |
31 | | - return repo, nil |
| 53 | + { |
| 54 | + name: "file argument", |
| 55 | + opts: BrowseOptions{SelectorArg: "path/to/file.txt"}, |
| 56 | + baseRepo: ghrepo.New("bchadwic", "cli"), |
| 57 | + defaultBranch: "main", |
| 58 | + expectedURL: "https://github.com/bchadwic/cli/tree/main/path/to/file.txt", |
32 | 59 | }, |
33 | | - } |
34 | | - |
35 | | - cmd := NewCmdBrowse(factory) |
36 | | - |
37 | | - argv, err := shlex.Split(cli) |
38 | | - if err != nil { |
39 | | - return nil, err |
40 | | - } |
41 | | - cmd.SetArgs(argv) |
42 | | - |
43 | | - cmd.SetIn(&bytes.Buffer{}) |
44 | | - cmd.SetOut(ioutil.Discard) |
45 | | - cmd.SetErr(ioutil.Discard) |
46 | | - |
47 | | - _, err = cmd.ExecuteC() |
48 | | - return &test.CmdOut{ |
49 | | - OutBuf: stdout, |
50 | | - ErrBuf: stderr, |
51 | | - BrowsedURL: browser.BrowsedURL(), |
52 | | - }, err |
53 | | -} |
54 | | -// func TestNewCmdBrowse(t *testing.T) { |
55 | | - |
56 | | -// type args struct { |
57 | | -// repo ghrepo.Interface |
58 | | -// cli string |
59 | | -// } |
60 | | - |
61 | | -// tests := []struct { |
62 | | -// name string |
63 | | -// args args |
64 | | -// errorExpected bool |
65 | | -// stdoutExpected string |
66 | | -// stderrExpected string |
67 | | -// urlExpected string |
68 | | -// }{ |
69 | | -// { |
70 | | -// name: "multiple flag", |
71 | | -// args: args{ |
72 | | -// repo: ghrepo.New("jessica", "cli"), |
73 | | -// cli: "--settings --projects", |
74 | | -// }, |
75 | | -// errorExpected: true, |
76 | | -// stdoutExpected: "", |
77 | | -// stderrExpected: "these two flags are incompatible, see below for instructions", |
78 | | -// urlExpected: "", |
79 | | -// }, |
80 | | -// { |
81 | | -// name: "settings flag", |
82 | | -// args: args{ |
83 | | -// repo: ghrepo.New("husrav", "cli"), |
84 | | -// cli: "--settings", |
85 | | -// }, |
86 | | -// errorExpected: false, |
87 | | -// stdoutExpected: "now opening https://github.com/husrav/cli/settings in browser . . .\n", |
88 | | -// stderrExpected: "", |
89 | | -// urlExpected: "https://github.com/husrav/cli/settings", |
90 | | -// }, |
91 | | -// { |
92 | | -// name: "projects flag", |
93 | | -// args: args{ |
94 | | -// repo: ghrepo.New("ben", "cli"), |
95 | | -// cli: "--projects", |
96 | | -// }, |
97 | | -// errorExpected: false, |
98 | | -// stdoutExpected: "now opening https://github.com/ben/cli/projects in browser . . .\n", |
99 | | -// stderrExpected: "", |
100 | | -// urlExpected: "https://github.com/ben/cli/projects", |
101 | | -// }, |
102 | | -// { |
103 | | -// name: "wiki flag", |
104 | | -// args: args{ |
105 | | -// repo: ghrepo.New("thanh", "cli"), |
106 | | -// cli: "--wiki", |
107 | | -// }, |
108 | | -// errorExpected: false, |
109 | | -// stdoutExpected: "now opening https://github.com/thanh/cli/wiki in browser . . .\n", |
110 | | -// stderrExpected: "", |
111 | | -// urlExpected: "https://github.com/thanh/cli/wiki", |
112 | | -// }, |
113 | | -// } |
114 | | - |
115 | | -// for _, tt := range tests { |
116 | | -// t.Run(tt.name, func(t *testing.T) { |
117 | | -// http := &httpmock.Registry{} |
118 | | -// // http.StubRepoInfoResponse(tt.args.repo.RepoHost(), tt.args.repo.RepoName(), "main") |
119 | | -// defer http.Verify(t) |
120 | | - |
121 | | -// _, cmdTeardown := run.Stub() |
122 | | -// defer cmdTeardown(t) |
123 | | - |
124 | | -// output, err := runCommand(http, tt.args.repo, tt.args.cli) |
125 | | - |
126 | | -// if tt.errorExpected { |
127 | | -// assert.Equal(t, err.Error(), tt.stderrExpected) |
128 | | -// } else { |
129 | | -// assert.Equal(t, err, nil) |
130 | | -// } |
131 | | -// assert.Equal(t, tt.stdoutExpected, output.OutBuf.String()) |
132 | | -// assert.Equal(t, tt.urlExpected, output.BrowsedURL) |
133 | | -// }) |
134 | | -// } |
135 | | -// } |
136 | | - |
137 | | -func TestBrowse(t *testing.T) { |
138 | | - tests := []BrowseOptions{ |
139 | 60 | { |
140 | | - |
141 | | - BaseRepo func() (ghrepo.Interface, error) |
142 | | - Browser browser |
143 | | - HttpClient func() (*http.Client, error) |
144 | | - IO *iostreams.IOStreams |
145 | | - |
146 | | - FlagAmount int |
147 | | - SelectorArg string |
148 | | - |
149 | | - Branch string |
150 | | - ProjectsFlag bool |
151 | | - RepoFlag bool |
152 | | - SettingsFlag bool |
153 | | - WikiFlag bool |
| 61 | + name: "branch flag", |
| 62 | + opts: BrowseOptions{ |
| 63 | + Branch: "trunk", |
| 64 | + FlagAmount: 1, |
| 65 | + }, |
| 66 | + baseRepo: ghrepo.New("bchadwic", "cli"), |
| 67 | + expectedURL: "https://github.com/bchadwic/cli/tree/trunk", |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "settings flag", |
| 71 | + opts: BrowseOptions{ |
| 72 | + SettingsFlag: true, |
| 73 | + FlagAmount: 1, |
| 74 | + }, |
| 75 | + baseRepo: ghrepo.New("bchadwic", "cli"), |
| 76 | + expectedURL: "https://github.com/bchadwic/cli/settings", |
154 | 77 | }, |
155 | 78 | } |
156 | | - for _,tt range tests { |
| 79 | + for _, tt := range tests { |
| 80 | + t.Run(tt.name, func(t *testing.T) { |
| 81 | + io, _, stdout, stderr := iostreams.Test() |
| 82 | + browser := cmdutil.TestBrowser{} |
| 83 | + |
| 84 | + reg := httpmock.Registry{} |
| 85 | + defer reg.Verify(t) |
| 86 | + if tt.defaultBranch != "" { |
| 87 | + reg.StubRepoInfoResponse(tt.baseRepo.RepoOwner(), tt.baseRepo.RepoName(), tt.defaultBranch) |
| 88 | + } |
| 89 | + |
| 90 | + opts := tt.opts |
| 91 | + opts.IO = io |
| 92 | + opts.BaseRepo = func() (ghrepo.Interface, error) { |
| 93 | + return tt.baseRepo, nil |
| 94 | + } |
| 95 | + opts.HttpClient = func() (*http.Client, error) { |
| 96 | + return &http.Client{Transport: ®}, nil |
| 97 | + } |
| 98 | + opts.Browser = &browser |
| 99 | + |
| 100 | + err := runBrowse(&opts) |
| 101 | + if tt.wantsErr { |
| 102 | + assert.Error(t, err) |
| 103 | + } else { |
| 104 | + assert.NoError(t, err) |
| 105 | + } |
157 | 106 |
|
| 107 | + assert.Equal(t, "", stdout.String()) |
| 108 | + assert.Equal(t, "", stderr.String()) |
| 109 | + browser.Verify(t, tt.expectedURL) |
| 110 | + }) |
158 | 111 | } |
159 | 112 | } |
160 | 113 |
|
@@ -198,6 +151,5 @@ func TestFileArgParsing(t *testing.T) { |
198 | 151 | } |
199 | 152 | assert.Equal(t, tt.fileArg, arr[0]) |
200 | 153 | } |
201 | | - |
202 | 154 | } |
203 | 155 | } |
0 commit comments