github.com/aarzilli/tools@v0.0.0-20151123112009-0d27094f75e0/os/fsi/tests/suite_helpers_test.go (about)

     1  package tests
     2  
     3  // Copyright © 2014 Steve Francia <spf@spf13.com>.
     4  // Copyright 2009 The Go Authors. All rights reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  import (
    18  	"io"
    19  	"io/ioutil"
    20  	"log"
    21  	"path"
    22  	"runtime"
    23  	"strings"
    24  	"testing"
    25  
    26  	"appengine/aetest"
    27  
    28  	"github.com/pbberlin/tools/os/fsi"
    29  	"github.com/pbberlin/tools/os/fsi/dsfs"
    30  	"github.com/pbberlin/tools/os/fsi/memfs"
    31  	"github.com/pbberlin/tools/os/fsi/osfs"
    32  )
    33  
    34  func initFileSystems() (fss []fsi.FileSystem, c aetest.Context) {
    35  
    36  	var err error
    37  	c, err = aetest.NewContext(nil)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	// defer c.Close()
    43  	// Not here, but instead at the start of the test-funcs
    44  
    45  	// We cant make variadic options generic,
    46  	// since they need the concrete filesystem type.
    47  	fs1 := dsfs.New(
    48  		dsfs.MountName(dsfs.MountPointLast()),
    49  		dsfs.AeContext(c),
    50  	)
    51  
    52  	fs3 := osfs.New()
    53  
    54  	fs4 := memfs.New(
    55  		memfs.Ident("m"),
    56  	)
    57  
    58  	fss = []fsi.FileSystem{fs1, fs3, fs4}
    59  
    60  	return fss, c
    61  }
    62  
    63  func newFile(testName string, fs fsi.FileSystem, t *testing.T) (f fsi.File) {
    64  	// Use a local file system, not NFS.
    65  	// On Unix, override $TMPDIR in case the user
    66  	// has it set to an NFS-mounted directory.
    67  	dir := ""
    68  	if runtime.GOOS != "windows" {
    69  		dir = "/tmp"
    70  	}
    71  	fs.MkdirAll(dir, 0777)
    72  	f, err := fs.Create(path.Join(dir, testName))
    73  	if err != nil {
    74  		t.Fatalf("%v: open %s: %s", fs.Name(), testName, err)
    75  	}
    76  	return f
    77  }
    78  
    79  func writeFile(t *testing.T, fs fsi.FileSystem, fname string, flag int, text string) string {
    80  	f, err := fs.OpenFile(fname, flag, 0666)
    81  	if err != nil {
    82  		t.Fatalf("Open: %v", err)
    83  	}
    84  	n, err := io.WriteString(f, text)
    85  	if err != nil {
    86  		t.Fatalf("WriteString: %d, %v", n, err)
    87  	}
    88  	f.Close()
    89  	data, err := ioutil.ReadFile(fname)
    90  	if err != nil {
    91  		t.Fatalf("ReadFile: %v", err)
    92  	}
    93  	return string(data)
    94  }
    95  
    96  func testReaddirnames(fs fsi.FileSystem, dir string, contents []string, t *testing.T) {
    97  	file, err := fs.Open(dir)
    98  	if err != nil {
    99  		t.Fatalf("open %q failed: %v", dir, err)
   100  	}
   101  	defer file.Close()
   102  	s, err2 := file.Readdirnames(-1)
   103  	if err2 != nil {
   104  		t.Fatalf("readdirnames %q failed: %v", dir, err2)
   105  	}
   106  	for _, m := range contents {
   107  		found := false
   108  		for _, n := range s {
   109  			if n == "." || n == ".." {
   110  				t.Errorf("got %s in directory", n)
   111  			}
   112  			if equal(m, n) {
   113  				if found {
   114  					t.Error("present twice:", m)
   115  				}
   116  				found = true
   117  			}
   118  		}
   119  		if !found {
   120  			t.Error("could not find", m)
   121  		}
   122  	}
   123  }
   124  
   125  func testReaddir(fs fsi.FileSystem, dir string, contents []string, t *testing.T) {
   126  	file, err := fs.Open(dir)
   127  	if err != nil {
   128  		t.Fatalf("open %q failed: %v", dir, err)
   129  	}
   130  	defer file.Close()
   131  	s, err2 := file.Readdir(-1)
   132  	if err2 != nil {
   133  		t.Fatalf("readdir %q failed: %v", dir, err2)
   134  	}
   135  	for _, m := range contents {
   136  		found := false
   137  		for _, n := range s {
   138  			if equal(m, n.Name()) {
   139  				if found {
   140  					t.Error("present twice:", m)
   141  				}
   142  				found = true
   143  			}
   144  		}
   145  		if !found {
   146  			t.Error("could not find", m)
   147  		}
   148  	}
   149  }
   150  
   151  func equal(name1, name2 string) (r bool) {
   152  	switch runtime.GOOS {
   153  	case "windows":
   154  		r = strings.ToLower(name1) == strings.ToLower(name2)
   155  	default:
   156  		r = name1 == name2
   157  	}
   158  	return
   159  }
   160  
   161  func checkSize(t *testing.T, f fsi.File, size int64) {
   162  	dir, err := f.Stat()
   163  	if err != nil {
   164  		t.Fatalf("Stat %q (looking for size %d): %s", f.Name(), size, err)
   165  	}
   166  	if dir.Size() != size {
   167  		t.Errorf("Stat %q: size %d want %d", f.Name(), dir.Size(), size)
   168  	}
   169  }