github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/os/dir_test.go (about) 1 //go:build darwin || (linux && !baremetal && !js && !wasip1 && !386 && !arm) 2 3 package os_test 4 5 import ( 6 . "os" 7 "testing" 8 ) 9 10 func testReaddirnames(dir string, contents []string, t *testing.T) { 11 file, err := Open(dir) 12 if err != nil { 13 t.Fatalf("open %q failed: %v", dir, err) 14 } 15 defer file.Close() 16 s, err2 := file.Readdirnames(-1) 17 if err2 != nil { 18 t.Fatalf("Readdirnames %q failed: %v", dir, err2) 19 } 20 for _, m := range contents { 21 found := false 22 for _, n := range s { 23 if n == "." || n == ".." { 24 t.Errorf("got %q in directory", n) 25 } 26 if !equal(m, n) { 27 continue 28 } 29 if found { 30 t.Error("present twice:", m) 31 } 32 found = true 33 } 34 if !found { 35 t.Error("could not find", m) 36 } 37 } 38 if s == nil { 39 t.Error("Readdirnames returned nil instead of empty slice") 40 } 41 } 42 43 func testReaddir(dir string, contents []string, t *testing.T) { 44 file, err := Open(dir) 45 if err != nil { 46 t.Fatalf("open %q failed: %v", dir, err) 47 } 48 defer file.Close() 49 s, err2 := file.Readdir(-1) 50 if err2 != nil { 51 t.Fatalf("Readdir %q failed: %v", dir, err2) 52 } 53 for _, m := range contents { 54 found := false 55 for _, n := range s { 56 if n.Name() == "." || n.Name() == ".." { 57 t.Errorf("got %q in directory", n.Name()) 58 } 59 if !equal(m, n.Name()) { 60 continue 61 } 62 if found { 63 t.Error("present twice:", m) 64 } 65 found = true 66 } 67 if !found { 68 t.Error("could not find", m) 69 } 70 } 71 if s == nil { 72 t.Error("Readdir returned nil instead of empty slice") 73 } 74 } 75 76 func testReadDir(dir string, contents []string, t *testing.T) { 77 file, err := Open(dir) 78 if err != nil { 79 t.Fatalf("open %q failed: %v", dir, err) 80 } 81 defer file.Close() 82 s, err2 := file.ReadDir(-1) 83 if err2 != nil { 84 t.Fatalf("ReadDir %q failed: %v", dir, err2) 85 } 86 for _, m := range contents { 87 found := false 88 for _, n := range s { 89 if n.Name() == "." || n.Name() == ".." { 90 t.Errorf("got %q in directory", n) 91 } 92 if !equal(m, n.Name()) { 93 continue 94 } 95 if found { 96 t.Error("present twice:", m) 97 } 98 found = true 99 lstat, err := Lstat(dir + "/" + m) 100 if err != nil { 101 t.Fatal(err) 102 } 103 if n.IsDir() != lstat.IsDir() { 104 t.Errorf("%s: IsDir=%v, want %v", m, n.IsDir(), lstat.IsDir()) 105 } 106 if n.Type() != lstat.Mode().Type() { 107 t.Errorf("%s: IsDir=%v, want %v", m, n.Type(), lstat.Mode().Type()) 108 } 109 info, err := n.Info() 110 if err != nil { 111 t.Errorf("%s: Info: %v", m, err) 112 continue 113 } 114 if !SameFile(info, lstat) { 115 t.Errorf("%s: Info: SameFile(info, lstat) = false", m) 116 } 117 } 118 if !found { 119 t.Error("could not find", m) 120 } 121 } 122 if s == nil { 123 t.Error("ReadDir returned nil instead of empty slice") 124 } 125 } 126 127 func TestFileReaddirnames(t *testing.T) { 128 testReaddirnames(".", dot, t) 129 testReaddirnames(TempDir(), nil, t) 130 } 131 132 func TestFileReaddir(t *testing.T) { 133 testReaddir(".", dot, t) 134 testReaddir(TempDir(), nil, t) 135 } 136 137 func TestFileReadDir(t *testing.T) { 138 testReadDir(".", dot, t) 139 testReadDir(TempDir(), nil, t) 140 } 141 142 func TestReadDir(t *testing.T) { 143 dirname := "rumpelstilzchen" 144 _, err := ReadDir(dirname) 145 if err == nil { 146 t.Fatalf("ReadDir %s: error expected, none found", dirname) 147 } 148 149 dirname = "testdata" 150 list, err := ReadDir(dirname) 151 if err != nil { 152 t.Fatalf("ReadDir %s: %v", dirname, err) 153 } 154 155 foundFile := false 156 foundSubDir := false 157 for _, dir := range list { 158 switch { 159 case !dir.IsDir() && dir.Name() == "hello": 160 foundFile = true 161 case dir.IsDir() && dir.Name() == "issue37161": 162 foundSubDir = true 163 } 164 } 165 if !foundFile { 166 t.Fatalf("ReadDir %s: hello file not found", dirname) 167 } 168 if !foundSubDir { 169 t.Fatalf("ReadDir %s: testdata directory not found", dirname) 170 } 171 } 172 173 // TestReadNonDir just verifies that opening a non-directory returns an error. 174 func TestReadNonDir(t *testing.T) { 175 // Use filename of this source file; it is known to exist, and go tests run in source tree. 176 dirname := "dir_test.go" 177 _, err := ReadDir(dirname) 178 if err == nil { 179 t.Fatalf("ReadDir %s: error on non-dir expected, none found", dirname) 180 } 181 }