OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import "dart:async"; |
| 6 import "dart:io"; |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 |
| 10 const String file1str = "file1"; |
| 11 |
| 12 void doTestSync() { |
| 13 // Stuff that should exist. |
| 14 Directory dir1 = new Directory("/dir1"); |
| 15 Directory dir2 = new Directory("/dir1/dir2"); |
| 16 File file1 = new File("/dir1/dir2/file1"); |
| 17 |
| 18 Expect.isTrue(dir1.existsSync()); |
| 19 Expect.isTrue(dir2.existsSync()); |
| 20 Expect.isTrue(file1.existsSync()); |
| 21 Expect.equals(file1str, file1.readAsStringSync()); |
| 22 |
| 23 // Relative paths since cwd of the namespace should be "/". |
| 24 Directory dir1rel = new Directory("dir1"); |
| 25 Directory dir2rel = new Directory("dir1/dir2"); |
| 26 File file1rel = new File("dir1/dir2/file1"); |
| 27 |
| 28 Expect.equals("/", Directory.current.path); |
| 29 Expect.isTrue(dir1rel.existsSync()); |
| 30 Expect.isTrue(dir2rel.existsSync()); |
| 31 Expect.isTrue(file1rel.existsSync()); |
| 32 Expect.equals(file1str, file1.readAsStringSync()); |
| 33 |
| 34 // Stuff that should not exist. |
| 35 Expect.isFalse(new Directory("/tmp").existsSync()); |
| 36 Expect.isFalse(new File("/tmp").existsSync()); |
| 37 Expect.isFalse(new File(Platform.script.path).existsSync()); |
| 38 Expect.isFalse(new File(Platform.executable).existsSync()); |
| 39 Expect.isFalse(new File(Platform.resolvedExecutable).existsSync()); |
| 40 |
| 41 // File operations in the namespace. |
| 42 // copy. |
| 43 File file2 = file1.copySync("/file2"); |
| 44 Expect.isTrue(file2.existsSync()); |
| 45 Expect.equals(file1.readAsStringSync(), file2.readAsStringSync()); |
| 46 // create. |
| 47 File file3 = new File("/file3")..createSync(); |
| 48 Expect.isTrue(file3.existsSync()); |
| 49 // last{Accessed,Modified}. |
| 50 DateTime time = new DateTime.fromMillisecondsSinceEpoch(0); |
| 51 file2.setLastAccessedSync(time); |
| 52 file2.setLastModifiedSync(time); |
| 53 Expect.equals(time, file2.lastAccessedSync()); |
| 54 Expect.equals(time, file2.lastModifiedSync()); |
| 55 Expect.equals(file1str.length, file2.lengthSync()); |
| 56 // open. |
| 57 RandomAccessFile file2raf = file2.openSync(); |
| 58 Expect.equals(file1str.codeUnitAt(0), file2raf.readByteSync()); |
| 59 file2raf.closeSync(); |
| 60 // rename. |
| 61 File file4 = new File("file4"); |
| 62 file3.renameSync(file4.path); |
| 63 Expect.isFalse(file3.existsSync()); |
| 64 Expect.isTrue(file4.existsSync()); |
| 65 // delete. |
| 66 file4.deleteSync(); |
| 67 Expect.isFalse(file4.existsSync()); |
| 68 // stat. |
| 69 FileStat stat = file2.statSync(); |
| 70 Expect.equals(time, stat.modified); |
| 71 |
| 72 // Directory operaions in the namespace. |
| 73 // absolute. |
| 74 Expect.equals(dir1.path, dir1rel.absolute.path); |
| 75 // create |
| 76 Directory dir3 = new Directory("/dir3"); |
| 77 dir3.createSync(); |
| 78 Expect.isTrue(dir3.existsSync()); |
| 79 // createTemp |
| 80 Directory dir3temp = dir3.createTempSync(); |
| 81 Expect.isTrue(dir3temp.existsSync()); |
| 82 // listSync |
| 83 List fses = Directory.current.listSync(); |
| 84 Expect.isTrue(fses.any((fse) => fse.path == dir3.path)); |
| 85 // rename. |
| 86 Directory dir4 = new Directory("dir4"); |
| 87 dir3.renameSync(dir4.path); |
| 88 Expect.isTrue(dir4.existsSync()); |
| 89 // delete. |
| 90 dir4.deleteSync(recursive: true); |
| 91 Expect.isFalse(dir4.existsSync()); |
| 92 // stat. |
| 93 FileStat dirstat = dir2.statSync(); |
| 94 Expect.equals(FileSystemEntityType.DIRECTORY, dirstat.type); |
| 95 } |
| 96 |
| 97 doTestAsync() async { |
| 98 // Stuff that should exist. |
| 99 Directory dir1 = new Directory("/dir1"); |
| 100 Directory dir2 = new Directory("/dir1/dir2"); |
| 101 File file1 = new File("/dir1/dir2/file1"); |
| 102 |
| 103 Expect.isTrue(await dir1.exists()); |
| 104 Expect.isTrue(await dir2.exists()); |
| 105 Expect.isTrue(await file1.exists()); |
| 106 Expect.equals(file1str, await file1.readAsString()); |
| 107 |
| 108 // Relative paths since cwd of the namespace should be "/". |
| 109 Directory dir1rel = new Directory("dir1"); |
| 110 Directory dir2rel = new Directory("dir1/dir2"); |
| 111 File file1rel = new File("dir1/dir2/file1"); |
| 112 |
| 113 Expect.equals("/", Directory.current.path); |
| 114 Expect.isTrue(await dir1rel.exists()); |
| 115 Expect.isTrue(await dir2rel.exists()); |
| 116 Expect.isTrue(await file1rel.exists()); |
| 117 Expect.equals(file1str, await file1.readAsString()); |
| 118 |
| 119 // Stuff that should not exist. |
| 120 Expect.isFalse(await new Directory("/tmp").exists()); |
| 121 Expect.isFalse(await new File("/tmp").exists()); |
| 122 Expect.isFalse(await new File(Platform.script.path).exists()); |
| 123 Expect.isFalse(await new File(Platform.executable).exists()); |
| 124 Expect.isFalse(await new File(Platform.resolvedExecutable).exists()); |
| 125 |
| 126 // File operations in the namespace. |
| 127 // copy. |
| 128 File file2 = await file1.copy("/file2"); |
| 129 Expect.isTrue(await file2.exists()); |
| 130 Expect.equals(await file1.readAsString(), await file2.readAsString()); |
| 131 // create. |
| 132 File file3 = new File("/file3"); |
| 133 await file3.create(); |
| 134 Expect.isTrue(await file3.exists()); |
| 135 // last{Accessed,Modified}. |
| 136 DateTime time = new DateTime.fromMillisecondsSinceEpoch(0); |
| 137 await file2.setLastAccessed(time); |
| 138 await file2.setLastModified(time); |
| 139 Expect.equals(time, await file2.lastAccessed()); |
| 140 Expect.equals(time, await file2.lastModified()); |
| 141 Expect.equals(file1str.length, await file2.length()); |
| 142 // open. |
| 143 RandomAccessFile file2raf = await file2.open(); |
| 144 Expect.equals(file1str.codeUnitAt(0), await file2raf.readByte()); |
| 145 await file2raf.close(); |
| 146 // rename. |
| 147 File file4 = new File("file4"); |
| 148 await file3.rename(file4.path); |
| 149 Expect.isFalse(await file3.exists()); |
| 150 Expect.isTrue(await file4.exists()); |
| 151 // delete. |
| 152 await file4.delete(); |
| 153 Expect.isFalse(await file4.exists()); |
| 154 // stat. |
| 155 FileStat stat = await file2.stat(); |
| 156 Expect.equals(time, stat.modified); |
| 157 |
| 158 // Directory operaions in the namespace. |
| 159 // absolute. |
| 160 Expect.equals(dir1.path, dir1rel.absolute.path); |
| 161 // create |
| 162 Directory dir3 = new Directory("/dir3"); |
| 163 await dir3.create(); |
| 164 Expect.isTrue(await dir3.exists()); |
| 165 // createTemp |
| 166 Directory dir3temp = await dir3.createTemp(); |
| 167 Expect.isTrue(await dir3temp.exists()); |
| 168 // listSync |
| 169 List fses = await Directory.current.list().toList(); |
| 170 Expect.isTrue(fses.any((fse) => fse.path == dir3.path)); |
| 171 // rename. |
| 172 Directory dir4 = new Directory("dir4"); |
| 173 dir3.renameSync(dir4.path); |
| 174 Expect.isTrue(await dir4.exists()); |
| 175 // delete. |
| 176 dir4.deleteSync(recursive: true); |
| 177 Expect.isFalse(await dir4.exists()); |
| 178 // stat. |
| 179 FileStat dirstat = await dir2.stat(); |
| 180 Expect.equals(FileSystemEntityType.DIRECTORY, dirstat.type); |
| 181 } |
| 182 |
| 183 List<String> packageOptions() { |
| 184 if (Platform.packageRoot != null) { |
| 185 return <String>["--package-root=${Platform.packageRoot}"]; |
| 186 } else if (Platform.packageConfig != null) { |
| 187 return <String>["--packages=${Platform.packageConfig}"]; |
| 188 } else { |
| 189 return <String>[]; |
| 190 } |
| 191 } |
| 192 |
| 193 void setupTest() { |
| 194 // Create a namespace in /tmp. |
| 195 Directory namespace = Directory.systemTemp.createTempSync("namespace"); |
| 196 try { |
| 197 // Create some stuff that should be visible. |
| 198 Directory dir1 = new Directory("${namespace.path}/dir1")..createSync(); |
| 199 Directory dir2 = new Directory("${dir1.path}/dir2")..createSync(); |
| 200 File file1 = new File("${dir2.path}/file1") |
| 201 ..createSync() |
| 202 ..writeAsStringSync(file1str); |
| 203 |
| 204 // Run the test and capture stdout. |
| 205 var args = packageOptions(); |
| 206 args.addAll([ |
| 207 "--namespace=${namespace.path}", |
| 208 Platform.script.toFilePath(), |
| 209 "--run" |
| 210 ]); |
| 211 var pr = Process.runSync(Platform.executable, args); |
| 212 if (pr.exitCode != 0) { |
| 213 print("stdout:\n${pr.stdout}"); |
| 214 print("stderr:\n${pr.stderr}"); |
| 215 } |
| 216 Expect.equals(0, pr.exitCode); |
| 217 } finally { |
| 218 namespace.deleteSync(recursive: true); |
| 219 } |
| 220 } |
| 221 |
| 222 main(List<String> arguments) async { |
| 223 if (!Platform.isLinux) { |
| 224 return; |
| 225 } |
| 226 if (arguments.contains("--run")) { |
| 227 doTestSync(); |
| 228 await doTestAsync(); |
| 229 } else { |
| 230 setupTest(); |
| 231 } |
| 232 } |
OLD | NEW |