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

Side by Side Diff: pkg/analyzer/test/file_system/physical_resource_provider_test.dart

Issue 632353002: Add support for cross-session storage (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library test.physical_file_system; 5 library test.physical_file_system;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:io' as io; 8 import 'dart:io' as io;
9 9
10 import 'package:analyzer/file_system/file_system.dart'; 10 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/file_system/physical_file_system.dart'; 11 import 'package:analyzer/file_system/physical_file_system.dart';
12 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
13 import 'package:path/path.dart'; 13 import 'package:path/path.dart';
14 import 'package:unittest/unittest.dart'; 14 import 'package:unittest/unittest.dart';
15 import 'package:watcher/watcher.dart'; 15 import 'package:watcher/watcher.dart';
16 16
17 17
18 var _isFile = new isInstanceOf<File>(); 18 var _isFile = new isInstanceOf<File>();
19 var _isFolder = new isInstanceOf<Folder>(); 19 var _isFolder = new isInstanceOf<Folder>();
20 20
21 21
22 main() { 22 main() {
23 groupSep = ' | '; 23 groupSep = ' | ';
24 24
25 group('PhysicalResourceProvider', () { 25 group('PhysicalResourceProvider', () {
26 io.Directory tempDirectory; 26 io.Directory tempDirectory;
27 String tempPath; 27 String tempPath;
28 28
29 setUp(() { 29 setUp(() {
30 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); 30 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource');
31 tempPath = tempDirectory.absolute.path; 31 tempPath = tempDirectory.absolute.path;
32 }); 32 });
33 33
34 tearDown(() { 34 tearDown(() {
35 tempDirectory.deleteSync(recursive: true); 35 tempDirectory.deleteSync(recursive: true);
36 }); 36 });
37 37
38 group('Watch', () { 38 group('Watch', () {
39 39
40 Future delayed(computation()) { 40 Future delayed(computation()) {
41 // Give the tests 1 second to detect the changes. While it may only 41 // Give the tests 1 second to detect the changes. While it may only
42 // take up to a few hundred ms, a whole second gives a good margin 42 // take up to a few hundred ms, a whole second gives a good margin
43 // for when running tests. 43 // for when running tests.
44 return new Future.delayed( 44 return new Future.delayed(
45 new Duration(seconds: 1), computation); 45 new Duration(seconds: 1), computation);
46 } 46 }
47 47
48 watchingFolder(String path, test(List<WatchEvent> changesReceived)) { 48 watchingFolder(String path, test(List<WatchEvent> changesReceived)) {
49 // Delay before we start watching the folder. This is necessary 49 // Delay before we start watching the folder. This is necessary
50 // because on MacOS, file modifications that occur just before we 50 // because on MacOS, file modifications that occur just before we
51 // start watching are sometimes misclassified as happening just after 51 // start watching are sometimes misclassified as happening just after
52 // we start watching. 52 // we start watching.
53 return delayed(() { 53 return delayed(() {
54 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path); 54 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path);
55 var changesReceived = <WatchEvent>[]; 55 var changesReceived = <WatchEvent>[];
56 var subscription = folder.changes.listen(changesReceived.add); 56 var subscription = folder.changes.listen(changesReceived.add);
57 // Delay running the rest of the test to allow folder.changes to 57 // Delay running the rest of the test to allow folder.changes to
58 // take a snapshot of the current directory state. Otherwise it 58 // take a snapshot of the current directory state. Otherwise it
59 // won't be able to reliably distinguish new files from modified 59 // won't be able to reliably distinguish new files from modified
60 // ones. 60 // ones.
61 return delayed(() => test(changesReceived)).whenComplete(() { 61 return delayed(() => test(changesReceived)).whenComplete(() {
62 subscription.cancel(); 62 subscription.cancel();
63 });
64 }); 63 });
65 } 64 });
66 65 }
67 test('create file', () => watchingFolder(tempPath, (changesReceived) { 66
67 test('create file', () => watchingFolder(tempPath, (changesReceived) {
68 expect(changesReceived, hasLength(0));
69 var path = join(tempPath, 'foo');
70 new io.File(path).writeAsStringSync('contents');
71 return delayed(() {
72 expect(changesReceived, hasLength(1));
73 expect(changesReceived[0].type, equals(ChangeType.ADD));
74 expect(changesReceived[0].path, equals(path));
75 });
76 }));
77
78 test('modify file', () {
79 var path = join(tempPath, 'foo');
80 var file = new io.File(path);
81 file.writeAsStringSync('contents 1');
82 return watchingFolder(tempPath, (changesReceived) {
68 expect(changesReceived, hasLength(0)); 83 expect(changesReceived, hasLength(0));
69 var path = join(tempPath, 'foo'); 84 file.writeAsStringSync('contents 2');
70 new io.File(path).writeAsStringSync('contents');
71 return delayed(() { 85 return delayed(() {
72 expect(changesReceived, hasLength(1)); 86 expect(changesReceived, hasLength(1));
73 expect(changesReceived[0].type, equals(ChangeType.ADD)); 87 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
74 expect(changesReceived[0].path, equals(path)); 88 expect(changesReceived[0].path, equals(path));
75 }); 89 });
76 })); 90 });
77 91 });
78 test('modify file', () { 92
79 var path = join(tempPath, 'foo'); 93 test('modify file in subdir', () {
80 var file = new io.File(path); 94 var subdirPath = join(tempPath, 'foo');
81 file.writeAsStringSync('contents 1'); 95 new io.Directory(subdirPath).createSync();
82 return watchingFolder(tempPath, (changesReceived) { 96 var path = join(tempPath, 'bar');
83 expect(changesReceived, hasLength(0)); 97 var file = new io.File(path);
84 file.writeAsStringSync('contents 2'); 98 file.writeAsStringSync('contents 1');
85 return delayed(() { 99 return watchingFolder(tempPath, (changesReceived) {
86 expect(changesReceived, hasLength(1)); 100 expect(changesReceived, hasLength(0));
87 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); 101 file.writeAsStringSync('contents 2');
88 expect(changesReceived[0].path, equals(path)); 102 return delayed(() {
89 }); 103 expect(changesReceived, hasLength(1));
104 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
105 expect(changesReceived[0].path, equals(path));
90 }); 106 });
91 }); 107 });
92 108 });
93 test('modify file in subdir', () { 109
94 var subdirPath = join(tempPath, 'foo'); 110 test('delete file', () {
95 new io.Directory(subdirPath).createSync(); 111 var path = join(tempPath, 'foo');
96 var path = join(tempPath, 'bar'); 112 var file = new io.File(path);
97 var file = new io.File(path); 113 file.writeAsStringSync('contents 1');
98 file.writeAsStringSync('contents 1'); 114 return watchingFolder(tempPath, (changesReceived) {
99 return watchingFolder(tempPath, (changesReceived) { 115 expect(changesReceived, hasLength(0));
100 expect(changesReceived, hasLength(0)); 116 file.deleteSync();
101 file.writeAsStringSync('contents 2'); 117 return delayed(() {
102 return delayed(() { 118 expect(changesReceived, hasLength(1));
103 expect(changesReceived, hasLength(1)); 119 expect(changesReceived[0].type, equals(ChangeType.REMOVE));
104 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); 120 expect(changesReceived[0].path, equals(path));
105 expect(changesReceived[0].path, equals(path));
106 });
107 }); 121 });
108 }); 122 });
109 123 });
110 test('delete file', () { 124 });
111 var path = join(tempPath, 'foo'); 125
112 var file = new io.File(path); 126 group('getStateLocation', () {
113 file.writeAsStringSync('contents 1'); 127 test('uniqueness', () {
114 return watchingFolder(tempPath, (changesReceived) { 128 PhysicalResourceProvider provider = PhysicalResourceProvider.INSTANCE;
115 expect(changesReceived, hasLength(0)); 129 String idOne = 'one';
116 file.deleteSync(); 130 Folder folderOne = provider.getStateLocation(idOne);
117 return delayed(() { 131 expect(folderOne, isNotNull);
118 expect(changesReceived, hasLength(1)); 132 String idTwo = 'two';
119 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); 133 Folder folderTwo = provider.getStateLocation(idTwo);
120 expect(changesReceived[0].path, equals(path)); 134 expect(folderTwo, isNotNull);
121 }); 135 expect(folderTwo, isNot(equals(folderOne)));
122 }); 136 expect(provider.getStateLocation(idOne), equals(folderOne));
123 }); 137 });
124 }); 138 });
125 139
126 group('File', () { 140 group('File', () {
127 String path; 141 String path;
128 File file; 142 File file;
129 143
130 setUp(() { 144 setUp(() {
131 path = join(tempPath, 'file.txt'); 145 path = join(tempPath, 'file.txt');
132 file = PhysicalResourceProvider.INSTANCE.getResource(path); 146 file = PhysicalResourceProvider.INSTANCE.getResource(path);
133 }); 147 });
134 148
135 test('createSource', () { 149 test('createSource', () {
150 new io.File(path).writeAsStringSync('contents');
151 Source source = file.createSource();
152 expect(source.uriKind, UriKind.FILE_URI);
153 expect(source.exists(), isTrue);
154 expect(source.contents.data, 'contents');
155 });
156
157 group('exists', () {
158 test('false', () {
159 expect(file.exists, isFalse);
160 });
161
162 test('true', () {
136 new io.File(path).writeAsStringSync('contents'); 163 new io.File(path).writeAsStringSync('contents');
137 Source source = file.createSource(); 164 expect(file.exists, isTrue);
138 expect(source.uriKind, UriKind.FILE_URI); 165 });
139 expect(source.exists(), isTrue); 166 });
140 expect(source.contents.data, 'contents'); 167
141 }); 168 test('fullName', () {
142 169 expect(file.path, path);
143 group('exists', () { 170 });
144 test('false', () { 171
145 expect(file.exists, isFalse); 172 test('hashCode', () {
146 }); 173 new io.File(path).writeAsStringSync('contents');
147 174 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path);
148 test('true', () { 175 expect(file.hashCode, equals(file2.hashCode));
149 new io.File(path).writeAsStringSync('contents'); 176 });
150 expect(file.exists, isTrue); 177
151 }); 178 test('equality: same path', () {
152 }); 179 new io.File(path).writeAsStringSync('contents');
153 180 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path);
154 test('fullName', () { 181 expect(file == file2, isTrue);
155 expect(file.path, path); 182 });
156 }); 183
157 184 test('equality: different paths', () {
158 test('hashCode', () { 185 String path2 = join(tempPath, 'file2.txt');
159 new io.File(path).writeAsStringSync('contents'); 186 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path2);
160 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path); 187 expect(file == file2, isFalse);
161 expect(file.hashCode, equals(file2.hashCode)); 188 });
162 }); 189
163 190 test('isOrContains', () {
164 test('equality: same path', () { 191 File file = PhysicalResourceProvider.INSTANCE.getResource(path);
165 new io.File(path).writeAsStringSync('contents'); 192 expect(file.isOrContains(path), isTrue);
166 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path); 193 expect(file.isOrContains('foo'), isFalse);
167 expect(file == file2, isTrue); 194 });
168 }); 195
169 196 test('shortName', () {
170 test('equality: different paths', () { 197 expect(file.shortName, 'file.txt');
171 String path2 = join(tempPath, 'file2.txt'); 198 });
172 File file2 = PhysicalResourceProvider.INSTANCE.getResource(path2); 199
173 expect(file == file2, isFalse); 200 test('toString', () {
174 }); 201 expect(file.toString(), path);
175 202 });
176 test('isOrContains', () { 203
177 File file = PhysicalResourceProvider.INSTANCE.getResource(path); 204 test('parent', () {
178 expect(file.isOrContains(path), isTrue); 205 Resource parent = file.parent;
179 expect(file.isOrContains('foo'), isFalse); 206 expect(parent, new isInstanceOf<Folder>());
180 }); 207 expect(parent.path, equals(tempPath));
181 208 });
182 test('shortName', () { 209 });
183 expect(file.shortName, 'file.txt'); 210
184 }); 211 group('Folder', () {
185 212 String path;
186 test('toString', () { 213 Folder folder;
187 expect(file.toString(), path); 214
188 }); 215 setUp(() {
189 216 path = join(tempPath, 'folder');
190 test('parent', () { 217 new io.Directory(path).createSync();
191 Resource parent = file.parent; 218 folder = PhysicalResourceProvider.INSTANCE.getResource(path);
192 expect(parent, new isInstanceOf<Folder>()); 219 });
193 expect(parent.path, equals(tempPath)); 220
194 }); 221 test('hashCode', () {
195 }); 222 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path);
196 223 expect(folder.hashCode, equals(folder2.hashCode));
197 group('Folder', () { 224 });
198 String path; 225
199 Folder folder; 226 test('equality: same path', () {
200 227 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path);
201 setUp(() { 228 expect(folder == folder2, isTrue);
202 path = join(tempPath, 'folder'); 229 });
203 new io.Directory(path).createSync(); 230
204 folder = PhysicalResourceProvider.INSTANCE.getResource(path); 231 test('equality: different paths', () {
205 }); 232 String path2 = join(tempPath, 'folder2');
206 233 new io.Directory(path2).createSync();
207 test('hashCode', () { 234 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path2);
208 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path); 235 expect(folder == folder2, isFalse);
209 expect(folder.hashCode, equals(folder2.hashCode)); 236 });
210 }); 237
211 238 test('contains', () {
212 test('equality: same path', () { 239 expect(folder.contains(join(path, 'aaa.txt')), isTrue);
213 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path); 240 expect(folder.contains(join(path, 'aaa', 'bbb.txt')), isTrue);
214 expect(folder == folder2, isTrue); 241 expect(folder.contains(join(tempPath, 'baz.txt')), isFalse);
215 }); 242 expect(folder.contains(path), isFalse);
216 243 });
217 test('equality: different paths', () { 244
218 String path2 = join(tempPath, 'folder2'); 245 test('isOrContains', () {
219 new io.Directory(path2).createSync(); 246 expect(folder.isOrContains(path), isTrue);
220 Folder folder2 = PhysicalResourceProvider.INSTANCE.getResource(path2); 247 expect(folder.isOrContains(join(path, 'aaa.txt')), isTrue);
221 expect(folder == folder2, isFalse); 248 expect(folder.isOrContains(join(path, 'aaa', 'bbb.txt')), isTrue);
222 }); 249 expect(folder.isOrContains(join(tempPath, 'baz.txt')), isFalse);
223 250 });
224 test('contains', () { 251
225 expect(folder.contains(join(path, 'aaa.txt')), isTrue); 252 group('getChild', () {
226 expect(folder.contains(join(path, 'aaa', 'bbb.txt')), isTrue); 253 test('does not exist', () {
227 expect(folder.contains(join(tempPath, 'baz.txt')), isFalse); 254 var child = folder.getChild('no-such-resource');
228 expect(folder.contains(path), isFalse); 255 expect(child, isNotNull);
229 }); 256 expect(child.exists, isFalse);
230 257 });
231 test('isOrContains', () { 258
232 expect(folder.isOrContains(path), isTrue); 259 test('file', () {
233 expect(folder.isOrContains(join(path, 'aaa.txt')), isTrue); 260 new io.File(join(path, 'myFile')).createSync();
234 expect(folder.isOrContains(join(path, 'aaa', 'bbb.txt')), isTrue); 261 var child = folder.getChild('myFile');
235 expect(folder.isOrContains(join(tempPath, 'baz.txt')), isFalse); 262 expect(child, _isFile);
236 }); 263 expect(child.exists, isTrue);
237 264 });
238 group('getChild', () { 265
239 test('does not exist', () { 266 test('folder', () {
240 var child = folder.getChild('no-such-resource'); 267 new io.Directory(join(path, 'myFolder')).createSync();
241 expect(child, isNotNull); 268 var child = folder.getChild('myFolder');
242 expect(child.exists, isFalse); 269 expect(child, _isFolder);
243 }); 270 expect(child.exists, isTrue);
244 271 });
245 test('file', () { 272 });
246 new io.File(join(path, 'myFile')).createSync(); 273
247 var child = folder.getChild('myFile'); 274 test('getChildren', () {
248 expect(child, _isFile); 275 // create 2 files and 1 folder
249 expect(child.exists, isTrue); 276 new io.File(join(path, 'a.txt')).createSync();
250 }); 277 new io.Directory(join(path, 'bFolder')).createSync();
251 278 new io.File(join(path, 'c.txt')).createSync();
252 test('folder', () { 279 // prepare 3 children
253 new io.Directory(join(path, 'myFolder')).createSync(); 280 List<Resource> children = folder.getChildren();
254 var child = folder.getChild('myFolder'); 281 expect(children, hasLength(3));
255 expect(child, _isFolder); 282 children.sort((a, b) => a.shortName.compareTo(b.shortName));
256 expect(child.exists, isTrue); 283 // check that each child exists
257 }); 284 children.forEach((child) {
258 }); 285 expect(child.exists, true);
259 286 });
260 test('getChildren', () { 287 // check names
261 // create 2 files and 1 folder 288 expect(children[0].shortName, 'a.txt');
262 new io.File(join(path, 'a.txt')).createSync(); 289 expect(children[1].shortName, 'bFolder');
263 new io.Directory(join(path, 'bFolder')).createSync(); 290 expect(children[2].shortName, 'c.txt');
264 new io.File(join(path, 'c.txt')).createSync(); 291 // check types
265 // prepare 3 children 292 expect(children[0], _isFile);
266 List<Resource> children = folder.getChildren(); 293 expect(children[1], _isFolder);
267 expect(children, hasLength(3)); 294 expect(children[2], _isFile);
268 children.sort((a, b) => a.shortName.compareTo(b.shortName)); 295 });
269 // check that each child exists 296
270 children.forEach((child) { 297 test('parent', () {
271 expect(child.exists, true); 298 Resource parent = folder.parent;
272 }); 299 expect(parent, new isInstanceOf<Folder>());
273 // check names 300 expect(parent.path, equals(tempPath));
274 expect(children[0].shortName, 'a.txt'); 301
275 expect(children[1].shortName, 'bFolder'); 302 // Since the OS is in control of where tempPath is, we don't know how
276 expect(children[2].shortName, 'c.txt'); 303 // far it should be from the root. So just verify that each call to
277 // check types 304 // parent results in a a folder with a shorter path, and that we
278 expect(children[0], _isFile); 305 // reach the root eventually.
279 expect(children[1], _isFolder); 306 while (true) {
280 expect(children[2], _isFile); 307 Resource grandParent = parent.parent;
281 }); 308 if (grandParent == null) {
282 309 break;
283 test('parent', () {
284 Resource parent = folder.parent;
285 expect(parent, new isInstanceOf<Folder>());
286 expect(parent.path, equals(tempPath));
287
288 // Since the OS is in control of where tempPath is, we don't know how
289 // far it should be from the root. So just verify that each call to
290 // parent results in a a folder with a shorter path, and that we
291 // reach the root eventually.
292 while (true) {
293 Resource grandParent = parent.parent;
294 if (grandParent == null) {
295 break;
296 }
297 expect(grandParent, new isInstanceOf<Folder>());
298 expect(grandParent.path.length, lessThan(parent.path.length));
299 parent = grandParent;
300 } 310 }
301 }); 311 expect(grandParent, new isInstanceOf<Folder>());
302 312 expect(grandParent.path.length, lessThan(parent.path.length));
303 test('canonicalizePath', () { 313 parent = grandParent;
304 String path2 = join(tempPath, 'folder2'); 314 }
305 String path3 = join(tempPath, 'folder3'); 315 });
306 expect(folder.canonicalizePath('baz'), equals(join(path, 'baz'))); 316
307 expect(folder.canonicalizePath(path2), equals(path2)); 317 test('canonicalizePath', () {
308 expect(folder.canonicalizePath(join('..', 'folder2')), equals(path2)); 318 String path2 = join(tempPath, 'folder2');
309 expect(folder.canonicalizePath(join(path2, '..', 'folder3')), equals(p ath3)); 319 String path3 = join(tempPath, 'folder3');
310 expect(folder.canonicalizePath(join('.', 'baz')), equals(join(path, 'b az'))); 320 expect(folder.canonicalizePath('baz'), equals(join(path, 'baz')));
311 expect(folder.canonicalizePath(join(path2, '.', 'baz')), equals(join(p ath2, 'baz'))); 321 expect(folder.canonicalizePath(path2), equals(path2));
312 }); 322 expect(folder.canonicalizePath(join('..', 'folder2')), equals(path2));
313 }); 323 expect(folder.canonicalizePath(join(path2, '..', 'folder3')), equals(pat h3));
314 }); 324 expect(folder.canonicalizePath(join('.', 'baz')), equals(join(path, 'baz ')));
325 expect(folder.canonicalizePath(join(path2, '.', 'baz')), equals(join(pat h2, 'baz')));
326 });
327 });
328 });
315 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698