github.com/IBM/fsgo@v0.0.0-20220920202152-e16fd2119d49/ioutil_test.go (about)

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