1 // ©2015 The Go Authors
2 // Copyright ©2015 Steve Francia <spf@spf13.com>
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
20 func checkSizePath(t *testing.T, path string, size int64) {
21 dir, err := testFS.Stat(path)
23 t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
25 if dir.Size() != size {
26 t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
30 func TestReadFile(t *testing.T) {
32 fsutil := &Afero{Fs: testFS}
34 testFS.Create("this_exists.go")
35 filename := "rumpelstilzchen"
36 contents, err := fsutil.ReadFile(filename)
38 t.Fatalf("ReadFile %s: error expected, none found", filename)
41 filename = "this_exists.go"
42 contents, err = fsutil.ReadFile(filename)
44 t.Fatalf("ReadFile %s: %v", filename, err)
47 checkSizePath(t, filename, int64(len(contents)))
50 func TestWriteFile(t *testing.T) {
52 fsutil := &Afero{Fs: testFS}
53 f, err := fsutil.TempFile("", "ioutil-test")
58 data := "Programming today is a race between software engineers striving to " +
59 "build bigger and better idiot-proof programs, and the Universe trying " +
60 "to produce bigger and better idiots. So far, the Universe is winning."
62 if err := fsutil.WriteFile(filename, []byte(data), 0644); err != nil {
63 t.Fatalf("WriteFile %s: %v", filename, err)
66 contents, err := fsutil.ReadFile(filename)
68 t.Fatalf("ReadFile %s: %v", filename, err)
71 if string(contents) != data {
72 t.Fatalf("contents = %q\nexpected = %q", string(contents), data)
77 testFS.Remove(filename) // ignore error
80 func TestReadDir(t *testing.T) {
82 testFS.Mkdir("/i-am-a-dir", 0777)
83 testFS.Create("/this_exists.go")
84 dirname := "rumpelstilzchen"
85 _, err := ReadDir(testFS, dirname)
87 t.Fatalf("ReadDir %s: error expected, none found", dirname)
91 list, err := ReadDir(testFS, dirname)
93 t.Fatalf("ReadDir %s: %v", dirname, err)
98 for _, dir := range list {
100 case !dir.IsDir() && dir.Name() == "this_exists.go":
102 case dir.IsDir() && dir.Name() == "i-am-a-dir":
107 t.Fatalf("ReadDir %s: this_exists.go file not found", dirname)
110 t.Fatalf("ReadDir %s: i-am-a-dir directory not found", dirname)