| 1 | // Copyright 2013 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package mapfs |
| 6 | |
| 7 | import ( |
| 8 | "io/ioutil" |
| 9 | "os" |
| 10 | "reflect" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestOpenRoot(t *testing.T) { |
| 15 | fs := New(map[string]string{ |
| 16 | "foo/bar/three.txt": "a", |
| 17 | "foo/bar.txt": "b", |
| 18 | "top.txt": "c", |
| 19 | "other-top.txt": "d", |
| 20 | }) |
| 21 | tests := []struct { |
| 22 | path string |
| 23 | want string |
| 24 | }{ |
| 25 | {"/foo/bar/three.txt", "a"}, |
| 26 | {"foo/bar/three.txt", "a"}, |
| 27 | {"foo/bar.txt", "b"}, |
| 28 | {"top.txt", "c"}, |
| 29 | {"/top.txt", "c"}, |
| 30 | {"other-top.txt", "d"}, |
| 31 | {"/other-top.txt", "d"}, |
| 32 | } |
| 33 | for _, tt := range tests { |
| 34 | rsc, err := fs.Open(tt.path) |
| 35 | if err != nil { |
| 36 | t.Errorf("Open(%q) = %v", tt.path, err) |
| 37 | continue |
| 38 | } |
| 39 | slurp, err := ioutil.ReadAll(rsc) |
| 40 | if err != nil { |
| 41 | t.Error(err) |
| 42 | } |
| 43 | if string(slurp) != tt.want { |
| 44 | t.Errorf("Read(%q) = %q; want %q", tt.path, tt.want, slurp) |
| 45 | } |
| 46 | rsc.Close() |
| 47 | } |
| 48 | |
| 49 | _, err := fs.Open("/xxxx") |
| 50 | if !os.IsNotExist(err) { |
| 51 | t.Errorf("ReadDir /xxxx = %v; want os.IsNotExist error", err) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestReaddir(t *testing.T) { |
| 56 | fs := New(map[string]string{ |
| 57 | "foo/bar/three.txt": "333", |
| 58 | "foo/bar.txt": "22", |
| 59 | "top.txt": "top.txt file", |
| 60 | "other-top.txt": "other-top.txt file", |
| 61 | }) |
| 62 | tests := []struct { |
| 63 | dir string |
| 64 | want []os.FileInfo |
| 65 | }{ |
| 66 | { |
| 67 | dir: "/", |
| 68 | want: []os.FileInfo{ |
| 69 | mapFI{name: "foo", dir: true}, |
| 70 | mapFI{name: "other-top.txt", size: len("other-top.txt file")}, |
| 71 | mapFI{name: "top.txt", size: len("top.txt file")}, |
| 72 | }, |
| 73 | }, |
| 74 | { |
| 75 | dir: "/foo", |
| 76 | want: []os.FileInfo{ |
| 77 | mapFI{name: "bar", dir: true}, |
| 78 | mapFI{name: "bar.txt", size: 2}, |
| 79 | }, |
| 80 | }, |
| 81 | { |
| 82 | dir: "/foo/", |
| 83 | want: []os.FileInfo{ |
| 84 | mapFI{name: "bar", dir: true}, |
| 85 | mapFI{name: "bar.txt", size: 2}, |
| 86 | }, |
| 87 | }, |
| 88 | { |
| 89 | dir: "/foo/bar", |
| 90 | want: []os.FileInfo{ |
| 91 | mapFI{name: "three.txt", size: 3}, |
| 92 | }, |
| 93 | }, |
| 94 | } |
| 95 | for _, tt := range tests { |
| 96 | fis, err := fs.ReadDir(tt.dir) |
| 97 | if err != nil { |
| 98 | t.Errorf("ReadDir(%q) = %v", tt.dir, err) |
| 99 | continue |
| 100 | } |
| 101 | if !reflect.DeepEqual(fis, tt.want) { |
| 102 | t.Errorf("ReadDir(%q) = %#v; want %#v", tt.dir, fis, tt.want) |
| 103 | continue |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | _, err := fs.ReadDir("/xxxx") |
| 108 | if !os.IsNotExist(err) { |
| 109 | t.Errorf("ReadDir /xxxx = %v; want os.IsNotExist error", err) |
| 110 | } |
| 111 | } |
| 112 |
Members