@@ -238,34 +238,73 @@ func Test_openWithWindowsStart(t *testing.T) {
238238}
239239
240240func 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