Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "os" | |
| 10 "reflect" | |
| 11 "testing" | |
| 12 ) | |
| 13 | |
| 14 // basicFileInfo implements some of os.FileInfo, and panics if unexpected parts | |
| 15 // of that interface are called. | |
| 16 type basicFileInfo struct { | |
| 17 size int64 | |
| 18 mode os.FileMode | |
| 19 isDir bool | |
| 20 | |
| 21 os.FileInfo | |
| 22 } | |
| 23 | |
| 24 func (bfi basicFileInfo) Size() int64 { return bfi.size } | |
| 25 func (bfi basicFileInfo) Mode() os.FileMode { return bfi.mode } | |
| 26 func (bfi basicFileInfo) IsDir() bool { return bfi.isDir } | |
| 27 | |
| 28 type file struct { | |
| 29 path string | |
| 30 bfi basicFileInfo | |
| 31 } | |
| 32 | |
| 33 func symlink(path string, size int64) file { | |
| 34 return file{ | |
| 35 path: path, | |
| 36 bfi: basicFileInfo{ | |
| 37 size: size, | |
| 38 mode: os.ModeSymlink, | |
| 39 isDir: false, | |
| 40 }, | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 func regularFile(path string, size int64) file { | |
| 45 return file{ | |
| 46 path: path, | |
| 47 bfi: basicFileInfo{ | |
| 48 size: size, | |
| 49 mode: 0, | |
| 50 isDir: false, | |
| 51 }, | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 func directory(path string) file { | |
| 56 return file{ | |
| 57 path: path, | |
| 58 bfi: basicFileInfo{ | |
| 59 size: 0, | |
| 60 mode: os.ModeDir, | |
| 61 isDir: true, | |
| 62 }, | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 var errBang = errors.New("bang") | |
| 67 | |
| 68 func TestWalkFn(t *testing.T) { | |
| 69 type testCase struct { | |
| 70 name string | |
| 71 files []file | |
| 72 walkFnErr error | |
| 73 want partitionedDeps | |
| 74 wantErr error | |
| 75 } | |
| 76 | |
| 77 TestCases: | |
| 78 for _, tc := range []testCase{ | |
| 79 { | |
| 80 name: "partitions files", | |
| 81 files: []file{ | |
|
mithro
2017/06/29 07:25:18
I think this test would technically have dir("/roo
mcgreevy
2017/06/30 01:24:19
I don't know what you mean by "this test would tec
| |
| 82 symlink("/rootDir/patha", 1e3), | |
| 83 regularFile("/rootDir/pathb", 10e3), | |
| 84 regularFile("/rootDir/pathc", 100e3), | |
| 85 }, | |
| 86 want: partitionedDeps{ | |
| 87 links: itemGroup{ | |
| 88 items: []*Item{ | |
| 89 { | |
| 90 Path: "/rootDir/patha ", | |
| 91 RelPath: "patha", | |
| 92 Mode: os.ModeSymlink, | |
| 93 Size: 1e3, | |
| 94 }, | |
| 95 }, | |
| 96 size: 1e3, | |
| 97 }, | |
| 98 archiveFiles: itemGroup{ | |
| 99 items: []*Item{ | |
| 100 { | |
| 101 Path: "/rootDir/pathb ", | |
| 102 RelPath: "pathb", | |
| 103 Mode: 0, | |
| 104 Size: 10e3, | |
| 105 }, | |
| 106 }, | |
| 107 size: 10e3, | |
| 108 }, | |
| 109 indivFiles: itemGroup{ | |
| 110 items: []*Item{ | |
| 111 { | |
| 112 Path: "/rootDir/pathc ", | |
| 113 RelPath: "pathc", | |
| 114 Mode: 0, | |
| 115 Size: 100e3, | |
| 116 }, | |
| 117 }, | |
| 118 size: 100e3, | |
| 119 }, | |
| 120 }, | |
| 121 }, | |
| 122 { | |
| 123 name: "handles zero-size files", | |
| 124 files: []file{ | |
| 125 symlink("/rootDir/patha", 0), | |
| 126 regularFile("/rootDir/pathb", 0), | |
| 127 }, | |
| 128 want: partitionedDeps{ | |
| 129 links: itemGroup{ | |
| 130 items: []*Item{ | |
| 131 { | |
| 132 Path: "/rootDir/patha ", | |
| 133 RelPath: "patha", | |
| 134 Mode: os.ModeSymlink, | |
| 135 Size: 0, | |
| 136 }, | |
| 137 }, | |
| 138 size: 0, | |
| 139 }, | |
| 140 archiveFiles: itemGroup{ | |
| 141 items: []*Item{ | |
| 142 { | |
| 143 Path: "/rootDir/pathb ", | |
| 144 RelPath: "pathb", | |
| 145 Mode: 0, | |
| 146 Size: 0, | |
| 147 }, | |
| 148 }, | |
| 149 size: 0, | |
| 150 }, | |
| 151 }, | |
| 152 }, | |
| 153 { | |
| 154 name: "aggregates link sizes", | |
| 155 files: []file{ | |
| 156 symlink("/rootDir/patha", 1), | |
| 157 symlink("/rootDir/pathb", 2), | |
| 158 }, | |
| 159 want: partitionedDeps{ | |
| 160 links: itemGroup{ | |
| 161 items: []*Item{ | |
| 162 { | |
| 163 Path: "/rootDir/patha ", | |
| 164 RelPath: "patha", | |
| 165 Mode: os.ModeSymlink, | |
| 166 Size: 1, | |
| 167 }, | |
| 168 { | |
| 169 Path: "/rootDir/pathb ", | |
| 170 RelPath: "pathb", | |
| 171 Mode: os.ModeSymlink, | |
| 172 Size: 2, | |
| 173 }, | |
| 174 }, | |
| 175 size: 3, | |
| 176 }, | |
| 177 }, | |
| 178 }, | |
| 179 { | |
| 180 name: "aggregates large file sizes", | |
| 181 files: []file{ | |
| 182 regularFile("/rootDir/patha", 100e3), | |
| 183 regularFile("/rootDir/pathb", 200e3), | |
| 184 }, | |
| 185 want: partitionedDeps{ | |
| 186 indivFiles: itemGroup{ | |
| 187 items: []*Item{ | |
| 188 { | |
| 189 Path: "/rootDir/patha ", | |
| 190 RelPath: "patha", | |
| 191 Mode: 0, | |
| 192 Size: 100e3, | |
| 193 }, | |
| 194 { | |
| 195 Path: "/rootDir/pathb ", | |
| 196 RelPath: "pathb", | |
| 197 Mode: 0, | |
| 198 Size: 200e3, | |
| 199 }, | |
| 200 }, | |
| 201 size: 300e3, | |
| 202 }, | |
| 203 }, | |
| 204 }, | |
| 205 { | |
| 206 name: "aggregates small file sizes", | |
| 207 files: []file{ | |
| 208 regularFile("/rootDir/patha", 10e3), | |
| 209 regularFile("/rootDir/pathb", 20e3), | |
| 210 }, | |
| 211 want: partitionedDeps{ | |
| 212 archiveFiles: itemGroup{ | |
| 213 items: []*Item{ | |
| 214 { | |
| 215 Path: "/rootDir/patha ", | |
| 216 RelPath: "patha", | |
| 217 Mode: 0, | |
| 218 Size: 10e3, | |
| 219 }, | |
| 220 { | |
| 221 Path: "/rootDir/pathb ", | |
| 222 RelPath: "pathb", | |
| 223 Mode: 0, | |
| 224 Size: 20e3, | |
| 225 }, | |
| 226 }, | |
| 227 size: 30e3, | |
| 228 }, | |
| 229 }, | |
| 230 }, | |
| 231 { | |
| 232 name: "returns errors unchanged", | |
| 233 files: []file{regularFile("/rootDir/patha", 1)}, | |
| 234 walkFnErr: errBang, | |
| 235 want: partitionedDeps{}, | |
| 236 wantErr: errBang, | |
| 237 }, | |
| 238 { | |
| 239 name: "does nothing for directories", | |
| 240 files: []file{directory("/rootDir/patha")}, | |
| 241 want: partitionedDeps{}, | |
| 242 wantErr: nil, | |
| 243 }, | |
| 244 } { | |
| 245 pw := partitioningWalker{rootDir: "/rootDir"} | |
| 246 for _, f := range tc.files { | |
| 247 err := pw.walkFn(f.path, f.bfi, tc.walkFnErr) | |
| 248 if err != tc.wantErr { | |
| 249 t.Errorf("partitioning deps(%s): walkFn got err %v; want %v", tc.name, err, tc.wantErr) | |
| 250 continue TestCases | |
| 251 } | |
| 252 } | |
| 253 if got, want := pw.parts, tc.want; !reflect.DeepEqual(got, want) { | |
| 254 t.Errorf("partitioning deps(%s): got %#v; want %#v", tc. name, got, want) | |
| 255 | |
| 256 } | |
| 257 } | |
| 258 | |
| 259 } | |
| 260 | |
| 261 func TestWalkFn_BadRelpath(t *testing.T) { | |
| 262 pw := partitioningWalker{rootDir: "/rootDir"} | |
| 263 f := regularFile("./patha", 1) | |
| 264 if err := pw.walkFn(f.path, f.bfi, nil); err == nil { | |
| 265 t.Errorf("testing bad relpath: walkFn returned nil err; want non -nil err") | |
| 266 } | |
| 267 } | |
| OLD | NEW |