@@ -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+ }
0 commit comments