Skip to content

Commit a68d12d

Browse files
committed
add test for different platform
1 parent d51a925 commit a68d12d

File tree

2 files changed

+78
-32
lines changed

2 files changed

+78
-32
lines changed

cmd/browser.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,28 @@ var ErrMockBrowser = errors.New("mock browser error")
1212
// OpenURLInBrowser is exported for testing
1313
var OpenURLInBrowser = openURLInBrowser
1414

15+
// getPlatform returns the current platform, can be mocked for testing
16+
var getPlatform = func() string {
17+
return runtime.GOOS
18+
}
19+
1520
func openURLInBrowser(url string) error {
21+
platform := getPlatform()
22+
1623
// On Linux, use xdg-open with output redirection to suppress messages
17-
if runtime.GOOS == "linux" {
24+
if platform == "linux" {
1825
return openWithXdgOpen(url)
1926
}
2027
// On macOS, use open command with output redirection to suppress messages
21-
if runtime.GOOS == "darwin" {
28+
if platform == "darwin" {
2229
return openWithMacOSOpen(url)
2330
}
2431
// On Windows, use start command with output redirection to suppress messages
25-
if runtime.GOOS == "windows" {
32+
if platform == "windows" {
2633
return openWithWindowsStart(url)
2734
}
2835
// For other platforms, return an error
29-
return errors.New("unsupported platform: " + runtime.GOOS)
36+
return errors.New("unsupported platform: " + platform)
3037
}
3138

3239
func openWithXdgOpen(url string) error {

cmd/browser_test.go

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -238,34 +238,73 @@ func Test_openWithWindowsStart(t *testing.T) {
238238
}
239239

240240
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,
241+
// Save original function
242+
originalGetPlatform := getPlatform
243+
defer func() {
244+
getPlatform = originalGetPlatform
245+
}()
246+
247+
tests := []struct {
248+
name string
249+
platform string
250+
url string
251+
wantErr bool
252+
errMsg string
253+
}{
254+
{
255+
name: "Linux platform",
256+
platform: "linux",
257+
url: "https://github.com/test/repo",
258+
wantErr: false, // May fail in CI but function should be called
259+
},
260+
{
261+
name: "macOS platform",
262+
platform: "darwin",
263+
url: "https://github.com/test/repo",
264+
wantErr: false, // May fail in CI but function should be called
265+
},
266+
{
267+
name: "Windows platform",
268+
platform: "windows",
269+
url: "https://github.com/test/repo",
270+
wantErr: false, // May fail in CI but function should be called
271+
},
272+
{
273+
name: "Unsupported platform - FreeBSD",
274+
platform: "freebsd",
275+
url: "https://github.com/test/repo",
276+
wantErr: true,
277+
errMsg: "unsupported platform: freebsd",
278+
},
279+
{
280+
name: "Unsupported platform - Plan9",
281+
platform: "plan9",
282+
url: "https://github.com/test/repo",
283+
wantErr: true,
284+
errMsg: "unsupported platform: plan9",
285+
},
256286
}
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-
}
287+
288+
for _, tt := range tests {
289+
t.Run(tt.name, func(t *testing.T) {
290+
// Mock the platform
291+
getPlatform = func() string {
292+
return tt.platform
293+
}
294+
295+
err := openURLInBrowser(tt.url)
296+
297+
if tt.wantErr {
298+
if err == nil {
299+
t.Errorf("openURLInBrowser() expected error for platform %s, got nil", tt.platform)
300+
} else if err.Error() != tt.errMsg {
301+
t.Errorf("openURLInBrowser() error = %v, want %s", err, tt.errMsg)
302+
}
303+
} else {
304+
// For supported platforms, the function should be called
305+
// It might fail due to missing commands in CI, but that's expected
306+
t.Logf("openURLInBrowser() for platform %s returned: %v", tt.platform, err)
307+
}
308+
})
270309
}
271310
}

0 commit comments

Comments
 (0)