Skip to content

Commit 0f8a22f

Browse files
committed
Enhance OS-specific browser tests, PR feedback
This commit addresses @babakks feedback in the PR of splitting out Windows and Unix/Linux specific browser tests from the shared common tests as well as minor restructuring of test setup / teardown logic.
1 parent df956a6 commit 0f8a22f

File tree

4 files changed

+141
-40
lines changed

4 files changed

+141
-40
lines changed

pkg/browser/browser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ func isPossibleProtocol(u string) bool {
101101
return true
102102
}
103103

104-
// Disallow URLs that match existing files or directorys on the filesystem
105-
if fileInfo, _ := os.Lstat(u); fileInfo != nil {
104+
// Disallow URLs using alternative `file:` protocol not located previously
105+
if strings.HasPrefix(u, "file:") {
106106
return false
107107
}
108108

109-
// Disallow URLs using alternative `file:` protocol not located previously
110-
if strings.HasPrefix(u, "file:") {
109+
// Disallow URLs that match existing files or directories on the filesystem
110+
if fileInfo, _ := os.Lstat(u); fileInfo != nil {
111111
return false
112112
}
113113

pkg/browser/browser_other_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:build !windows
2+
3+
package browser
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"os"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// TestBrowseOthers covers unique Unix and Linux specific test cases that go beyond TestBrowse.
16+
func TestBrowseOthers(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
url string
20+
launcher string
21+
expected string
22+
setup func(*testing.T) error
23+
wantErr bool
24+
}{
25+
{
26+
name: "Explicit absolute `file://` URL errors",
27+
url: "file:///System/Applications/Calculator.app",
28+
wantErr: true,
29+
},
30+
{
31+
name: "Implicit absolute file URL errors",
32+
url: "/bin/sh",
33+
wantErr: true,
34+
},
35+
{
36+
name: "Implicit absolute directory URL errors",
37+
url: "/System/Applications/Calculator.app",
38+
wantErr: true,
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
if tt.setup != nil {
45+
err := tt.setup(t)
46+
require.NoError(t, err)
47+
}
48+
49+
stdout := &bytes.Buffer{}
50+
stderr := &bytes.Buffer{}
51+
b := Browser{launcher: tt.launcher, stdout: stdout, stderr: stderr}
52+
err := b.browse(tt.url, []string{"GH_WANT_HELPER_PROCESS=1", fmt.Sprintf("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR"))})
53+
54+
if tt.wantErr {
55+
require.Error(t, err)
56+
} else {
57+
require.NoError(t, err)
58+
assert.Equal(t, tt.expected, stdout.String())
59+
assert.Equal(t, "", stderr.String())
60+
}
61+
})
62+
}
63+
}

pkg/browser/browser_test.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,75 +59,60 @@ func TestBrowse(t *testing.T) {
5959
launcher: fmt.Sprintf("%q -test.run=TestHelperProcess -- implicit https", os.Args[0]),
6060
expected: "[implicit https github.com]",
6161
},
62-
{
63-
name: "Explicit absolute `file://` URL errors",
64-
url: "file:///System/Applications/Calculator.app",
65-
wantErr: true,
66-
},
67-
{
68-
name: "Implicit absolute file URL errors",
69-
url: "/bin/sh",
70-
wantErr: true,
71-
},
72-
{
73-
name: "Implicit absolute directory URL errors",
74-
url: "/System/Applications/Calculator.app",
75-
wantErr: true,
76-
},
77-
{
78-
name: "Explicit absolute Windows file URL errors",
79-
url: `C:\Windows\System32\calc.exe`,
80-
wantErr: true,
81-
},
8262
{
8363
name: "Implicit relative file URL errors",
8464
url: "poc.command",
8565
setup: func(t *testing.T) error {
86-
// Capture current working directory for test cleanup
66+
// Setup a temporary directory to stage content and execute the test within,
67+
// ensure the test's original working directory is restored after.
8768
cwd, err := os.Getwd()
8869
if err != nil {
8970
return err
9071
}
9172

92-
// Create temporary directory containing relative executable for testing
9373
tempDir := t.TempDir()
9474
err = os.Chdir(tempDir)
9575
if err != nil {
9676
return err
9777
}
9878

79+
t.Cleanup(func() {
80+
_ = os.Chdir(cwd)
81+
})
82+
83+
// Create content for local file URL testing
9984
path := filepath.Join(tempDir, "poc.command")
10085
err = os.WriteFile(path, []byte("#!/bin/bash\necho hello"), 0755)
10186
if err != nil {
10287
return err
10388
}
10489

105-
// Restore original working directory after test
106-
t.Cleanup(func() {
107-
_ = os.Chdir(cwd)
108-
})
109-
11090
return nil
11191
},
11292
wantErr: true,
11393
},
11494
{
11595
name: "Implicit relative directory URL errors",
116-
url: "poc.command",
96+
url: "Fake.app",
11797
setup: func(t *testing.T) error {
118-
// Capture current working directory for test cleanup
98+
// Setup a temporary directory to stage content and execute the test within,
99+
// ensure the test's original working directory is restored after.
119100
cwd, err := os.Getwd()
120101
if err != nil {
121102
return err
122103
}
123104

124-
// Create temporary directory containing relative executable for testing
125105
tempDir := t.TempDir()
126106
err = os.Chdir(tempDir)
127107
if err != nil {
128108
return err
129109
}
130110

111+
t.Cleanup(func() {
112+
_ = os.Chdir(cwd)
113+
})
114+
115+
// Create content for local directory URL testing
131116
path := filepath.Join(tempDir, "Fake.app")
132117
err = os.Mkdir(path, 0755)
133118
if err != nil {
@@ -140,11 +125,6 @@ func TestBrowse(t *testing.T) {
140125
return err
141126
}
142127

143-
// Restore original working directory after test
144-
t.Cleanup(func() {
145-
_ = os.Chdir(cwd)
146-
})
147-
148128
return nil
149129
},
150130
wantErr: true,
@@ -161,7 +141,7 @@ func TestBrowse(t *testing.T) {
161141
stdout := &bytes.Buffer{}
162142
stderr := &bytes.Buffer{}
163143
b := Browser{launcher: tt.launcher, stdout: stdout, stderr: stderr}
164-
err := b.browse(tt.url, []string{"GH_WANT_HELPER_PROCESS=1"})
144+
err := b.browse(tt.url, []string{"GH_WANT_HELPER_PROCESS=1", fmt.Sprintf("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR"))})
165145

166146
if tt.wantErr {
167147
require.Error(t, err)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build windows
2+
3+
package browser
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
"os"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
// TestBrowseWindows covers unique Windows-specific test cases that go beyond TestBrowse.
16+
func TestBrowseWindows(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
url string
20+
launcher string
21+
expected string
22+
setup func(*testing.T) error
23+
wantErr bool
24+
}{
25+
{
26+
name: "Explicit absolute Windows file URL errors",
27+
url: `C:\Windows\System32\calc.exe`,
28+
wantErr: true,
29+
},
30+
{
31+
name: "Explicit absolute Windows directory URL errors",
32+
url: `C:\Windows\System32`,
33+
wantErr: true,
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
if tt.setup != nil {
40+
err := tt.setup(t)
41+
require.NoError(t, err)
42+
}
43+
44+
stdout := &bytes.Buffer{}
45+
stderr := &bytes.Buffer{}
46+
b := Browser{launcher: tt.launcher, stdout: stdout, stderr: stderr}
47+
err := b.browse(tt.url, []string{"GH_WANT_HELPER_PROCESS=1", fmt.Sprintf("GOCOVERDIR=%s", os.Getenv("GOCOVERDIR"))})
48+
49+
if tt.wantErr {
50+
require.Error(t, err)
51+
} else {
52+
require.NoError(t, err)
53+
assert.Equal(t, tt.expected, stdout.String())
54+
assert.Equal(t, "", stderr.String())
55+
}
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)