...
Run Format

Source file src/io/ioutil/ioutil_test.go

Documentation: io/ioutil

     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.
     4  
     5  package ioutil
     6  
     7  import (
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func checkSize(t *testing.T, path string, size int64) {
    13  	dir, err := os.Stat(path)
    14  	if err != nil {
    15  		t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
    16  	}
    17  	if dir.Size() != size {
    18  		t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
    19  	}
    20  }
    21  
    22  func TestReadFile(t *testing.T) {
    23  	filename := "rumpelstilzchen"
    24  	contents, err := ReadFile(filename)
    25  	if err == nil {
    26  		t.Fatalf("ReadFile %s: error expected, none found", filename)
    27  	}
    28  
    29  	filename = "ioutil_test.go"
    30  	contents, err = ReadFile(filename)
    31  	if err != nil {
    32  		t.Fatalf("ReadFile %s: %v", filename, err)
    33  	}
    34  
    35  	checkSize(t, filename, int64(len(contents)))
    36  }
    37  
    38  func TestWriteFile(t *testing.T) {
    39  	f, err := TempFile("", "ioutil-test")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	filename := f.Name()
    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."
    47  
    48  	if err := WriteFile(filename, []byte(data), 0644); err != nil {
    49  		t.Fatalf("WriteFile %s: %v", filename, err)
    50  	}
    51  
    52  	contents, err := ReadFile(filename)
    53  	if err != nil {
    54  		t.Fatalf("ReadFile %s: %v", filename, err)
    55  	}
    56  
    57  	if string(contents) != data {
    58  		t.Fatalf("contents = %q\nexpected = %q", string(contents), data)
    59  	}
    60  
    61  	// cleanup
    62  	f.Close()
    63  	os.Remove(filename) // ignore error
    64  }
    65  
    66  func TestReadDir(t *testing.T) {
    67  	dirname := "rumpelstilzchen"
    68  	_, err := ReadDir(dirname)
    69  	if err == nil {
    70  		t.Fatalf("ReadDir %s: error expected, none found", dirname)
    71  	}
    72  
    73  	dirname = ".."
    74  	list, err := ReadDir(dirname)
    75  	if err != nil {
    76  		t.Fatalf("ReadDir %s: %v", dirname, err)
    77  	}
    78  
    79  	foundFile := false
    80  	foundSubDir := false
    81  	for _, dir := range list {
    82  		switch {
    83  		case !dir.IsDir() && dir.Name() == "io_test.go":
    84  			foundFile = true
    85  		case dir.IsDir() && dir.Name() == "ioutil":
    86  			foundSubDir = true
    87  		}
    88  	}
    89  	if !foundFile {
    90  		t.Fatalf("ReadDir %s: io_test.go file not found", dirname)
    91  	}
    92  	if !foundSubDir {
    93  		t.Fatalf("ReadDir %s: ioutil directory not found", dirname)
    94  	}
    95  }
    96  

View as plain text