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

Side by Side Diff: tests/standalone/io/namespace_test.dart

Issue 3001963002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Fuchsia fix Created 3 years, 3 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
« no previous file with comments | « tests/standalone/io/directory_test.dart ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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")..createSync()
201 ..writeAsStringSync(file1str);
202
203 // Run the test and capture stdout.
204 var args = packageOptions();
205 args.addAll([
206 "--namespace=${namespace.path}",
207 Platform.script.toFilePath(),
208 "--run"
209 ]);
210 var pr = Process.runSync(Platform.executable, args);
211 if (pr.exitCode != 0) {
212 print("stdout:\n${pr.stdout}");
213 print("stderr:\n${pr.stderr}");
214 }
215 Expect.equals(0, pr.exitCode);
216 } finally {
217 namespace.deleteSync(recursive: true);
218 }
219 }
220
221 main(List<String> arguments) async {
222 if (!Platform.isLinux) {
223 return;
224 }
225 if (arguments.contains("--run")) {
226 doTestSync();
227 await doTestAsync();
228 } else {
229 setupTest();
230 }
231 }
OLDNEW
« no previous file with comments | « tests/standalone/io/directory_test.dart ('k') | tests/standalone/standalone.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698