OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // SharedOptions=--supermixin | 4 // SharedOptions=--supermixin |
5 | 5 |
6 library front_end.test.physical_file_system_test; | 6 library front_end.test.physical_file_system_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 import 'dart:io' as io; | 10 import 'dart:io' as io; |
11 | 11 |
12 import 'package:front_end/file_system.dart'; | 12 import 'package:front_end/file_system.dart'; |
13 import 'package:front_end/physical_file_system.dart'; | 13 import 'package:front_end/physical_file_system.dart'; |
14 import 'package:path/path.dart' as p; | 14 import 'package:path/path.dart' as p; |
15 import 'package:test/test.dart'; | 15 import 'package:test/test.dart'; |
16 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 16 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
17 | 17 |
18 main() { | 18 main() { |
19 defineReflectiveSuite(() { | 19 defineReflectiveSuite(() { |
20 defineReflectiveTests(PhysicalFileSystemTest); | 20 defineReflectiveTests(PhysicalFileSystemTest); |
21 defineReflectiveTests(FileTest); | 21 defineReflectiveTests(FileTest); |
22 defineReflectiveTests(DirectoryTest); | 22 defineReflectiveTests(DirectoryTest); |
23 }); | 23 }); |
24 } | 24 } |
25 | 25 |
| 26 const Matcher _throwsFileSystemException = |
| 27 const Throws(const isInstanceOf<FileSystemException>()); |
| 28 |
| 29 @reflectiveTest |
| 30 class DirectoryTest extends _BaseTest { |
| 31 String path; |
| 32 FileSystemEntity dir; |
| 33 |
| 34 setUp() { |
| 35 super.setUp(); |
| 36 path = p.join(tempPath, 'dir'); |
| 37 dir = PhysicalFileSystem.instance.entityForUri(p.toUri(path)); |
| 38 } |
| 39 |
| 40 test_equals_differentPaths() { |
| 41 expect(dir == entityForPath(p.join(tempPath, 'dir2')), isFalse); |
| 42 } |
| 43 |
| 44 test_equals_samePath() { |
| 45 expect(dir == entityForPath(p.join(tempPath, 'dir')), isTrue); |
| 46 } |
| 47 |
| 48 test_exists_directoryExists() async { |
| 49 await new io.Directory(path).create(); |
| 50 expect(await dir.exists(), isTrue); |
| 51 } |
| 52 |
| 53 test_exists_doesNotExist() async { |
| 54 expect(await dir.exists(), isFalse); |
| 55 } |
| 56 |
| 57 test_readAsBytes() async { |
| 58 await new io.Directory(path).create(); |
| 59 expect(dir.readAsBytes(), _throwsFileSystemException); |
| 60 } |
| 61 |
| 62 test_uri() { |
| 63 expect(dir.uri, p.toUri(path)); |
| 64 } |
| 65 } |
| 66 |
26 @reflectiveTest | 67 @reflectiveTest |
27 class FileTest extends _BaseTest { | 68 class FileTest extends _BaseTest { |
28 String path; | 69 String path; |
29 FileSystemEntity file; | 70 FileSystemEntity file; |
30 | 71 |
31 setUp() { | 72 setUp() { |
32 super.setUp(); | 73 super.setUp(); |
33 path = p.join(tempPath, 'file.txt'); | 74 path = p.join(tempPath, 'file.txt'); |
34 file = PhysicalFileSystem.instance.entityForUri(p.toUri(path)); | 75 file = PhysicalFileSystem.instance.entityForUri(p.toUri(path)); |
35 } | 76 } |
36 | 77 |
37 test_equals_differentPaths() { | 78 test_equals_differentPaths() { |
38 expect(file == entityForPath(p.join(tempPath, 'file2.txt')), isFalse); | 79 expect(file == entityForPath(p.join(tempPath, 'file2.txt')), isFalse); |
39 } | 80 } |
40 | 81 |
41 test_equals_samePath() { | 82 test_equals_samePath() { |
42 expect(file == entityForPath(p.join(tempPath, 'file.txt')), isTrue); | 83 expect(file == entityForPath(p.join(tempPath, 'file.txt')), isTrue); |
43 } | 84 } |
44 | 85 |
45 test_hashCode_samePath() { | |
46 expect(file.hashCode, entityForPath(p.join(tempPath, 'file.txt')).hashCode); | |
47 } | |
48 | |
49 test_readAsBytes_badUtf8() async { | |
50 // A file containing invalid UTF-8 can still be read as raw bytes. | |
51 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8 | |
52 new io.File(path).writeAsBytesSync(bytes); | |
53 expect(await file.readAsBytes(), bytes); | |
54 } | |
55 | |
56 test_readAsBytes_doesNotExist() { | |
57 expect(file.readAsBytes(), throwsException); | |
58 } | |
59 | |
60 test_readAsBytes_exists() async { | |
61 var s = 'contents'; | |
62 new io.File(path).writeAsStringSync(s); | |
63 expect(await file.readAsBytes(), UTF8.encode(s)); | |
64 } | |
65 | |
66 test_readAsString_badUtf8() { | |
67 new io.File(path).writeAsBytesSync([0xc0, 0x40]); // Invalid UTF-8 | |
68 expect(file.readAsString(), throwsException); | |
69 } | |
70 | |
71 test_readAsString_doesNotExist() { | |
72 expect(file.readAsString(), throwsException); | |
73 } | |
74 | |
75 test_readAsString_exists() async { | |
76 var s = 'contents'; | |
77 new io.File(path).writeAsStringSync(s); | |
78 expect(await file.readAsString(), s); | |
79 } | |
80 | |
81 test_readAsString_utf8() async { | |
82 var bytes = [0xe2, 0x82, 0xac]; // Unicode € symbol (in UTF-8) | |
83 new io.File(path).writeAsBytesSync(bytes); | |
84 expect(await file.readAsString(), '\u20ac'); | |
85 } | |
86 | |
87 test_uri() { | |
88 expect(file.uri, p.toUri(path)); | |
89 } | |
90 | |
91 test_exists_doesNotExist() async { | 86 test_exists_doesNotExist() async { |
92 expect(await file.exists(), isFalse); | 87 expect(await file.exists(), isFalse); |
93 } | 88 } |
94 | 89 |
95 test_exists_fileExists() async { | 90 test_exists_fileExists() async { |
96 new io.File(path).writeAsStringSync('contents'); | 91 new io.File(path).writeAsStringSync('contents'); |
97 expect(await file.exists(), isTrue); | 92 expect(await file.exists(), isTrue); |
98 } | 93 } |
99 | 94 |
| 95 test_hashCode_samePath() { |
| 96 expect(file.hashCode, entityForPath(p.join(tempPath, 'file.txt')).hashCode); |
| 97 } |
| 98 |
| 99 test_lastModified_doesNotExist() { |
| 100 expect(file.lastModified(), _throwsFileSystemException); |
| 101 } |
| 102 |
100 test_lastModified_increasesOnEachChange() async { | 103 test_lastModified_increasesOnEachChange() async { |
101 new io.File(path).writeAsStringSync('contents1'); | 104 new io.File(path).writeAsStringSync('contents1'); |
102 var mod1 = await file.lastModified(); | 105 var mod1 = await file.lastModified(); |
103 | 106 |
104 // Pause to ensure the file-system time-stamps are different. | 107 // Pause to ensure the file-system time-stamps are different. |
105 await new Future.delayed(new Duration(seconds: 1)); | 108 await new Future.delayed(new Duration(seconds: 1)); |
106 new io.File(path).writeAsStringSync('contents2'); | 109 new io.File(path).writeAsStringSync('contents2'); |
107 var mod2 = await file.lastModified(); | 110 var mod2 = await file.lastModified(); |
108 expect(mod2.isAfter(mod1), isTrue); | 111 expect(mod2.isAfter(mod1), isTrue); |
109 | 112 |
110 await new Future.delayed(new Duration(seconds: 1)); | 113 await new Future.delayed(new Duration(seconds: 1)); |
111 var path2 = p.join(tempPath, 'file2.txt'); | 114 var path2 = p.join(tempPath, 'file2.txt'); |
112 new io.File(path2).writeAsStringSync('contents2'); | 115 new io.File(path2).writeAsStringSync('contents2'); |
113 var file2 = entityForPath(path2); | 116 var file2 = entityForPath(path2); |
114 var mod3 = await file2.lastModified(); | 117 var mod3 = await file2.lastModified(); |
115 expect(mod3.isAfter(mod2), isTrue); | 118 expect(mod3.isAfter(mod2), isTrue); |
116 } | 119 } |
117 } | |
118 | 120 |
119 @reflectiveTest | 121 test_readAsBytes_badUtf8() async { |
120 class DirectoryTest extends _BaseTest { | 122 // A file containing invalid UTF-8 can still be read as raw bytes. |
121 String path; | 123 List<int> bytes = [0xc0, 0x40]; // Invalid UTF-8 |
122 FileSystemEntity dir; | 124 new io.File(path).writeAsBytesSync(bytes); |
123 | 125 expect(await file.readAsBytes(), bytes); |
124 setUp() { | |
125 super.setUp(); | |
126 path = p.join(tempPath, 'dir'); | |
127 dir = PhysicalFileSystem.instance.entityForUri(p.toUri(path)); | |
128 } | 126 } |
129 | 127 |
130 test_equals_differentPaths() { | 128 test_readAsBytes_doesNotExist() { |
131 expect(dir == entityForPath(p.join(tempPath, 'dir2')), isFalse); | 129 expect(file.readAsBytes(), _throwsFileSystemException); |
132 } | 130 } |
133 | 131 |
134 test_equals_samePath() { | 132 test_readAsBytes_exists() async { |
135 expect(dir == entityForPath(p.join(tempPath, 'dir')), isTrue); | 133 var s = 'contents'; |
| 134 new io.File(path).writeAsStringSync(s); |
| 135 expect(await file.readAsBytes(), UTF8.encode(s)); |
136 } | 136 } |
137 | 137 |
138 test_readAsBytes() async { | 138 test_readAsString_badUtf8() { |
139 await new io.Directory(path).create(); | 139 new io.File(path).writeAsBytesSync([0xc0, 0x40]); // Invalid UTF-8 |
140 expect(dir.readAsBytes(), throwsException); | 140 expect(file.readAsString(), _throwsFileSystemException); |
| 141 } |
| 142 |
| 143 test_readAsString_doesNotExist() { |
| 144 expect(file.readAsString(), _throwsFileSystemException); |
| 145 } |
| 146 |
| 147 test_readAsString_exists() async { |
| 148 var s = 'contents'; |
| 149 new io.File(path).writeAsStringSync(s); |
| 150 expect(await file.readAsString(), s); |
| 151 } |
| 152 |
| 153 test_readAsString_utf8() async { |
| 154 var bytes = [0xe2, 0x82, 0xac]; // Unicode € symbol (in UTF-8) |
| 155 new io.File(path).writeAsBytesSync(bytes); |
| 156 expect(await file.readAsString(), '\u20ac'); |
141 } | 157 } |
142 | 158 |
143 test_uri() { | 159 test_uri() { |
144 expect(dir.uri, p.toUri(path)); | 160 expect(file.uri, p.toUri(path)); |
145 } | |
146 | |
147 test_exists_doesNotExist() async { | |
148 expect(await dir.exists(), isFalse); | |
149 } | |
150 | |
151 test_exists_directoryExists() async { | |
152 await new io.Directory(path).create(); | |
153 expect(await dir.exists(), isTrue); | |
154 } | 161 } |
155 } | 162 } |
156 | 163 |
157 @reflectiveTest | 164 @reflectiveTest |
158 class PhysicalFileSystemTest extends _BaseTest { | 165 class PhysicalFileSystemTest extends _BaseTest { |
159 Uri tempUri; | 166 Uri tempUri; |
160 | 167 |
161 setUp() { | 168 setUp() { |
162 super.setUp(); | 169 super.setUp(); |
163 tempUri = new Uri.directory(tempPath); | 170 tempUri = new Uri.directory(tempPath); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 tempDirectory.deleteSync(recursive: true); | 263 tempDirectory.deleteSync(recursive: true); |
257 } on io.FileSystemException { | 264 } on io.FileSystemException { |
258 // Sometimes on Windows the delete fails with errno 32 | 265 // Sometimes on Windows the delete fails with errno 32 |
259 // (ERROR_SHARING_VIOLATION: The process cannot access the file because it | 266 // (ERROR_SHARING_VIOLATION: The process cannot access the file because it |
260 // is being used by another process). Wait 1 second and try again. | 267 // is being used by another process). Wait 1 second and try again. |
261 await new Future.delayed(new Duration(seconds: 1)); | 268 await new Future.delayed(new Duration(seconds: 1)); |
262 tempDirectory.deleteSync(recursive: true); | 269 tempDirectory.deleteSync(recursive: true); |
263 } | 270 } |
264 } | 271 } |
265 } | 272 } |
OLD | NEW |