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

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

Issue 28553006: dart:io | Fix File.rename on Windows to overwrite an existing target. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix previously existing problem in test. Created 7 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « runtime/bin/file_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 // Dart test program for testing file I/O. 5 // Dart test program for testing file I/O.
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:collection'; 9 import 'dart:collection';
10 import 'dart:io'; 10 import 'dart:io';
(...skipping 1192 matching lines...) Expand 10 before | Expand all | Expand 10 after
1203 openedFile.setPositionSync(2); 1203 openedFile.setPositionSync(2);
1204 openedFile.writeStringSync(string); 1204 openedFile.writeStringSync(string);
1205 Expect.equals(4, openedFile.lengthSync()); 1205 Expect.equals(4, openedFile.lengthSync());
1206 openedFile.closeSync(); 1206 openedFile.closeSync();
1207 var readBack = file.readAsStringSync(); 1207 var readBack = file.readAsStringSync();
1208 Expect.stringEquals(readBack, '$string$string'); 1208 Expect.stringEquals(readBack, '$string$string');
1209 file.deleteSync(); 1209 file.deleteSync();
1210 Expect.isFalse(file.existsSync()); 1210 Expect.isFalse(file.existsSync());
1211 } 1211 }
1212 1212
1213 // Test that opens the same file for writing then for appending to test 1213 static void testRename({bool targetExists}) {
1214 // that the file is not truncated when opened for appending. 1214 lift(Function f) =>
1215 static void testRename() { 1215 (futureValue) => futureValue.then((value) => f(value));
1216 asyncTestStarted(); 1216 asyncTestStarted();
1217 var file = new File('${tempDirectory.path}/rename_name');
1218 file.create().then((file) {
1219 file.rename("${tempDirectory.path}/rename_newname").then((newfile) {
1220 file.exists().then((e) {
1221 Expect.isFalse(e);
1222 newfile.exists().then((e) {
1223 Expect.isTrue(e);
1224 newfile.delete().then((_) {
1225 file.exists().then((e) {
1226 Expect.isFalse(e);
1227 if (Platform.operatingSystem != "windows") {
1228 var brokenLink =
1229 new Link('${tempDirectory.path}/rename_name');
1230 brokenLink.create(
1231 '${tempDirectory.path}/rename_newname').then((_) {
1232 file.rename("xxx").then((_) {
1233 throw "Rename of broken link succeeded";
1234 }).catchError((e) {
1235 Expect.isTrue(e is FileException);
1236 asyncTestDone("testRename");
1237 });
1238 });
1239 1217
1240 } else { 1218 String source = join(tempDirectory.path, 'rename_${targetExists}_source');
1241 asyncTestDone("testRename"); 1219 String dest = join(tempDirectory.path, 'rename_${targetExists}_dest');
1242 } 1220 var file = new File(source);
1243 }); 1221 var newfile = new File(dest);
1222 file.create()
1223 .then((_) => targetExists ? newfile.create() : null)
1224 .then((_) => file.rename(dest))
1225 .then((_) => lift(Expect.isFalse)(file.exists()))
1226 .then((_) => lift(Expect.isTrue)(newfile.exists()))
1227 .then((_) => newfile.delete())
1228 .then((_) => lift(Expect.isFalse)(newfile.exists()))
1229 .then((_) {
1230 if (Platform.operatingSystem != "windows") {
1231 new Link(source).create(dest)
1232 .then((_) => file.rename("xxx"))
1233 .then((_) { throw "Rename of broken link succeeded"; })
1234 .catchError((e) {
1235 Expect.isTrue(e is FileException);
1236 asyncTestDone("testRename$targetExists");
1244 }); 1237 });
1245 }); 1238 } else {
1246 }); 1239 asyncTestDone("testRename$targetExists");
1240 }
1247 }); 1241 });
1248 });
1249 } 1242 }
1250 1243
1251 static void testRenameSync() { 1244 static void testRenameSync({bool targetExists}) {
1252 var file = new File('${tempDirectory.path}/rename_name_sync'); 1245 String source = join(tempDirectory.path, 'rename_source');
1246 String dest = join(tempDirectory.path, 'rename_dest');
1247 var file = new File(source);
1248 var newfile = new File(dest);
1253 file.createSync(); 1249 file.createSync();
1254 var newfile = file.renameSync('${tempDirectory.path}/rename_newname_sync'); 1250 if (targetExists) {
1251 newfile.createSync();
1252 }
1253 var result = file.renameSync(dest);
1255 Expect.isFalse(file.existsSync()); 1254 Expect.isFalse(file.existsSync());
1256 Expect.isTrue(newfile.existsSync()); 1255 Expect.isTrue(newfile.existsSync());
1256 Expect.equals(result.path, newfile.path);
1257 newfile.deleteSync(); 1257 newfile.deleteSync();
1258 Expect.isFalse(newfile.existsSync()); 1258 Expect.isFalse(newfile.existsSync());
1259 if (Platform.operatingSystem != "windows") { 1259 if (Platform.operatingSystem != "windows") {
1260 var brokenLink = new Link('${tempDirectory.path}/rename_name_sync'); 1260 var brokenLink = new Link(source);
1261 brokenLink.createSync('${tempDirectory.path}/rename_newname_sync'); 1261 brokenLink.createSync(dest);
1262 Expect.throws(() => file.renameSync('xxx')); 1262 Expect.throws(() => file.renameSync('xxx'));
1263 brokenLink.deleteSync();
1263 } 1264 }
1264 } 1265 }
1265 1266
1266 // Helper method to be able to run the test from the runtime 1267 // Helper method to be able to run the test from the runtime
1267 // directory, or the top directory. 1268 // directory, or the top directory.
1268 static String getFilename(String path) => 1269 static String getFilename(String path) =>
1269 new File(path).existsSync() ? path : 'runtime/$path'; 1270 new File(path).existsSync() ? path : 'runtime/$path';
1270 1271
1271 static String getDataFilename(String path) => 1272 static String getDataFilename(String path) =>
1272 new File(path).existsSync() ? path : '../$path'; 1273 new File(path).existsSync() ? path : '../$path';
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1313 testAppend(); 1314 testAppend();
1314 testAppendSync(); 1315 testAppendSync();
1315 testWriteAppend(); 1316 testWriteAppend();
1316 testOutputStreamWriteAppend(); 1317 testOutputStreamWriteAppend();
1317 testOutputStreamWriteString(); 1318 testOutputStreamWriteString();
1318 testWriteVariousLists(); 1319 testWriteVariousLists();
1319 testDirectory(); 1320 testDirectory();
1320 testDirectorySync(); 1321 testDirectorySync();
1321 testWriteStringUtf8(); 1322 testWriteStringUtf8();
1322 testWriteStringUtf8Sync(); 1323 testWriteStringUtf8Sync();
1323 testRename(); 1324 testRename(targetExists: false);
1324 testRenameSync(); 1325 testRenameSync(targetExists: false);
1326 testRename(targetExists: true);
1327 testRenameSync(targetExists: true);
1325 testLastModified(); 1328 testLastModified();
1326 testDoubleAsyncOperation(); 1329 testDoubleAsyncOperation();
1327 asyncEnd(); 1330 asyncEnd();
1328 }); 1331 });
1329 } 1332 }
1330 } 1333 }
1331 1334
1332 main() { 1335 main() {
1333 FileTest.testMain(); 1336 FileTest.testMain();
1334 } 1337 }
OLDNEW
« no previous file with comments | « runtime/bin/file_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698