Skip to content

Commit d51a925

Browse files
committed
update open function
1 parent 1a3a54f commit d51a925

File tree

4 files changed

+223
-131
lines changed

4 files changed

+223
-131
lines changed

cmd/browser.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"errors"
55
"os/exec"
66
"runtime"
7-
8-
"github.com/pkg/browser"
97
)
108

119
// ErrMockBrowser is used for testing browser errors
@@ -19,8 +17,16 @@ func openURLInBrowser(url string) error {
1917
if runtime.GOOS == "linux" {
2018
return openWithXdgOpen(url)
2119
}
22-
// For other platforms, use the default browser library
23-
return browser.OpenURL(url)
20+
// On macOS, use open command with output redirection to suppress messages
21+
if runtime.GOOS == "darwin" {
22+
return openWithMacOSOpen(url)
23+
}
24+
// On Windows, use start command with output redirection to suppress messages
25+
if runtime.GOOS == "windows" {
26+
return openWithWindowsStart(url)
27+
}
28+
// For other platforms, return an error
29+
return errors.New("unsupported platform: " + runtime.GOOS)
2430
}
2531

2632
func openWithXdgOpen(url string) error {
@@ -31,6 +37,22 @@ func openWithXdgOpen(url string) error {
3137
return cmd.Start()
3238
}
3339

40+
func openWithMacOSOpen(url string) error {
41+
cmd := exec.Command("open", url)
42+
// Redirect stdout and stderr to /dev/null to suppress output
43+
cmd.Stdout = nil
44+
cmd.Stderr = nil
45+
return cmd.Start()
46+
}
47+
48+
func openWithWindowsStart(url string) error {
49+
cmd := exec.Command("cmd", "/c", "start", "", url)
50+
// Redirect stdout and stderr to /dev/null to suppress output
51+
cmd.Stdout = nil
52+
cmd.Stderr = nil
53+
return cmd.Start()
54+
}
55+
3456
func openURLInBrowserFunc(url string) error {
3557
return OpenURLInBrowser(url)
3658
}

cmd/browser_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,189 @@ func Test_openWithXdgOpen(t *testing.T) {
8383
})
8484
}
8585
}
86+
87+
func Test_openWithMacOSOpen(t *testing.T) {
88+
// This test is for macOS-specific functionality
89+
if runtime.GOOS != "darwin" {
90+
t.Skip("Skipping test on non-macOS platforms")
91+
}
92+
93+
tests := []struct {
94+
name string
95+
url string
96+
wantErr bool
97+
}{
98+
{
99+
name: "success",
100+
url: "https://github.com/zhaochunqi/git-open",
101+
wantErr: false,
102+
},
103+
}
104+
105+
for _, tt := range tests {
106+
t.Run(tt.name, func(t *testing.T) {
107+
err := openWithMacOSOpen(tt.url)
108+
if (err != nil) != tt.wantErr {
109+
t.Errorf("openWithMacOSOpen() error = %v, wantErr %v", err, tt.wantErr)
110+
}
111+
})
112+
}
113+
}
114+
115+
func Test_openURLInBrowser_PlatformSpecific(t *testing.T) {
116+
// Save original function
117+
original := OpenURLInBrowser
118+
defer func() {
119+
OpenURLInBrowser = original
120+
}()
121+
122+
tests := []struct {
123+
name string
124+
url string
125+
expectError bool
126+
}{
127+
{
128+
name: "valid URL",
129+
url: "https://github.com/zhaochunqi/git-open",
130+
expectError: false,
131+
},
132+
{
133+
name: "empty URL",
134+
url: "",
135+
expectError: false, // Commands might handle empty URL gracefully
136+
},
137+
}
138+
139+
for _, tt := range tests {
140+
t.Run(tt.name, func(t *testing.T) {
141+
// Test the actual platform-specific implementation
142+
err := openURLInBrowser(tt.url)
143+
if (err != nil) != tt.expectError {
144+
t.Errorf("openURLInBrowser() error = %v, expectError %v", err, tt.expectError)
145+
}
146+
})
147+
}
148+
}
149+
150+
func Test_openURLInBrowser_AllPlatforms(t *testing.T) {
151+
tests := []struct {
152+
name string
153+
url string
154+
platform string
155+
}{
156+
{
157+
name: "Linux platform",
158+
url: "https://github.com/zhaochunqi/git-open",
159+
platform: "linux",
160+
},
161+
{
162+
name: "macOS platform",
163+
url: "https://github.com/zhaochunqi/git-open",
164+
platform: "darwin",
165+
},
166+
{
167+
name: "Windows platform",
168+
url: "https://github.com/zhaochunqi/git-open",
169+
platform: "windows",
170+
},
171+
}
172+
173+
for _, tt := range tests {
174+
t.Run(tt.name, func(t *testing.T) {
175+
// Save the original runtime.GOOS
176+
originalGOOS := runtime.GOOS
177+
// We can't actually change runtime.GOOS, but we can test the functions directly
178+
179+
switch tt.platform {
180+
case "linux":
181+
// Test openWithXdgOpen directly if not on Linux
182+
if runtime.GOOS != "linux" {
183+
err := openWithXdgOpen(tt.url)
184+
// On non-Linux systems, this should fail as xdg-open doesn't exist
185+
if err == nil {
186+
t.Logf("openWithXdgOpen() succeeded on %s platform, command might exist", originalGOOS)
187+
}
188+
}
189+
case "darwin":
190+
// Test openWithMacOSOpen directly if not on macOS
191+
if runtime.GOOS != "darwin" {
192+
err := openWithMacOSOpen(tt.url)
193+
// On non-macOS systems, this should fail as open command might not exist
194+
if err == nil {
195+
t.Logf("openWithMacOSOpen() succeeded on %s platform, command might exist", originalGOOS)
196+
}
197+
}
198+
case "windows":
199+
// Test openWithWindowsStart directly if not on Windows
200+
if runtime.GOOS != "windows" {
201+
err := openWithWindowsStart(tt.url)
202+
// On non-Windows systems, this should fail as cmd doesn't exist
203+
if err == nil {
204+
t.Logf("openWithWindowsStart() succeeded on %s platform, command might exist", originalGOOS)
205+
}
206+
}
207+
}
208+
})
209+
}
210+
}
211+
212+
func Test_openWithWindowsStart(t *testing.T) {
213+
// This test is for Windows-specific functionality
214+
if runtime.GOOS != "windows" {
215+
t.Skip("Skipping test on non-Windows platforms")
216+
}
217+
218+
tests := []struct {
219+
name string
220+
url string
221+
wantErr bool
222+
}{
223+
{
224+
name: "success",
225+
url: "https://github.com/zhaochunqi/git-open",
226+
wantErr: false,
227+
},
228+
}
229+
230+
for _, tt := range tests {
231+
t.Run(tt.name, func(t *testing.T) {
232+
err := openWithWindowsStart(tt.url)
233+
if (err != nil) != tt.wantErr {
234+
t.Errorf("openWithWindowsStart() error = %v, wantErr %v", err, tt.wantErr)
235+
}
236+
})
237+
}
238+
}
239+
240+
func Test_openURLInBrowser_UnsupportedPlatform(t *testing.T) {
241+
// Test error handling for unsupported platforms
242+
// We can't actually test this directly since we can't change runtime.GOOS,
243+
// but we can test the error message format
244+
url := "https://github.com/zhaochunqi/git-open"
245+
246+
// Create a mock scenario by testing the current platform
247+
// This test primarily ensures the function structure is correct
248+
err := openURLInBrowser(url)
249+
250+
// On supported platforms (linux, darwin, windows), this should succeed
251+
// On unsupported platforms, this would return an error
252+
supportedPlatforms := map[string]bool{
253+
"linux": true,
254+
"darwin": true,
255+
"windows": true,
256+
}
257+
258+
if supportedPlatforms[runtime.GOOS] {
259+
// Current platform is supported, so error should be nil or a command execution error
260+
if err != nil {
261+
t.Logf("openURLInBrowser() error on supported platform %s: %v (this might be expected in CI)", runtime.GOOS, err)
262+
}
263+
} else {
264+
// Current platform is not supported, should return unsupported platform error
265+
if err == nil {
266+
t.Errorf("openURLInBrowser() should return error for unsupported platform %s", runtime.GOOS)
267+
} else if err.Error() != "unsupported platform: "+runtime.GOOS {
268+
t.Errorf("openURLInBrowser() error = %v, want 'unsupported platform: %s'", err, runtime.GOOS)
269+
}
270+
}
271+
}

go.mod

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ require (
77
github.com/spf13/viper v1.20.1
88
)
99

10-
require (
11-
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
12-
golang.org/x/sync v0.13.0 // indirect
13-
)
10+
require github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
1411

1512
require (
1613
dario.cat/mergo v1.0.0 // indirect
@@ -24,17 +21,12 @@ require (
2421
github.com/go-git/go-billy/v5 v5.6.2 // indirect
2522
github.com/go-git/go-git/v5 v5.16.3
2623
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
27-
github.com/hashicorp/hcl v1.0.0 // indirect
2824
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2925
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
3026
github.com/kevinburke/ssh_config v1.2.0 // indirect
31-
github.com/magiconair/properties v1.8.7 // indirect
32-
github.com/mitchellh/mapstructure v1.5.0 // indirect
3327
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
3428
github.com/pjbgf/sha1cd v0.3.2 // indirect
35-
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
3629
github.com/sagikazarmark/locafero v0.7.0 // indirect
37-
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
3830
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
3931
github.com/skeema/knownhosts v1.3.1 // indirect
4032
github.com/sourcegraph/conc v0.3.0 // indirect
@@ -46,13 +38,9 @@ require (
4638
go.uber.org/atomic v1.9.0 // indirect
4739
go.uber.org/multierr v1.9.0 // indirect
4840
golang.org/x/crypto v0.37.0 // indirect
49-
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
50-
golang.org/x/mod v0.19.0 // indirect
5141
golang.org/x/net v0.39.0 // indirect
5242
golang.org/x/sys v0.32.0 // indirect
5343
golang.org/x/text v0.24.0 // indirect
54-
golang.org/x/tools v0.23.0 // indirect
55-
gopkg.in/ini.v1 v1.67.0 // indirect
5644
gopkg.in/warnings.v0 v0.1.2 // indirect
5745
gopkg.in/yaml.v3 v3.0.1 // indirect
5846
)

0 commit comments

Comments
 (0)