Skip to content

Commit 70f4717

Browse files
committed
os: use relative paths in a test dir in TestOpenError
Refactor TestOpenError to use relative paths in test cases, in preparation for extending it to test os.Root. Use a test temporary directory instead of system directory with presumed-known contents. Move the testcase type and case definitions inline with the test. For #67002 Change-Id: Idc53dd9fcecf763d3e4eb3b4643032e3003d7ef4 Reviewed-on: https://go-review.googlesource.com/c/go/+/620157 Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent cbdb354 commit 70f4717

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/os/os_test.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,35 +1777,36 @@ func TestSeekError(t *testing.T) {
17771777
}
17781778
}
17791779

1780-
type openErrorTest struct {
1781-
path string
1782-
mode int
1783-
error error
1784-
}
1780+
func TestOpenError(t *testing.T) {
1781+
t.Parallel()
1782+
1783+
dir := t.TempDir()
1784+
if err := WriteFile(filepath.Join(dir, "is-a-file"), nil, 0o666); err != nil {
1785+
t.Fatal(err)
1786+
}
1787+
if err := Mkdir(filepath.Join(dir, "is-a-dir"), 0o777); err != nil {
1788+
t.Fatal(err)
1789+
}
17851790

1786-
var openErrorTests = []openErrorTest{
1787-
{
1788-
sfdir + "/no-such-file",
1791+
for _, tt := range []struct {
1792+
path string
1793+
mode int
1794+
error error
1795+
}{{
1796+
"no-such-file",
17891797
O_RDONLY,
17901798
syscall.ENOENT,
1791-
},
1792-
{
1793-
sfdir,
1799+
}, {
1800+
"is-a-dir",
17941801
O_WRONLY,
17951802
syscall.EISDIR,
1796-
},
1797-
{
1798-
sfdir + "/" + sfname + "/no-such-file",
1803+
}, {
1804+
"is-a-file/no-such-file",
17991805
O_WRONLY,
18001806
syscall.ENOTDIR,
1801-
},
1802-
}
1803-
1804-
func TestOpenError(t *testing.T) {
1805-
t.Parallel()
1806-
1807-
for _, tt := range openErrorTests {
1808-
f, err := OpenFile(tt.path, tt.mode, 0)
1807+
}} {
1808+
path := filepath.Join(dir, tt.path)
1809+
f, err := OpenFile(path, tt.mode, 0)
18091810
if err == nil {
18101811
t.Errorf("Open(%q, %d) succeeded", tt.path, tt.mode)
18111812
f.Close()

0 commit comments

Comments
 (0)