Source file
src/os/read_test.go
1
2
3
4
5 package os_test
6
7 import (
8 "bytes"
9 . "os"
10 "path/filepath"
11 "runtime"
12 "testing"
13 )
14
15 func checkNamedSize(t *testing.T, path string, size int64) {
16 dir, err := Stat(path)
17 if err != nil {
18 t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
19 }
20 if dir.Size() != size {
21 t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
22 }
23 }
24
25 func TestReadFile(t *testing.T) {
26 t.Parallel()
27
28 filename := "rumpelstilzchen"
29 contents, err := ReadFile(filename)
30 if err == nil {
31 t.Fatalf("ReadFile %s: error expected, none found", filename)
32 }
33
34 filename = "read_test.go"
35 contents, err = ReadFile(filename)
36 if err != nil {
37 t.Fatalf("ReadFile %s: %v", filename, err)
38 }
39
40 checkNamedSize(t, filename, int64(len(contents)))
41 }
42
43 func TestWriteFile(t *testing.T) {
44 t.Parallel()
45
46 f, err := CreateTemp("", "os-test")
47 if err != nil {
48 t.Fatal(err)
49 }
50 defer f.Close()
51 defer Remove(f.Name())
52
53 msg := "Programming today is a race between software engineers striving to " +
54 "build bigger and better idiot-proof programs, and the Universe trying " +
55 "to produce bigger and better idiots. So far, the Universe is winning."
56
57 if err := WriteFile(f.Name(), []byte(msg), 0644); err != nil {
58 t.Fatalf("WriteFile %s: %v", f.Name(), err)
59 }
60
61 data, err := ReadFile(f.Name())
62 if err != nil {
63 t.Fatalf("ReadFile %s: %v", f.Name(), err)
64 }
65
66 if string(data) != msg {
67 t.Fatalf("ReadFile: wrong data:\nhave %q\nwant %q", string(data), msg)
68 }
69 }
70
71 func TestReadOnlyWriteFile(t *testing.T) {
72 if Getuid() == 0 {
73 t.Skipf("Root can write to read-only files anyway, so skip the read-only test.")
74 }
75 if runtime.GOOS == "wasip1" {
76 t.Skip("no support for file permissions on " + runtime.GOOS)
77 }
78 t.Parallel()
79
80
81 filename := filepath.Join(t.TempDir(), "blurp.txt")
82
83 shmorp := []byte("shmorp")
84 florp := []byte("florp")
85 err := WriteFile(filename, shmorp, 0444)
86 if err != nil {
87 t.Fatalf("WriteFile %s: %v", filename, err)
88 }
89 err = WriteFile(filename, florp, 0444)
90 if err == nil {
91 t.Fatalf("Expected an error when writing to read-only file %s", filename)
92 }
93 got, err := ReadFile(filename)
94 if err != nil {
95 t.Fatalf("ReadFile %s: %v", filename, err)
96 }
97 if !bytes.Equal(got, shmorp) {
98 t.Fatalf("want %s, got %s", shmorp, got)
99 }
100 }
101
102 func TestReadDir(t *testing.T) {
103 t.Parallel()
104
105 dirname := "rumpelstilzchen"
106 _, err := ReadDir(dirname)
107 if err == nil {
108 t.Fatalf("ReadDir %s: error expected, none found", dirname)
109 }
110
111 dirname = "."
112 list, err := ReadDir(dirname)
113 if err != nil {
114 t.Fatalf("ReadDir %s: %v", dirname, err)
115 }
116
117 foundFile := false
118 foundSubDir := false
119 for _, dir := range list {
120 switch {
121 case !dir.IsDir() && dir.Name() == "read_test.go":
122 foundFile = true
123 case dir.IsDir() && dir.Name() == "exec":
124 foundSubDir = true
125 }
126 }
127 if !foundFile {
128 t.Fatalf("ReadDir %s: read_test.go file not found", dirname)
129 }
130 if !foundSubDir {
131 t.Fatalf("ReadDir %s: exec directory not found", dirname)
132 }
133 }
134
View as plain text