github.com/LandonTClipp/afero@v1.3.6-0.20200907052150-97f9d166c7a3/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 (
    19  	"path/filepath"
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  func checkSizePath(t *testing.T, path string, size int64) {
    25  	dir, err := testFS.Stat(path)
    26  	if err != nil {
    27  		t.Fatalf("Stat %q (looking for size %d): %s", path, size, err)
    28  	}
    29  	if dir.Size() != size {
    30  		t.Errorf("Stat %q: size %d want %d", path, dir.Size(), size)
    31  	}
    32  }
    33  
    34  func TestReadFile(t *testing.T) {
    35  	testFS = &MemMapFs{}
    36  	fsutil := &Afero{Fs: testFS}
    37  
    38  	testFS.Create("this_exists.go")
    39  	filename := "rumpelstilzchen"
    40  	contents, err := fsutil.ReadFile(filename)
    41  	if err == nil {
    42  		t.Fatalf("ReadFile %s: error expected, none found", filename)
    43  	}
    44  
    45  	filename = "this_exists.go"
    46  	contents, err = fsutil.ReadFile(filename)
    47  	if err != nil {
    48  		t.Fatalf("ReadFile %s: %v", filename, err)
    49  	}
    50  
    51  	checkSizePath(t, filename, int64(len(contents)))
    52  }
    53  
    54  func TestWriteFile(t *testing.T) {
    55  	testFS = &MemMapFs{}
    56  	fsutil := &Afero{Fs: testFS}
    57  	f, err := fsutil.TempFile("", "ioutil-test")
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	filename := f.Name()
    62  	data := "Programming today is a race between software engineers striving to " +
    63  		"build bigger and better idiot-proof programs, and the Universe trying " +
    64  		"to produce bigger and better idiots. So far, the Universe is winning."
    65  
    66  	if err := fsutil.WriteFile(filename, []byte(data), 0644); err != nil {
    67  		t.Fatalf("WriteFile %s: %v", filename, err)
    68  	}
    69  
    70  	contents, err := fsutil.ReadFile(filename)
    71  	if err != nil {
    72  		t.Fatalf("ReadFile %s: %v", filename, err)
    73  	}
    74  
    75  	if string(contents) != data {
    76  		t.Fatalf("contents = %q\nexpected = %q", string(contents), data)
    77  	}
    78  
    79  	// cleanup
    80  	f.Close()
    81  	testFS.Remove(filename) // ignore error
    82  }
    83  
    84  func TestReadDir(t *testing.T) {
    85  	testFS = &MemMapFs{}
    86  	testFS.Mkdir("/i-am-a-dir", 0777)
    87  	testFS.Create("/this_exists.go")
    88  	dirname := "rumpelstilzchen"
    89  	_, err := ReadDir(testFS, dirname)
    90  	if err == nil {
    91  		t.Fatalf("ReadDir %s: error expected, none found", dirname)
    92  	}
    93  
    94  	dirname = ".."
    95  	list, err := ReadDir(testFS, dirname)
    96  	if err != nil {
    97  		t.Fatalf("ReadDir %s: %v", dirname, err)
    98  	}
    99  
   100  	foundFile := false
   101  	foundSubDir := false
   102  	for _, dir := range list {
   103  		switch {
   104  		case !dir.IsDir() && dir.Name() == "this_exists.go":
   105  			foundFile = true
   106  		case dir.IsDir() && dir.Name() == "i-am-a-dir":
   107  			foundSubDir = true
   108  		}
   109  	}
   110  	if !foundFile {
   111  		t.Fatalf("ReadDir %s: this_exists.go file not found", dirname)
   112  	}
   113  	if !foundSubDir {
   114  		t.Fatalf("ReadDir %s: i-am-a-dir directory not found", dirname)
   115  	}
   116  }
   117  
   118  func TestTempFile(t *testing.T) {
   119  	type args struct {
   120  		dir     string
   121  		pattern string
   122  	}
   123  	tests := map[string]struct {
   124  		args args
   125  		want func(*testing.T, string)
   126  	}{
   127  		"foo": { // simple file name
   128  			args: args{
   129  				dir:     "",
   130  				pattern: "foo",
   131  			},
   132  			want: func(t *testing.T, base string) {
   133  				if !strings.HasPrefix(base, "foo") || len(base) <= len("foo") {
   134  					t.Errorf("TempFile() file = %s, invalid file name", base)
   135  				}
   136  			},
   137  		},
   138  		"foo.bar": { // file name w/ ext
   139  			args: args{
   140  				dir:     "",
   141  				pattern: "foo.bar",
   142  			},
   143  			want: func(t *testing.T, base string) {
   144  				if !strings.HasPrefix(base, "foo.bar") || len(base) <= len("foo.bar") {
   145  					t.Errorf("TempFile() file = %v, invalid file name", base)
   146  				}
   147  			},
   148  		},
   149  		"foo-*.bar": { // file name with wild card
   150  			args: args{
   151  				dir:     "",
   152  				pattern: "foo-*.bar",
   153  			},
   154  			want: func(t *testing.T, base string) {
   155  				if !(strings.HasPrefix(base, "foo-") || strings.HasPrefix(base, "bar")) ||
   156  					len(base) <= len("foo-*.bar") {
   157  					t.Errorf("TempFile() file = %v, invalid file name", base)
   158  				}
   159  			},
   160  		},
   161  	}
   162  	for name, tt := range tests {
   163  		t.Run(name, func(t *testing.T) {
   164  			file, err := TempFile(NewMemMapFs(), tt.args.dir, tt.args.pattern)
   165  			if err != nil {
   166  				t.Errorf("TempFile() error = %v, none expected", err)
   167  				return
   168  			}
   169  			if file == nil {
   170  				t.Errorf("TempFile() file = %v, should not be nil", file)
   171  				return
   172  			}
   173  			tt.want(t, filepath.Base(file.Name()))
   174  		})
   175  	}
   176  }