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