Skip to content

Commit f5ab731

Browse files
committed
os: add TestReadNonDir, fix problem it found on darwin
1 parent f9c588c commit f5ab731

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/os/dir_darwin.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ func darwinOpenDir(fd syscallFd) (uintptr, string, error) {
140140
break
141141
}
142142
}
143+
// Without this test, darwinOpenDir would happily open non-directories, causing trouble in at least Glob().
144+
// Upstream doesn't test dir for nil here, but dtruss shows stat is being called behind the scenes.
145+
// Is syscall.Fdopendir not checking for errno properly?
146+
if err == nil && dir == 0 {
147+
err = syscall.ENOTDIR
148+
}
143149
if err != nil {
144150
syscall.Close(fd2)
145151
return 0, "fdopendir", err

src/os/dir_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,14 @@ func TestReadDir(t *testing.T) {
169169
t.Fatalf("ReadDir %s: testdata directory not found", dirname)
170170
}
171171
}
172+
173+
// TestReadNonDir is a tinygo regression test to verify that opening a non-directory returns an error.
174+
// See note re f.Stat() in dir_darwin.go.
175+
func TestReadNonDir(t *testing.T) {
176+
// Use filename of this source file, it is known to exist, and go tests run in source tree.
177+
dirname := "dir_test.go"
178+
_, err := ReadDir(dirname)
179+
if err == nil {
180+
t.Fatalf("ReadDir %s: error on non-dir expected, none found", dirname)
181+
}
182+
}

0 commit comments

Comments
 (0)