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.
12 func checkSize(t
*testing
.T
, path
string, size
int64) {
13 dir
, err
:= os
.Stat(path
)
15 t
.Fatalf("Stat %q (looking for size %d): %s", path
, size
, err
)
17 if dir
.Size() != size
{
18 t
.Errorf("Stat %q: size %d want %d", path
, dir
.Size(), size
)
22 func TestReadFile(t
*testing
.T
) {
23 filename
:= "rumpelstilzchen"
24 contents
, err
:= ReadFile(filename
)
26 t
.Fatalf("ReadFile %s: error expected, none found", filename
)
29 filename
= "ioutil_test.go"
30 contents
, err
= ReadFile(filename
)
32 t
.Fatalf("ReadFile %s: %v", filename
, err
)
35 checkSize(t
, filename
, int64(len(contents
)))
38 func TestWriteFile(t
*testing
.T
) {
39 f
, err
:= TempFile("", "ioutil-test")
44 data
:= "Programming today is a race between software engineers striving to " +
45 "build bigger and better idiot-proof programs, and the Universe trying " +
46 "to produce bigger and better idiots. So far, the Universe is winning."
48 if err
:= WriteFile(filename
, []byte(data
), 0644); err
!= nil {
49 t
.Fatalf("WriteFile %s: %v", filename
, err
)
52 contents
, err
:= ReadFile(filename
)
54 t
.Fatalf("ReadFile %s: %v", filename
, err
)
57 if string(contents
) != data
{
58 t
.Fatalf("contents = %q\nexpected = %q", string(contents
), data
)
63 os
.Remove(filename
) // ignore error
66 func TestReadDir(t
*testing
.T
) {
67 dirname
:= "rumpelstilzchen"
68 _
, err
:= ReadDir(dirname
)
70 t
.Fatalf("ReadDir %s: error expected, none found", dirname
)
73 /* Does not work in gccgo testing environment.
75 list, err := ReadDir(dirname)
77 t.Fatalf("ReadDir %s: %v", dirname, err)
82 for _, dir := range list {
84 case !dir.IsDir() && dir.Name() == "io_test.go":
86 case dir.IsDir() && dir.Name() == "ioutil":
91 t.Fatalf("ReadDir %s: io_test.go file not found", dirname)
94 t.Fatalf("ReadDir %s: ioutil directory not found", dirname)