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.
15 func checkNamedSize(t
*testing
.T
, path
string, size
int64) {
16 dir
, err
:= Stat(path
)
18 t
.Fatalf("Stat %q (looking for size %d): %s", path
, size
, err
)
20 if dir
.Size() != size
{
21 t
.Errorf("Stat %q: size %d want %d", path
, dir
.Size(), size
)
25 func TestReadFile(t
*testing
.T
) {
26 filename
:= "rumpelstilzchen"
27 contents
, err
:= ReadFile(filename
)
29 t
.Fatalf("ReadFile %s: error expected, none found", filename
)
32 filename
= "read_test.go"
33 contents
, err
= ReadFile(filename
)
35 t
.Fatalf("ReadFile %s: %v", filename
, err
)
38 checkNamedSize(t
, filename
, int64(len(contents
)))
41 func TestWriteFile(t
*testing
.T
) {
42 f
, err
:= CreateTemp("", "ioutil-test")
47 defer Remove(f
.Name())
49 msg
:= "Programming today is a race between software engineers striving to " +
50 "build bigger and better idiot-proof programs, and the Universe trying " +
51 "to produce bigger and better idiots. So far, the Universe is winning."
53 if err
:= WriteFile(f
.Name(), []byte(msg
), 0644); err
!= nil {
54 t
.Fatalf("WriteFile %s: %v", f
.Name(), err
)
57 data
, err
:= ReadFile(f
.Name())
59 t
.Fatalf("ReadFile %s: %v", f
.Name(), err
)
62 if string(data
) != msg
{
63 t
.Fatalf("ReadFile: wrong data:\nhave %q\nwant %q", string(data
), msg
)
67 func TestReadOnlyWriteFile(t
*testing
.T
) {
69 t
.Skipf("Root can write to read-only files anyway, so skip the read-only test.")
72 // We don't want to use CreateTemp directly, since that opens a file for us as 0600.
73 tempDir
, err
:= MkdirTemp("", t
.Name())
77 defer RemoveAll(tempDir
)
78 filename
:= filepath
.Join(tempDir
, "blurp.txt")
80 shmorp
:= []byte("shmorp")
81 florp
:= []byte("florp")
82 err
= WriteFile(filename
, shmorp
, 0444)
84 t
.Fatalf("WriteFile %s: %v", filename
, err
)
86 err
= WriteFile(filename
, florp
, 0444)
88 t
.Fatalf("Expected an error when writing to read-only file %s", filename
)
90 got
, err
:= ReadFile(filename
)
92 t
.Fatalf("ReadFile %s: %v", filename
, err
)
94 if !bytes
.Equal(got
, shmorp
) {
95 t
.Fatalf("want %s, got %s", shmorp
, got
)
99 func TestReadDir(t
*testing
.T
) {
100 dirname
:= "rumpelstilzchen"
101 _
, err
:= ReadDir(dirname
)
103 t
.Fatalf("ReadDir %s: error expected, none found", dirname
)
107 list
, err
:= ReadDir(dirname
)
109 t
.Fatalf("ReadDir %s: %v", dirname
, err
)
114 for _
, dir
:= range list
{
116 case !dir
.IsDir() && dir
.Name() == "read_test.go":
118 case dir
.IsDir() && dir
.Name() == "exec":
123 t
.Fatalf("ReadDir %s: read_test.go file not found", dirname
)
126 // This doesn't work in the gofrontend testsuite framework.
127 if runtime
.Compiler
== "gc" {
128 t
.Fatalf("ReadDir %s: exec directory not found", dirname
)