11package cmd
22
33import (
4+ "os/exec"
45 "runtime"
56 "testing"
67)
78
89// mockOpenURL is a mock function for testing
910var mockOpenURL func (string ) error
1011
12+ func withNoopCommandRunner (t * testing.T ) {
13+ t .Helper ()
14+ t .Cleanup (func () {
15+ commandRunner = func (name string , args ... string ) error {
16+ cmd := exec .Command (name , args ... )
17+ // Redirect stdout and stderr to /dev/null to suppress output
18+ cmd .Stdout = nil
19+ cmd .Stderr = nil
20+ return cmd .Start ()
21+ }
22+ })
23+ commandRunner = func (name string , args ... string ) error {
24+ return nil
25+ }
26+ }
27+
1128func Test_openURLInBrowser (t * testing.T ) {
1229 // Save original function
1330 original := OpenURLInBrowser
@@ -76,6 +93,7 @@ func Test_openWithXdgOpen(t *testing.T) {
7693
7794 for _ , tt := range tests {
7895 t .Run (tt .name , func (t * testing.T ) {
96+ withNoopCommandRunner (t )
7997 err := openWithXdgOpen (tt .url )
8098 if (err != nil ) != tt .wantErr {
8199 t .Errorf ("openWithXdgOpen() error = %v, wantErr %v" , err , tt .wantErr )
@@ -104,6 +122,7 @@ func Test_openWithMacOSOpen(t *testing.T) {
104122
105123 for _ , tt := range tests {
106124 t .Run (tt .name , func (t * testing.T ) {
125+ withNoopCommandRunner (t )
107126 err := openWithMacOSOpen (tt .url )
108127 if (err != nil ) != tt .wantErr {
109128 t .Errorf ("openWithMacOSOpen() error = %v, wantErr %v" , err , tt .wantErr )
@@ -139,6 +158,7 @@ func Test_openURLInBrowser_PlatformSpecific(t *testing.T) {
139158 for _ , tt := range tests {
140159 t .Run (tt .name , func (t * testing.T ) {
141160 // Test the actual platform-specific implementation
161+ withNoopCommandRunner (t )
142162 err := openURLInBrowser (tt .url )
143163 if (err != nil ) != tt .expectError {
144164 t .Errorf ("openURLInBrowser() error = %v, expectError %v" , err , tt .expectError )
@@ -175,11 +195,12 @@ func Test_openURLInBrowser_AllPlatforms(t *testing.T) {
175195 // Save the original runtime.GOOS
176196 originalGOOS := runtime .GOOS
177197 // We can't actually change runtime.GOOS, but we can test the functions directly
178-
198+
179199 switch tt .platform {
180200 case "linux" :
181201 // Test openWithXdgOpen directly if not on Linux
182202 if runtime .GOOS != "linux" {
203+ withNoopCommandRunner (t )
183204 err := openWithXdgOpen (tt .url )
184205 // On non-Linux systems, this should fail as xdg-open doesn't exist
185206 if err == nil {
@@ -189,6 +210,7 @@ func Test_openURLInBrowser_AllPlatforms(t *testing.T) {
189210 case "darwin" :
190211 // Test openWithMacOSOpen directly if not on macOS
191212 if runtime .GOOS != "darwin" {
213+ withNoopCommandRunner (t )
192214 err := openWithMacOSOpen (tt .url )
193215 // On non-macOS systems, this should fail as open command might not exist
194216 if err == nil {
@@ -198,6 +220,7 @@ func Test_openURLInBrowser_AllPlatforms(t *testing.T) {
198220 case "windows" :
199221 // Test openWithWindowsStart directly if not on Windows
200222 if runtime .GOOS != "windows" {
223+ withNoopCommandRunner (t )
201224 err := openWithWindowsStart (tt .url )
202225 // On non-Windows systems, this should fail as cmd doesn't exist
203226 if err == nil {
@@ -258,14 +281,14 @@ func Test_openURLInBrowser_UnsupportedPlatform(t *testing.T) {
258281 wantErr : false , // May fail in CI but function should be called
259282 },
260283 {
261- name : "macOS platform" ,
284+ name : "macOS platform" ,
262285 platform : "darwin" ,
263286 url : "https://github.com/test/repo" ,
264287 wantErr : false , // May fail in CI but function should be called
265288 },
266289 {
267290 name : "Windows platform" ,
268- platform : "windows" ,
291+ platform : "windows" ,
269292 url : "https://github.com/test/repo" ,
270293 wantErr : false , // May fail in CI but function should be called
271294 },
@@ -291,9 +314,10 @@ func Test_openURLInBrowser_UnsupportedPlatform(t *testing.T) {
291314 getPlatform = func () string {
292315 return tt .platform
293316 }
317+ withNoopCommandRunner (t )
294318
295319 err := openURLInBrowser (tt .url )
296-
320+
297321 if tt .wantErr {
298322 if err == nil {
299323 t .Errorf ("openURLInBrowser() expected error for platform %s, got nil" , tt .platform )
0 commit comments