| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.physical.resource.provider; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io' as io; |
| 9 |
| 10 import 'mocks.dart'; |
| 11 |
| 12 import 'package:analysis_server/src/resource.dart'; |
| 13 import 'package:analyzer/src/generated/engine.dart' show TimestampedData; |
| 14 import 'package:analyzer/src/generated/source_io.dart'; |
| 15 import 'package:path/path.dart'; |
| 16 import 'package:unittest/unittest.dart'; |
| 17 import 'package:watcher/watcher.dart'; |
| 18 |
| 19 main() { |
| 20 groupSep = ' | '; |
| 21 |
| 22 group('PhysicalResourceProvider', () { |
| 23 io.Directory tempDirectory; |
| 24 String tempPath; |
| 25 |
| 26 setUp(() { |
| 27 tempDirectory = io.Directory.systemTemp.createTempSync('test_resource'); |
| 28 tempPath = tempDirectory.absolute.path; |
| 29 }); |
| 30 |
| 31 tearDown(() { |
| 32 tempDirectory.deleteSync(recursive: true); |
| 33 }); |
| 34 |
| 35 group('Watch', () { |
| 36 |
| 37 Future delayed(computation()) { |
| 38 // On Windows, watching the filesystem is accomplished by polling once |
| 39 // per second. So wait 2 seconds to give time for polling to reliably |
| 40 // occur. |
| 41 return new Future.delayed(new Duration(seconds: 2), computation); |
| 42 } |
| 43 |
| 44 watchingFolder(String path, test(List<WatchEvent> changesReceived)) { |
| 45 // Delay before we start watching the folder. This is necessary |
| 46 // because on MacOS, file modifications that occur just before we star
t |
| 47 // watching are sometimes misclassified as happening just after we |
| 48 // start watching. |
| 49 return delayed(() { |
| 50 Folder folder = PhysicalResourceProvider.INSTANCE.getResource(path); |
| 51 var changesReceived = <WatchEvent>[]; |
| 52 var subscription = folder.changes.listen(changesReceived.add); |
| 53 // Delay running the rest of the test to allow folder.changes to tak
e |
| 54 // a snapshot of the current directory state. Otherwise it won't be |
| 55 // able to reliably distinguish new files from modified ones. |
| 56 return delayed(() => test(changesReceived)).whenComplete(() { |
| 57 subscription.cancel(); |
| 58 }); |
| 59 }); |
| 60 } |
| 61 |
| 62 test('create file', () => watchingFolder(tempPath, (changesReceived) { |
| 63 expect(changesReceived, hasLength(0)); |
| 64 var path = join(tempPath, 'foo'); |
| 65 new io.File(path).writeAsStringSync('contents'); |
| 66 return delayed(() { |
| 67 expect(changesReceived, hasLength(1)); |
| 68 expect(changesReceived[0].type, equals(ChangeType.ADD)); |
| 69 expect(changesReceived[0].path, equals(path)); |
| 70 }); |
| 71 })); |
| 72 |
| 73 test('modify file', () { |
| 74 var path = join(tempPath, 'foo'); |
| 75 var file = new io.File(path); |
| 76 file.writeAsStringSync('contents 1'); |
| 77 return watchingFolder(tempPath, (changesReceived) { |
| 78 expect(changesReceived, hasLength(0)); |
| 79 file.writeAsStringSync('contents 2'); |
| 80 return delayed(() { |
| 81 expect(changesReceived, hasLength(1)); |
| 82 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
| 83 expect(changesReceived[0].path, equals(path)); |
| 84 }); |
| 85 }); |
| 86 }); |
| 87 |
| 88 test('modify file in subdir', () { |
| 89 var subdirPath = join(tempPath, 'foo'); |
| 90 new io.Directory(subdirPath).createSync(); |
| 91 var path = join(tempPath, 'bar'); |
| 92 var file = new io.File(path); |
| 93 file.writeAsStringSync('contents 1'); |
| 94 return watchingFolder(tempPath, (changesReceived) { |
| 95 expect(changesReceived, hasLength(0)); |
| 96 file.writeAsStringSync('contents 2'); |
| 97 return delayed(() { |
| 98 expect(changesReceived, hasLength(1)); |
| 99 expect(changesReceived[0].type, equals(ChangeType.MODIFY)); |
| 100 expect(changesReceived[0].path, equals(path)); |
| 101 }); |
| 102 }); |
| 103 }); |
| 104 |
| 105 test('delete file', () { |
| 106 var path = join(tempPath, 'foo'); |
| 107 var file = new io.File(path); |
| 108 file.writeAsStringSync('contents 1'); |
| 109 return watchingFolder(tempPath, (changesReceived) { |
| 110 expect(changesReceived, hasLength(0)); |
| 111 file.deleteSync(); |
| 112 return delayed(() { |
| 113 expect(changesReceived, hasLength(1)); |
| 114 expect(changesReceived[0].type, equals(ChangeType.REMOVE)); |
| 115 expect(changesReceived[0].path, equals(path)); |
| 116 }); |
| 117 }); |
| 118 }); |
| 119 }); |
| 120 |
| 121 group('File', () { |
| 122 String path; |
| 123 File file; |
| 124 |
| 125 setUp(() { |
| 126 path = join(tempPath, 'file.txt'); |
| 127 file = PhysicalResourceProvider.INSTANCE.getResource(path); |
| 128 }); |
| 129 |
| 130 test('createSource', () { |
| 131 new io.File(path).writeAsStringSync('contents'); |
| 132 var source = file.createSource(UriKind.FILE_URI); |
| 133 expect(source.uriKind, UriKind.FILE_URI); |
| 134 expect(source.exists(), isTrue); |
| 135 expect(source.contents.data, 'contents'); |
| 136 }); |
| 137 |
| 138 group('exists', () { |
| 139 test('false', () { |
| 140 expect(file.exists, isFalse); |
| 141 }); |
| 142 |
| 143 test('true', () { |
| 144 new io.File(path).writeAsStringSync('contents'); |
| 145 expect(file.exists, isTrue); |
| 146 }); |
| 147 }); |
| 148 |
| 149 test('fullName', () { |
| 150 expect(file.path, path); |
| 151 }); |
| 152 |
| 153 test('hashCode', () { |
| 154 file.hashCode; |
| 155 }); |
| 156 |
| 157 test('shortName', () { |
| 158 expect(file.shortName, 'file.txt'); |
| 159 }); |
| 160 |
| 161 test('toString', () { |
| 162 expect(file.toString(), path); |
| 163 }); |
| 164 }); |
| 165 |
| 166 group('Folder', () { |
| 167 String path; |
| 168 Folder folder; |
| 169 |
| 170 setUp(() { |
| 171 path = join(tempPath, 'folder'); |
| 172 new io.Directory(path).createSync(); |
| 173 folder = PhysicalResourceProvider.INSTANCE.getResource(path); |
| 174 }); |
| 175 |
| 176 group('getChild', () { |
| 177 test('does not exist', () { |
| 178 var child = folder.getChild('no-such-resource'); |
| 179 expect(child, isNotNull); |
| 180 expect(child.exists, isFalse); |
| 181 }); |
| 182 |
| 183 test('file', () { |
| 184 new io.File(join(path, 'myFile')).createSync(); |
| 185 var child = folder.getChild('myFile'); |
| 186 expect(child, _isFile); |
| 187 expect(child.exists, isTrue); |
| 188 }); |
| 189 |
| 190 test('folder', () { |
| 191 new io.Directory(join(path, 'myFolder')).createSync(); |
| 192 var child = folder.getChild('myFolder'); |
| 193 expect(child, _isFolder); |
| 194 expect(child.exists, isTrue); |
| 195 }); |
| 196 }); |
| 197 |
| 198 test('getChildren', () { |
| 199 // create 2 files and 1 folder |
| 200 new io.File(join(path, 'a.txt')).createSync(); |
| 201 new io.Directory(join(path, 'bFolder')).createSync(); |
| 202 new io.File(join(path, 'c.txt')).createSync(); |
| 203 // prepare 3 children |
| 204 List<Resource> children = folder.getChildren(); |
| 205 expect(children, hasLength(3)); |
| 206 children.sort((a, b) => a.shortName.compareTo(b.shortName)); |
| 207 // check that each child exists |
| 208 children.forEach((child) { |
| 209 expect(child.exists, true); |
| 210 }); |
| 211 // check names |
| 212 expect(children[0].shortName, 'a.txt'); |
| 213 expect(children[1].shortName, 'bFolder'); |
| 214 expect(children[2].shortName, 'c.txt'); |
| 215 // check types |
| 216 expect(children[0], _isFile); |
| 217 expect(children[1], _isFolder); |
| 218 expect(children[2], _isFile); |
| 219 }); |
| 220 }); |
| 221 }); |
| 222 } |
| 223 |
| 224 var _isFile = new isInstanceOf<File>(); |
| 225 var _isFolder = new isInstanceOf<Folder>(); |
| 226 var _isMemoryResourceException = new isInstanceOf<MemoryResourceException>(); |
| OLD | NEW |