1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 func checkSize(t
*testing
.T
, path
string, size
int64) {
14 dir
, err
:= os
.Stat(path
)
16 t
.Fatalf("Stat %q (looking for size %d): %s", path
, size
, err
)
19 t
.Errorf("Stat %q: size %d want %d", path
, dir
.Size
, size
)
23 func TestReadFile(t
*testing
.T
) {
24 filename
:= "rumpelstilzchen"
25 contents
, err
:= ReadFile(filename
)
27 t
.Fatalf("ReadFile %s: error expected, none found", filename
)
30 filename
= "ioutil_test.go"
31 contents
, err
= ReadFile(filename
)
33 t
.Fatalf("ReadFile %s: %v", filename
, err
)
36 checkSize(t
, filename
, int64(len(contents
)))
39 func TestWriteFile(t
*testing
.T
) {
40 filename
:= "_test/rumpelstilzchen"
41 data
:= "Programming today is a race between software engineers striving to " +
42 "build bigger and better idiot-proof programs, and the Universe trying " +
43 "to produce bigger and better idiots. So far, the Universe is winning."
45 if err
:= WriteFile(filename
, []byte(data
), 0644); err
!= nil {
46 t
.Fatalf("WriteFile %s: %v", filename
, err
)
49 contents
, err
:= ReadFile(filename
)
51 t
.Fatalf("ReadFile %s: %v", filename
, err
)
54 if string(contents
) != data
{
55 t
.Fatalf("contents = %q\nexpected = %q", string(contents
), data
)
59 os
.Remove(filename
) // ignore error
63 func TestReadDir(t
*testing
.T
) {
64 dirname
:= "rumpelstilzchen"
65 _
, err
:= ReadDir(dirname
)
67 t
.Fatalf("ReadDir %s: error expected, none found", dirname
)
71 list
, err
:= ReadDir(dirname
)
73 t
.Fatalf("ReadDir %s: %v", dirname
, err
)
78 for _
, dir
:= range list
{
80 case dir
.IsRegular() && dir
.Name
== "ioutil_test.go":
82 case dir
.IsDirectory() && dir
.Name
== "_test":
87 t
.Fatalf("ReadDir %s: test file not found", dirname
)
90 t
.Fatalf("ReadDir %s: _test directory not found", dirname
)