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

Side by Side Diff: pkg/analysis_server/test/resource_test.dart

Issue 303503007: Allow physical folder resources to be watched for changes to their contents. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 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
« no previous file with comments | « pkg/analysis_server/pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.resource; 5 library test.resource;
6 6
7 import 'dart:async';
7 import 'dart:io' as io; 8 import 'dart:io' as io;
8 9
9 import 'package:analysis_server/src/resource.dart'; 10 import 'package:analysis_server/src/resource.dart';
10 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; 11 import 'package:analyzer/src/generated/engine.dart' show TimestampedData;
11 import 'package:analyzer/src/generated/source_io.dart'; 12 import 'package:analyzer/src/generated/source_io.dart';
12 import 'package:path/path.dart'; 13 import 'package:path/path.dart';
13 import 'package:unittest/unittest.dart'; 14 import 'package:unittest/unittest.dart';
15 import 'package:watcher/watcher.dart';
14 16
15 main() { 17 main() {
16 groupSep = ' | '; 18 groupSep = ' | ';
17 19
18 group('MemoryResourceProvider', () { 20 group('MemoryResourceProvider', () {
19 MemoryResourceProvider provider; 21 MemoryResourceProvider provider;
20 22
21 setUp(() { 23 setUp(() {
22 provider = new MemoryResourceProvider(); 24 provider = new MemoryResourceProvider();
23 }); 25 });
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 245
244 setUp(() { 246 setUp(() {
245 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); 247 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource');
246 tempPath = tempDirectory.absolute.path; 248 tempPath = tempDirectory.absolute.path;
247 }); 249 });
248 250
249 tearDown(() { 251 tearDown(() {
250 tempDirectory.deleteSync(recursive: true); 252 tempDirectory.deleteSync(recursive: true);
251 }); 253 });
252 254
255 group('Watch', () {
256
257 Future delayed(computation()) {
258 // On windows, watching the filesystem is accomplished by polling once
scheglov 2014/05/27 22:15:14 "On Windows"
Paul Berry 2014/05/27 22:37:32 Done.
259 // per second. So wait 2 seconds to give time for polling to reliably
260 // occur.
261 return new Future.delayed(new Duration(seconds: 2), computation);
262 }
263
264 watchingFolder(String path, test(List<WatchEvent> changesReceived)) {
265 // Delay before we start watching the folder. This is necessary
266 // because on MacOS, file modifications that occur just before we start
267 // watching are sometimes misclassified as happening just after we
268 // start watching.
269 return delayed(() {
270 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path);
271 var changesReceived = <WatchEvent>[];
272 var subscription = folder.changes.listen(changesReceived.add);
273 // Delay running the rest of the test to allow folder.changes to take
274 // a snapshot of the current directory state. Otherwise it won't be
275 // able to reliably distinguish new files from modified ones.
276 return delayed(() => test(changesReceived)).whenComplete(() {
277 subscription.cancel();
278 });
279 });
280 }
281
282 test('create file', () => watchingFolder(tempPath, (changesReceived) {
283 expect(changesReceived, hasLength(0));
284 var path = join(tempPath, 'foo');
285 new io.File(path).writeAsStringSync('contents');
286 return delayed(() {
287 expect(changesReceived, hasLength(1));
288 expect(changesReceived[0].type, equals(ChangeType.ADD));
289 expect(changesReceived[0].path, equals(path));
290 });
291 }));
292
293 test('modify file', () {
294 var path = join(tempPath, 'foo');
295 var file = new io.File(path);
296 file.writeAsStringSync('contents 1');
297 return watchingFolder(tempPath, (changesReceived) {
298 expect(changesReceived, hasLength(0));
299 file.writeAsStringSync('contents 2');
300 return delayed(() {
301 expect(changesReceived, hasLength(1));
302 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
303 expect(changesReceived[0].path, equals(path));
304 });
305 });
306 });
307
308 test('modify file in subdir', () {
309 var subdirPath = join(tempPath, 'foo');
310 new io.Directory(subdirPath).createSync();
311 var path = join(tempPath, 'bar');
312 var file = new io.File(path);
313 file.writeAsStringSync('contents 1');
314 return watchingFolder(tempPath, (changesReceived) {
315 expect(changesReceived, hasLength(0));
316 file.writeAsStringSync('contents 2');
317 return delayed(() {
318 expect(changesReceived, hasLength(1));
319 expect(changesReceived[0].type, equals(ChangeType.MODIFY));
320 expect(changesReceived[0].path, equals(path));
321 });
322 });
323 });
324
325 test('delete file', () {
326 var path = join(tempPath, 'foo');
327 var file = new io.File(path);
328 file.writeAsStringSync('contents 1');
329 return watchingFolder(tempPath, (changesReceived) {
330 expect(changesReceived, hasLength(0));
331 file.deleteSync();
332 return delayed(() {
333 expect(changesReceived, hasLength(1));
334 expect(changesReceived[0].type, equals(ChangeType.REMOVE));
335 expect(changesReceived[0].path, equals(path));
336 });
337 });
338 });
339 });
340
253 group('File', () { 341 group('File', () {
254 String path; 342 String path;
255 File file; 343 File file;
256 344
257 setUp(() { 345 setUp(() {
258 path = join(tempPath, 'file.txt'); 346 path = join(tempPath, 'file.txt');
259 file = PhysicalResourceProvider.INSTANCE.getResource(path); 347 file = PhysicalResourceProvider.INSTANCE.getResource(path);
260 }); 348 });
261 349
262 test('createSource', () { 350 test('createSource', () {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 expect(children[1], _isFolder); 437 expect(children[1], _isFolder);
350 expect(children[2], _isFile); 438 expect(children[2], _isFile);
351 }); 439 });
352 }); 440 });
353 }); 441 });
354 } 442 }
355 443
356 var _isFile = new isInstanceOf<File>(); 444 var _isFile = new isInstanceOf<File>();
357 var _isFolder = new isInstanceOf<Folder>(); 445 var _isFolder = new isInstanceOf<Folder>();
358 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); 446 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>();
OLDNEW
« no previous file with comments | « pkg/analysis_server/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698