Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: client/cmd/isolate/exp_archive_test.go

Issue 2958913002: isolate: pull exparchive filewalk code into its own func (Closed)
Patch Set: Add tests. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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{
mithro 2017/06/27 07:48:10 I would have tested the following cases; * empty
mcgreevy 2017/06/28 01:25:51 Most of the tests that you have suggested below wo
mithro 2017/06/29 07:25:17 If I understand correctly, walkFn is called with a
mcgreevy 2017/06/30 01:24:19 walkFn is called with the *path*, and an os.FileIn
79 {
80 name: "partitions files",
81 files: []file{
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: "aggregates link sizes",
124 files: []file{
125 symlink("/rootDir/patha", 1),
126 symlink("/rootDir/pathb", 2),
127 },
128 want: partitionedDeps{
129 links: itemGroup{
130 items: []*Item{
131 {
132 Path: "/rootDir/patha ",
133 RelPath: "patha",
134 Mode: os.ModeSymlink,
135 Size: 1,
mithro 2017/06/27 07:48:10 Why are these size 1 / 2?
mcgreevy 2017/06/28 01:25:51 When testing things that add up values, I tend to
mithro 2017/06/29 07:25:17 Minor comments; That makes a lot of sense, but wi
mcgreevy 2017/06/30 01:24:19 OK, done.
136 },
137 {
138 Path: "/rootDir/pathb ",
139 RelPath: "pathb",
140 Mode: os.ModeSymlink,
141 Size: 2,
142 },
143 },
144 size: 3,
mithro 2017/06/27 07:48:10 Size 3? Is this the cumulative size / total size?
mcgreevy 2017/06/28 01:25:50 total size.
145 },
146 },
147 },
148 {
149 name: "aggregates large file sizes",
150 files: []file{
151 regularFile("/rootDir/patha", 100e3),
152 regularFile("/rootDir/pathb", 200e3),
153 },
154 want: partitionedDeps{
155 indivFiles: itemGroup{
156 items: []*Item{
157 {
158 Path: "/rootDir/patha ",
159 RelPath: "patha",
160 Mode: 0,
161 Size: 100e3,
162 },
163 {
164 Path: "/rootDir/pathb ",
165 RelPath: "pathb",
166 Mode: 0,
167 Size: 200e3,
168 },
169 },
170 size: 300e3,
171 },
172 },
173 },
174 {
175 name: "aggregates small file sizes",
176 files: []file{
177 regularFile("/rootDir/patha", 10e3),
178 regularFile("/rootDir/pathb", 20e3),
179 },
180 want: partitionedDeps{
181 archiveFiles: itemGroup{
182 items: []*Item{
183 {
184 Path: "/rootDir/patha ",
185 RelPath: "patha",
186 Mode: 0,
187 Size: 10e3,
188 },
189 {
190 Path: "/rootDir/pathb ",
191 RelPath: "pathb",
192 Mode: 0,
193 Size: 20e3,
194 },
195 },
196 size: 30e3,
197 },
198 },
199 },
200 {
201 name: "returns errors unchanged",
202 files: []file{regularFile("/rootDir/patha", 1)},
203 walkFnErr: errBang,
204 want: partitionedDeps{},
205 wantErr: errBang,
206 },
207 {
208 name: "does nothing for directories",
209 files: []file{directory("/rootDir/patha")},
210 want: partitionedDeps{},
211 wantErr: nil,
212 },
213 } {
214 pw := partitionWalker{rootDir: "/rootDir"}
215 for _, f := range tc.files {
216 err := pw.walkFn(f.path, f.bfi, tc.walkFnErr)
217 if err != tc.wantErr {
218 t.Errorf("partitioning deps(%s): walkFn got err %v; want %v", tc.name, err, tc.wantErr)
219 continue TestCases
mithro 2017/06/27 07:48:10 I don't quite get the usage of the continue here?
mcgreevy 2017/06/28 01:25:51 If we get an unexpected error from walkFunc, there
mithro 2017/06/29 07:25:17 I think the biggest problem is that the label for
mcgreevy 2017/06/30 01:24:19 Looking at this more closely, I think that it is w
220 }
221 }
222 if got, want := pw.parts, tc.want; !reflect.DeepEqual(got, want) {
223 t.Errorf("partitioning deps(%s): got %#v; want %#v", tc. name, got, want)
224
225 }
226 }
227
228 }
OLDNEW
« client/cmd/isolate/exp_archive.go ('K') | « client/cmd/isolate/exp_archive.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698