Exemple #1
0
func TestRel(t *testing.T) {
	// To randomize.
	cases := map[string]struct{}{
		"/":                      {},
		"C:/":                    {},
		"/a":                     {},
		"X:/a":                   {},
		"/tmp":                   {},
		"C:/Windows/Temp":        {},
		"/private/var/tmp":       {},
		"C:/Program Files (x86)": {},
	}
	for cas := range cases {
		cas = filepath.FromSlash(cas)
		for i, tree := range trees {
			// This is a hack, which emulates:
			//
			//   func EqualFilesystem(lhs memfs.FS, rhs fs.FS) bool
			//
			// Which should probably hit memfs/util.go file eventually.
			exp, spy := move(tree, cas), memfs.New()
			if n := Copy(tree, Rel(spy, cas)); n == 0 {
				t.Errorf("want n!=0 (cas=%s, i=%d)", cas, i)
				continue
			}
			if !memfs.Equal(spy, exp) {
				t.Errorf("want spy=exp (cas=%s, i=%d)", cas, i)
			}
			return
		}
	}
}
Exemple #2
0
// TODO(rjeczalik): Split into TestTeeOpen and TestTeeReaddir
func TestTeeOpen(t *testing.T) {
	cases := [...]struct {
		open []string
		read []string
		fs   []byte
	}{
		0: {
			open: []string{"/w/x/y/z"},
			fs:   []byte(".\nw\n\tx\n\t\ty\n\t\t\tz/"),
		},
		1: {
			open: []string{"/a.txt", "/w/w.txt", "/a"},
			fs:   []byte(".\na/\na.txt\nw\n\tw.txt"),
		},
		2: {
			read: []string{"/a/b2/c1"},
			fs:   []byte(".\na\n\tb2\n\t\tc1\n\t\t\td1.txt\n\t\t\td2/\n\t\t\td3.txt"),
		},
		3: {
			read: []string{"/a/b1/c1", "/a/b1/c2", "/a/b1/c3"},
			fs: []byte(".\na\n\tb1\n\t\tc1\n\t\t\tc1.txt\n\t\tc2\n\t\t\tc2.txt\n\t\t" +
				"c3\n\t\t\tc3.txt\n\t\t\td1/"),
		},
		4: {
			read: []string{"/w", "/w/x/y", "/w/x/y/z", "/w/x"},
			fs:   []byte(".\nw\n\tw.txt\n\tx\n\t\ty\n\t\t\tz\n\t\t\t\t1.txt\n\t\ty.txt"),
		}}
Test:
	for i, cas := range cases {
		spy := memfs.New()
		tee := Tee(tree, spy)
		for j, path := range cas.open {
			if _, err := tee.Open(filepath.FromSlash(path)); err != nil {
				t.Errorf("want err=nil; got %q (i=%d, j=%d)", err, i, j)
				continue Test
			}
		}
		for j, path := range cas.read {
			f, err := tee.Open(filepath.FromSlash(path))
			if err != nil {
				t.Errorf("want err=nil; got %q (i=%d, j=%d)", err, i, j)
				continue Test
			}
			if _, err = f.Readdir(0); err != nil {
				t.Errorf("want err=nil; got %q (i=%d, j=%d)", err, i, j)
				continue Test
			}
		}
		if !memfs.Equal(spy, memfs.Must(memfs.UnmarshalTab(cas.fs))) {
			t.Errorf("want Compare(...)=true; got false (i=%d)", i)
		}
	}
}