| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 watcher.test.utils; | 5 library watcher.test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:path/path.dart'; | 10 import 'package:path/path.dart' as p; |
| 11 import 'package:scheduled_test/scheduled_test.dart'; | 11 import 'package:scheduled_test/scheduled_test.dart'; |
| 12 import 'package:unittest/compact_vm_config.dart'; | 12 import 'package:unittest/compact_vm_config.dart'; |
| 13 import 'package:watcher/watcher.dart'; | 13 import 'package:watcher/watcher.dart'; |
| 14 import 'package:watcher/src/stat.dart'; | 14 import 'package:watcher/src/stat.dart'; |
| 15 | 15 |
| 16 /// The path to the temporary sandbox created for each test. All file | 16 /// The path to the temporary sandbox created for each test. All file |
| 17 /// operations are implicitly relative to this directory. | 17 /// operations are implicitly relative to this directory. |
| 18 String _sandboxDir; | 18 String _sandboxDir; |
| 19 | 19 |
| 20 /// The [DirectoryWatcher] being used for the current scheduled test. | 20 /// The [DirectoryWatcher] being used for the current scheduled test. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 43 /// Creates the sandbox directory the other functions in this library use and | 43 /// Creates the sandbox directory the other functions in this library use and |
| 44 /// ensures it's deleted when the test ends. | 44 /// ensures it's deleted when the test ends. |
| 45 /// | 45 /// |
| 46 /// This should usually be called by [setUp]. | 46 /// This should usually be called by [setUp]. |
| 47 void createSandbox() { | 47 void createSandbox() { |
| 48 var dir = new Directory("").createTempSync(); | 48 var dir = new Directory("").createTempSync(); |
| 49 _sandboxDir = dir.path; | 49 _sandboxDir = dir.path; |
| 50 | 50 |
| 51 _mockFileModificationTimes = new Map<String, int>(); | 51 _mockFileModificationTimes = new Map<String, int>(); |
| 52 mockGetModificationTime((path) { | 52 mockGetModificationTime((path) { |
| 53 path = relative(path, from: _sandboxDir); | 53 path = p.relative(path, from: _sandboxDir); |
| 54 | 54 |
| 55 // Make sure we got a path in the sandbox. | 55 // Make sure we got a path in the sandbox. |
| 56 assert(isRelative(path) && !path.startsWith("..")); | 56 assert(p.isRelative(path) && !path.startsWith("..")); |
| 57 | 57 |
| 58 return new DateTime.fromMillisecondsSinceEpoch( | 58 return new DateTime.fromMillisecondsSinceEpoch( |
| 59 _mockFileModificationTimes[path]); | 59 _mockFileModificationTimes[path]); |
| 60 }); | 60 }); |
| 61 | 61 |
| 62 // Delete the sandbox when done. | 62 // Delete the sandbox when done. |
| 63 currentSchedule.onComplete.schedule(() { | 63 currentSchedule.onComplete.schedule(() { |
| 64 if (_sandboxDir != null) { | 64 if (_sandboxDir != null) { |
| 65 new Directory(_sandboxDir).deleteSync(recursive: true); | 65 new Directory(_sandboxDir).deleteSync(recursive: true); |
| 66 _sandboxDir = null; | 66 _sandboxDir = null; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 102 |
| 103 // Make sure the schedule is watching it in case it fails. | 103 // Make sure the schedule is watching it in case it fails. |
| 104 currentSchedule.wrapFuture(future); | 104 currentSchedule.wrapFuture(future); |
| 105 | 105 |
| 106 // Schedule it so that later file modifications don't occur until after this | 106 // Schedule it so that later file modifications don't occur until after this |
| 107 // event is received. | 107 // event is received. |
| 108 schedule(() => future); | 108 schedule(() => future); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void expectAddEvent(String path) { | 111 void expectAddEvent(String path) { |
| 112 expectEvent(ChangeType.ADD, join(_sandboxDir, path)); | 112 expectEvent(ChangeType.ADD, p.join(_sandboxDir, path)); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void expectModifyEvent(String path) { | 115 void expectModifyEvent(String path) { |
| 116 expectEvent(ChangeType.MODIFY, join(_sandboxDir, path)); | 116 expectEvent(ChangeType.MODIFY, p.join(_sandboxDir, path)); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void expectRemoveEvent(String path) { | 119 void expectRemoveEvent(String path) { |
| 120 expectEvent(ChangeType.REMOVE, join(_sandboxDir, path)); | 120 expectEvent(ChangeType.REMOVE, p.join(_sandboxDir, path)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 /// Schedules writing a file in the sandbox at [path] with [contents]. | 123 /// Schedules writing a file in the sandbox at [path] with [contents]. |
| 124 /// | 124 /// |
| 125 /// If [contents] is omitted, creates an empty file. If [updatedModified] is | 125 /// If [contents] is omitted, creates an empty file. If [updatedModified] is |
| 126 /// `false`, the mock file modification time is not changed. | 126 /// `false`, the mock file modification time is not changed. |
| 127 void writeFile(String path, {String contents, bool updateModified}) { | 127 void writeFile(String path, {String contents, bool updateModified}) { |
| 128 if (contents == null) contents = ""; | 128 if (contents == null) contents = ""; |
| 129 if (updateModified == null) updateModified = true; | 129 if (updateModified == null) updateModified = true; |
| 130 | 130 |
| 131 schedule(() { | 131 schedule(() { |
| 132 var fullPath = join(_sandboxDir, path); | 132 var fullPath = p.join(_sandboxDir, path); |
| 133 | 133 |
| 134 // Create any needed subdirectories. | 134 // Create any needed subdirectories. |
| 135 var dir = new Directory(dirname(fullPath)); | 135 var dir = new Directory(p.dirname(fullPath)); |
| 136 if (!dir.existsSync()) { | 136 if (!dir.existsSync()) { |
| 137 dir.createSync(recursive: true); | 137 dir.createSync(recursive: true); |
| 138 } | 138 } |
| 139 | 139 |
| 140 new File(fullPath).writeAsStringSync(contents); | 140 new File(fullPath).writeAsStringSync(contents); |
| 141 | 141 |
| 142 // Manually update the mock modification time for the file. | 142 // Manually update the mock modification time for the file. |
| 143 if (updateModified) { | 143 if (updateModified) { |
| 144 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); | 144 var milliseconds = _mockFileModificationTimes.putIfAbsent(path, () => 0); |
| 145 _mockFileModificationTimes[path]++; | 145 _mockFileModificationTimes[path]++; |
| 146 } | 146 } |
| 147 }); | 147 }); |
| 148 } | 148 } |
| 149 | 149 |
| 150 /// Schedules deleting a file in the sandbox at [path]. | 150 /// Schedules deleting a file in the sandbox at [path]. |
| 151 void deleteFile(String path) { | 151 void deleteFile(String path) { |
| 152 schedule(() { | 152 schedule(() { |
| 153 new File(join(_sandboxDir, path)).deleteSync(); | 153 new File(p.join(_sandboxDir, path)).deleteSync(); |
| 154 }); | 154 }); |
| 155 } | 155 } |
| 156 | 156 |
| 157 /// Schedules renaming a file in the sandbox from [from] to [to]. | 157 /// Schedules renaming a file in the sandbox from [from] to [to]. |
| 158 /// | 158 /// |
| 159 /// If [contents] is omitted, creates an empty file. | 159 /// If [contents] is omitted, creates an empty file. |
| 160 void renameFile(String from, String to) { | 160 void renameFile(String from, String to) { |
| 161 schedule(() { | 161 schedule(() { |
| 162 new File(join(_sandboxDir, from)).renameSync(join(_sandboxDir, to)); | 162 new File(p.join(_sandboxDir, from)).renameSync(p.join(_sandboxDir, to)); |
| 163 | 163 |
| 164 // Manually update the mock modification time for the file. | 164 // Manually update the mock modification time for the file. |
| 165 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); | 165 var milliseconds = _mockFileModificationTimes.putIfAbsent(to, () => 0); |
| 166 _mockFileModificationTimes[to]++; | 166 _mockFileModificationTimes[to]++; |
| 167 }); | 167 }); |
| 168 } | 168 } |
| 169 | 169 |
| 170 /// A [Matcher] for [WatchEvent]s. | 170 /// A [Matcher] for [WatchEvent]s. |
| 171 class _ChangeMatcher extends BaseMatcher { | 171 class _ChangeMatcher extends BaseMatcher { |
| 172 /// The expected change. | 172 /// The expected change. |
| 173 final ChangeType type; | 173 final ChangeType type; |
| 174 | 174 |
| 175 /// The expected path. | 175 /// The expected path. |
| 176 final String path; | 176 final String path; |
| 177 | 177 |
| 178 _ChangeMatcher(this.type, this.path); | 178 _ChangeMatcher(this.type, this.path); |
| 179 | 179 |
| 180 Description describe(Description description) { | 180 Description describe(Description description) { |
| 181 description.add("$type $path"); | 181 description.add("$type $path"); |
| 182 } | 182 } |
| 183 | 183 |
| 184 bool matches(item, Map matchState) => | 184 bool matches(item, Map matchState) => |
| 185 item is WatchEvent && item.type == type && item.path == path; | 185 item is WatchEvent && item.type == type && item.path == path; |
| 186 } | 186 } |
| OLD | NEW |