github.com/chanzuckerberg/afero@v1.1.1/ioutil_test.go (about)

     1  // ©2015 The Go Authors
     2  // Copyright ©2015 Steve Francia <spf@spf13.com>
     3  //
     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
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    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.
    15  
    16  package afero
    17  
    18  import "testing"
    19  
    20  func checkSizePath(t *testing.T, path string, size int64) {
    21  	dir, err := testFS.Stat(path)
    22  	if err != nil {
    23  		t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
    24  	}
    25  	if dir.Size() != size {
    26  		t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
    27  	}
    28  }
    29  
    30  func TestReadFile(t *testing.T) {
    31  	testFS = &MemMapFs{}
    32  	fsutil := &Afero{Fs: testFS}
    33  
    34  	testFS.Create("this_exists.go")
    35  	filename := "rumpelstilzchen"
    36  	contents, err := fsutil.ReadFile(filename)
    37  	if err == nil {
    38  		t.Fatalf("ReadFile %s: error expected, none found", filename)
    39  	}
    40  
    41  	filename = "this_exists.go"
    42  	contents, err = fsutil.ReadFile(filename)
    43  	if err != nil {
    44  		t.Fatalf("ReadFile %s: %v", filename, err)
    45  	}
    46  
    47  	checkSizePath(t, filename, int64(len(contents)))
    48  }
    49  
    50  func TestWriteFile(t *testing.T) {
    51  	testFS = &MemMapFs{}
    52  	fsutil := &Afero{Fs: testFS}
    53  	f, err := fsutil.TempFile("", "ioutil-test")
    54  	if err != nil {
    55  		t.Fatal(err)
    56  	}
    57  	filename := f.Name()
    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."
    61  
    62  	if err := fsutil.WriteFile(filename, []byte(data), 0644); err != nil {
    63  		t.Fatalf("WriteFile %s: %v", filename, err)
    64  	}
    65  
    66  	contents, err := fsutil.ReadFile(filename)
    67  	if err != nil {
    68  		t.Fatalf("ReadFile %s: %v", filename, err)
    69  	}
    70  
    71  	if string(contents) != data {
    72  		t.Fatalf("contents = %q\nexpected = %q", string(contents), data)
    73  	}
    74  
    75  	// cleanup
    76  	f.Close()
    77  	testFS.Remove(filename) // ignore error
    78  }
    79  
    80  func TestReadDir(t *testing.T) {
    81  	testFS = &MemMapFs{}
    82  	testFS.Mkdir("/i-am-a-dir", 0777)
    83  	testFS.Create("/this_exists.go")
    84  	dirname := "rumpelstilzchen"
    85  	_, err := ReadDir(testFS, dirname)
    86  	if err == nil {
    87  		t.Fatalf("ReadDir %s: error expected, none found", dirname)
    88  	}
    89  
    90  	dirname = ".."
    91  	list, err := ReadDir(testFS, dirname)
    92  	if err != nil {
    93  		t.Fatalf("ReadDir %s: %v", dirname, err)
    94  	}
    95  
    96  	foundFile := false
    97  	foundSubDir := false
    98  	for _, dir := range list {
    99  		switch {
   100  		case !dir.IsDir() && dir.Name() == "this_exists.go":
   101  			foundFile = true
   102  		case dir.IsDir() && dir.Name() == "i-am-a-dir":
   103  			foundSubDir = true
   104  		}
   105  	}
   106  	if !foundFile {
   107  		t.Fatalf("ReadDir %s: this_exists.go file not found", dirname)
   108  	}
   109  	if !foundSubDir {
   110  		t.Fatalf("ReadDir %s: i-am-a-dir directory not found", dirname)
   111  	}
   112  }