From 8b30049c888ef151a74880a5691f0caaf23df140 Mon Sep 17 00:00:00 2001 From: "Sergey G. Grekhov" Date: Tue, 14 Nov 2023 11:21:44 +0200 Subject: [PATCH] #2303. Add more File.create()/createSync() tests (#2337) Add more File.create() tests, check all possible parameters, add tests checking file behavior on links, add check if the link is deleted. --- LibTest/io/File/createSync_A01_t01.dart | 27 +++++++---- LibTest/io/File/createSync_A02_t01.dart | 38 ++++++++++----- LibTest/io/File/createSync_A02_t02.dart | 34 +++++++++---- LibTest/io/File/createSync_A03_t01.dart | 37 ++++++++++----- LibTest/io/File/createSync_A03_t02.dart | 47 ++++++++++++++++++ LibTest/io/File/createSync_A03_t03.dart | 59 +++++++++++++++++++++++ LibTest/io/File/createSync_A03_t04.dart | 58 +++++++++++++++++++++++ LibTest/io/File/createSync_A04_t01.dart | 43 ++++++++++++----- LibTest/io/File/createSync_A04_t02.dart | 54 +++++++++++++++++++++ LibTest/io/File/create_A01_t01.dart | 23 ++++++--- LibTest/io/File/create_A02_t01.dart | 39 ++++++++++----- LibTest/io/File/create_A02_t02.dart | 36 +++++++++----- LibTest/io/File/create_A03_t01.dart | 37 ++++++++++----- LibTest/io/File/create_A03_t02.dart | 54 +++++++++++++++++++++ LibTest/io/File/create_A03_t03.dart | 63 +++++++++++++++++++++++++ LibTest/io/File/create_A03_t04.dart | 63 +++++++++++++++++++++++++ LibTest/io/File/create_A04_t01.dart | 46 +++++++++++++----- LibTest/io/File/create_A04_t02.dart | 60 +++++++++++++++++++++++ 18 files changed, 710 insertions(+), 108 deletions(-) create mode 100644 LibTest/io/File/createSync_A03_t02.dart create mode 100644 LibTest/io/File/createSync_A03_t03.dart create mode 100644 LibTest/io/File/createSync_A03_t04.dart create mode 100644 LibTest/io/File/createSync_A04_t02.dart create mode 100644 LibTest/io/File/create_A03_t02.dart create mode 100644 LibTest/io/File/create_A03_t03.dart create mode 100644 LibTest/io/File/create_A03_t04.dart create mode 100644 LibTest/io/File/create_A04_t02.dart diff --git a/LibTest/io/File/createSync_A01_t01.dart b/LibTest/io/File/createSync_A01_t01.dart index ebb38246e7..c9071b8893 100644 --- a/LibTest/io/File/createSync_A01_t01.dart +++ b/LibTest/io/File/createSync_A01_t01.dart @@ -2,16 +2,25 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion void createSync({bool recursive: false}) -/// Synchronously create the file. Existing files are left untouched by -/// createSync. Calling createSync on an existing file might fail if there are -/// restrictive permissions on the file. +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. /// /// Throws a FileSystemException if the operation fails. +/// /// @description Checks that this method creates the file /// @author sgrekhov@unipro.ru @@ -19,11 +28,11 @@ import "dart:io"; import "../../../Utils/expect.dart"; import "../file_utils.dart"; -main() async { - await inSandbox(_main); +main() { + inSandbox(_main); } -_main(Directory sandbox) async { +_main(Directory sandbox) { File file = new File(getTempFilePath(parent: sandbox)); file.createSync(); Expect.isTrue(file.existsSync()); diff --git a/LibTest/io/File/createSync_A02_t01.dart b/LibTest/io/File/createSync_A02_t01.dart index 99dfc37807..295a371405 100644 --- a/LibTest/io/File/createSync_A02_t01.dart +++ b/LibTest/io/File/createSync_A02_t01.dart @@ -2,33 +2,47 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion void createSync({bool recursive: false}) -/// Synchronously create the file. Existing files are left untouched by -/// createSync. Calling createSync on an existing file might fail if there are -/// restrictive permissions on the file. +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. /// /// Throws a FileSystemException if the operation fails. -/// @description Checks that if recursive is false and there are non-existing -/// path components, then operation fails +/// +/// @description Checks that if `recursive` is `false` and there are +/// non-existing path components, then operation fails /// @author sgrekhov@unipro.ru import "dart:io"; import "../../../Utils/expect.dart"; import "../file_utils.dart"; -main() async { - await inSandbox(_main); +main() { + inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool exclusive = false}) { String dirPath = getTempDirectoryPath(parent: sandbox); String filePath = dirPath + Platform.pathSeparator + getTempFileName(); File file = new File(filePath); Expect.throws(() { - file.createSync(recursive: false); + file.createSync(recursive: false, exclusive: exclusive); }, (e) => e is FileSystemException); } + +_main(Directory sandbox) { + _test(sandbox, exclusive: false); + _test(sandbox, exclusive: true); +} diff --git a/LibTest/io/File/createSync_A02_t02.dart b/LibTest/io/File/createSync_A02_t02.dart index 4607e330cc..66160e1ed5 100644 --- a/LibTest/io/File/createSync_A02_t02.dart +++ b/LibTest/io/File/createSync_A02_t02.dart @@ -2,16 +2,25 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion void createSync({bool recursive: false}) -/// Synchronously create the file. Existing files are left untouched by -/// createSync. Calling createSync on an existing file might fail if there are -/// restrictive permissions on the file. +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. /// /// Throws a FileSystemException if the operation fails. +/// /// @description Checks that if recursive is true, all non-existing path /// components are created /// @author sgrekhov@unipro.ru @@ -20,15 +29,20 @@ import "dart:io"; import "../../../Utils/expect.dart"; import "../file_utils.dart"; -main() async { - await inSandbox(_main); +main() { + inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool exclusive = false}) { String dirPath = getTempDirectoryPath(parent: sandbox); String filePath = dirPath + Platform.pathSeparator + getTempFileName(); File file = new File(filePath); - file.createSync(recursive: true); + file.createSync(recursive: true, exclusive: exclusive); Expect.isTrue(file.existsSync()); Expect.equals(filePath, file.path); } + +_main(Directory sandbox) { + _test(sandbox, exclusive: false); + _test(sandbox, exclusive: true); +} diff --git a/LibTest/io/File/createSync_A03_t01.dart b/LibTest/io/File/createSync_A03_t01.dart index 6534a9353e..fa57b51fcb 100644 --- a/LibTest/io/File/createSync_A03_t01.dart +++ b/LibTest/io/File/createSync_A03_t01.dart @@ -2,34 +2,49 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion void createSync({bool recursive: false}) -/// Synchronously create the file. Existing files are left untouched by -/// createSync. Calling createSync on an existing file might fail if there are -/// restrictive permissions on the file. +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. /// /// Throws a FileSystemException if the operation fails. -/// @description Checks that existing files are left untouched by create +/// +/// @description Checks that if `exclusive` is `false` then existing files are +/// left untouched by createSync /// @author sgrekhov@unipro.ru import "dart:io"; import "../../../Utils/expect.dart"; import "../file_utils.dart"; -main() async { - await inSandbox(_main); +main() { + inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool recursive = false}) async { File tmp = getTempFileSync(parent: sandbox); tmp.writeAsStringSync("Existing file content"); File file = new File(tmp.path); - file.createSync(); + file.createSync(recursive: recursive); Expect.isTrue(file.existsSync()); Expect.equals(tmp.path, file.path); Expect.equals("Existing file content", file.readAsStringSync()); } + +_main(Directory sandbox) { + _test(sandbox, recursive: false); + _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/createSync_A03_t02.dart b/LibTest/io/File/createSync_A03_t02.dart new file mode 100644 index 0000000000..28da34c5d5 --- /dev/null +++ b/LibTest/io/File/createSync_A03_t02.dart @@ -0,0 +1,47 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. +/// +/// Throws a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `true` and to-be-created file +/// already exists, then a [PathExistsException] is thrown. +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() { + inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) async { + File tmp = getTempFileSync(parent: sandbox); + File file = new File(tmp.path); + Expect.throws(() { + file.createSync(exclusive: true, recursive: recursive); + }, (e) => e is PathExistsException); +} + +_main(Directory sandbox) { + _test(sandbox, recursive: false); + _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/createSync_A03_t03.dart b/LibTest/io/File/createSync_A03_t03.dart new file mode 100644 index 0000000000..c5a341293a --- /dev/null +++ b/LibTest/io/File/createSync_A03_t03.dart @@ -0,0 +1,59 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. +/// +/// Throws a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `false`, existing files are left +/// untouched by create. Test [Link] pointing to a file +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) { + File target = getTempFileSync(parent: sandbox); + target.writeAsStringSync("Target content"); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = File(link.path); + file.createSync(recursive: recursive); + Expect.isTrue(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.equals(file.path, link.path); + // Now check that all read/write operations are performed on link's target + Expect.equals("Target content", file.readAsStringSync()); + file.writeAsStringSync("Lily was here"); + Expect.equals("Lily was here", target.readAsStringSync()); + // Delete doesn't delete the target of the link but the link itself + file.deleteSync(); + Expect.isFalse(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.isFalse(link.existsSync()); +} + +_main(Directory sandbox) { + _test(sandbox, recursive: false); + _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/createSync_A03_t04.dart b/LibTest/io/File/createSync_A03_t04.dart new file mode 100644 index 0000000000..d50b0da64e --- /dev/null +++ b/LibTest/io/File/createSync_A03_t04.dart @@ -0,0 +1,58 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. +/// +/// Throws a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `false`, existing files are left +/// untouched by create. Test [Link] pointing to a not existing entity +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) { + File target = File(getTempFilePath(parent: sandbox)); + Expect.isFalse(target.existsSync()); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = File(link.path); + file.createSync(recursive: recursive); + Expect.isTrue(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.equals(file.path, link.path); + // Now check that all read/write operations are performed on link's target + file.writeAsStringSync("Lily was here"); + Expect.equals("Lily was here", target.readAsStringSync()); + // Delete doesn't delete the target of the link but the link itself + file.deleteSync(); + Expect.isFalse(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.isFalse(link.existsSync()); +} + +_main(Directory sandbox) { + _test(sandbox, recursive: false); + _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/createSync_A04_t01.dart b/LibTest/io/File/createSync_A04_t01.dart index f5448a1e39..771814a8c9 100644 --- a/LibTest/io/File/createSync_A04_t01.dart +++ b/LibTest/io/File/createSync_A04_t01.dart @@ -2,32 +2,51 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion void createSync({bool recursive: false}) -/// Synchronously create the file. Existing files are left untouched by -/// createSync. Calling createSync on an existing file might fail if there are -/// restrictive permissions on the file. +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. /// /// Throws a FileSystemException if the operation fails. -/// @description Checks that a FileSystemException is thrown if the operation -/// fails +/// +/// @description Checks that a [FileSystemException] is thrown if the operation +/// fails. Test the case when there is an existing directory with the same path /// @author sgrekhov@unipro.ru import "dart:io"; import "../../../Utils/expect.dart"; import "../file_utils.dart"; -main() async { - await inSandbox(_main); +main() { + inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool recursive = false, bool exclusive = false}) { Directory dir = getTempDirectorySync(parent: sandbox); File file = new File(dir.path); Expect.throws(() { file.createSync(); - }, (e) => e is FileSystemException); + }, + (e) => e is FileSystemException, + "FileSystemException expected. " + "Resursive=$recursive, exclusive=$exclusive"); +} + +_main(Directory sandbox) { + _test(sandbox, recursive: false, exclusive: false); + _test(sandbox, recursive: false, exclusive: true); + _test(sandbox, recursive: true, exclusive: false); + _test(sandbox, recursive: true, exclusive: true); } diff --git a/LibTest/io/File/createSync_A04_t02.dart b/LibTest/io/File/createSync_A04_t02.dart new file mode 100644 index 0000000000..14f17f8d74 --- /dev/null +++ b/LibTest/io/File/createSync_A04_t02.dart @@ -0,0 +1,54 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion void createSync( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Synchronously creates the file. +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, all +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, a +/// PathExistsException is thrown. +/// +/// If exclusive is false, existing files are left untouched by createSync. +/// Calling createSync on an existing file still might fail if there are +/// restrictive permissions on the file. +/// +/// Throws a FileSystemException if the operation fails. +/// +/// @description Checks that a [FileSystemException] is thrown if the operation +/// fails. Test the case when there is an existing link to a directory on the +/// path +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false, bool exclusive = false}) { + Directory target = getTempDirectorySync(parent: sandbox); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = new File(link.path); + Expect.throws(() { + file.createSync(); + }, + (e) => e is FileSystemException, + "FileSystemException expected. " + "Resursive=$recursive, exclusive=$exclusive"); +} + +_main(Directory sandbox) { + _test(sandbox, recursive: false, exclusive: false); + _test(sandbox, recursive: false, exclusive: true); + _test(sandbox, recursive: true, exclusive: false); + _test(sandbox, recursive: true, exclusive: true); +} diff --git a/LibTest/io/File/create_A01_t01.dart b/LibTest/io/File/create_A01_t01.dart index e49577c5f3..58c52664b2 100644 --- a/LibTest/io/File/create_A01_t01.dart +++ b/LibTest/io/File/create_A01_t01.dart @@ -2,18 +2,27 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion Future create({bool recursive: false}) -/// Create the file. Returns a Future that completes with the file when it -/// has been created. +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. /// -/// Existing files are left untouched by create. Calling create on an existing -/// file might fail if there are restrictive permissions on the file. +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. /// /// Completes the future with a FileSystemException if the operation fails. +/// /// @description Checks that this method creates the file /// @author sgrekhov@unipro.ru diff --git a/LibTest/io/File/create_A02_t01.dart b/LibTest/io/File/create_A02_t01.dart index 43423242c1..39bf835ddf 100644 --- a/LibTest/io/File/create_A02_t01.dart +++ b/LibTest/io/File/create_A02_t01.dart @@ -2,20 +2,29 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion Future create({bool recursive: false}) -/// Create the file. Returns a Future that completes with the file when it -/// has been created. +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. /// -/// Existing files are left untouched by create. Calling create on an existing -/// file might fail if there are restrictive permissions on the file. +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. /// /// Completes the future with a FileSystemException if the operation fails. -/// @description Checks that if recursive is false and there are non-existing -/// path components, then operation fails +/// +/// @description Checks that if `recursive` is `false` and there are +/// non-existing path components, then operation fails /// @author sgrekhov@unipro.ru import "dart:io"; @@ -26,15 +35,21 @@ main() async { await inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool exclusive = false}) async { String dirPath = getTempDirectoryPath(parent: sandbox); String filePath = dirPath + Platform.pathSeparator + getTempFileName(); File file = new File(filePath); - asyncStart(); - await file.create(recursive: false).then((File created) { + await file.create(recursive: false, exclusive: exclusive).then( + (File created) { Expect.fail("FileSystemException is expected"); }, onError: (e) { Expect.isTrue(e is FileSystemException); asyncEnd(); }); } + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, exclusive: false); + await _test(sandbox, exclusive: true); +} diff --git a/LibTest/io/File/create_A02_t02.dart b/LibTest/io/File/create_A02_t02.dart index c5365f00a8..f47e9ddd3a 100644 --- a/LibTest/io/File/create_A02_t02.dart +++ b/LibTest/io/File/create_A02_t02.dart @@ -2,19 +2,28 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion Future create({bool recursive: false}) -/// Create the file. Returns a Future that completes with the file when it -/// has been created. +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. /// -/// Existing files are left untouched by create. Calling create on an existing -/// file might fail if there are restrictive permissions on the file. +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. /// /// Completes the future with a FileSystemException if the operation fails. -/// @description Checks that if recursive is true, all non-existing path +/// +/// @description Checks that if `recursive` is `true`, all non-existing path /// components are created /// @author sgrekhov@unipro.ru @@ -26,14 +35,19 @@ main() async { await inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool exclusive = false}) async { String dirPath = getTempDirectoryPath(parent: sandbox); String filePath = dirPath + Platform.pathSeparator + getTempFileName(); File file = new File(filePath); - asyncStart(); - await file.create(recursive: true).then((File created) { + await file.create(recursive: true, exclusive: exclusive).then((File created) { Expect.isTrue(created.existsSync()); Expect.equals(file.path, created.path); asyncEnd(); }); } + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, exclusive: false); + await _test(sandbox, exclusive: true); +} diff --git a/LibTest/io/File/create_A03_t01.dart b/LibTest/io/File/create_A03_t01.dart index 6db4463fdb..fe504a3994 100644 --- a/LibTest/io/File/create_A03_t01.dart +++ b/LibTest/io/File/create_A03_t01.dart @@ -2,19 +2,29 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion Future create({bool recursive: false}) -/// Create the file. Returns a Future that completes with the file when it -/// has been created. +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. /// -/// Existing files are left untouched by create. Calling create on an existing -/// file might fail if there are restrictive permissions on the file. +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. /// /// Completes the future with a FileSystemException if the operation fails. -/// @description Checks that existing files are left untouched by create +/// +/// @description Checks that if `exclusive` is `false`, existing files are left +/// untouched by create. /// @author sgrekhov@unipro.ru import "dart:io"; @@ -25,15 +35,20 @@ main() async { await inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, {bool recursive = false}) async { File tmp = getTempFileSync(parent: sandbox); tmp.writeAsStringSync("Existing file content"); File file = new File(tmp.path); - asyncStart(); - await file.create().then((File created) { + await file.create(recursive: recursive).then((File created) { Expect.isTrue(created.existsSync()); Expect.equals(tmp.path, created.path); Expect.equals("Existing file content", created.readAsStringSync()); asyncEnd(); }); } + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, recursive: false); + await _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/create_A03_t02.dart b/LibTest/io/File/create_A03_t02.dart new file mode 100644 index 0000000000..d8007626b6 --- /dev/null +++ b/LibTest/io/File/create_A03_t02.dart @@ -0,0 +1,54 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. +/// +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. +/// +/// Completes the future with a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `true` and to-be-created file +/// already exists, this operation completes the future with a +/// [PathExistsException]. +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) async { + File tmp = getTempFileSync(parent: sandbox); + File file = new File(tmp.path); + await file.create(exclusive: true, recursive: recursive).then((File created) { + Expect.fail("PathExistsException is expected"); + }, onError: (e) { + Expect.isTrue(e is PathExistsException); + asyncEnd(); + }); +} + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, recursive: true); + await _test(sandbox, recursive: false); +} diff --git a/LibTest/io/File/create_A03_t03.dart b/LibTest/io/File/create_A03_t03.dart new file mode 100644 index 0000000000..23863fffc3 --- /dev/null +++ b/LibTest/io/File/create_A03_t03.dart @@ -0,0 +1,63 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. +/// +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. +/// +/// Completes the future with a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `false`, existing files are left +/// untouched by create. Test [Link] pointing to a file +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) async { + File target = getTempFileSync(parent: sandbox); + target.writeAsStringSync("Target content"); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = File(link.path); + await file.create(recursive: recursive).then((File created) { + Expect.isTrue(created.existsSync()); + Expect.equals(file.path, created.path); + // Now check that all read/write operations are performed on link's target + Expect.equals("Target content", file.readAsStringSync()); + created.writeAsStringSync("Lily was here"); + Expect.equals("Lily was here", target.readAsStringSync()); + // Delete doesn't delete the target of the link but the link itself + created.deleteSync(); + Expect.isFalse(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.isFalse(link.existsSync()); + asyncEnd(); + }); +} + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, recursive: false); + await _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/create_A03_t04.dart b/LibTest/io/File/create_A03_t04.dart new file mode 100644 index 0000000000..3bdb055434 --- /dev/null +++ b/LibTest/io/File/create_A03_t04.dart @@ -0,0 +1,63 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. +/// +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. +/// +/// Completes the future with a FileSystemException if the operation fails. +/// +/// @description Checks that if `exclusive` is `false`, existing files are left +/// untouched by create. Test [Link] pointing to a not existing entity +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, {bool recursive = false}) async { + File target = File(getTempFilePath(parent: sandbox)); + Expect.isFalse(target.existsSync()); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = File(link.path); + await file.create(recursive: recursive).then((File created) { + Expect.isTrue(created.existsSync()); + Expect.equals(file.path, created.path); + // Now check that all read/write operations are performed on link's target + Expect.isTrue(target.existsSync()); + created.writeAsStringSync("Lily was here"); + Expect.equals("Lily was here", target.readAsStringSync()); + // Delete doesn't delete the target of the link but the link itself + created.deleteSync(); + Expect.isFalse(file.existsSync()); + Expect.isTrue(target.existsSync()); + Expect.isFalse(link.existsSync()); + asyncEnd(); + }); +} + +_main(Directory sandbox) async { + asyncMultiStart(2); + await _test(sandbox, recursive: false); + await _test(sandbox, recursive: true); +} diff --git a/LibTest/io/File/create_A04_t01.dart b/LibTest/io/File/create_A04_t01.dart index 1ec5b8ee56..0de2db9b77 100644 --- a/LibTest/io/File/create_A04_t01.dart +++ b/LibTest/io/File/create_A04_t01.dart @@ -2,20 +2,30 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -/// @assertion Future create({bool recursive: false}) -/// Create the file. Returns a Future that completes with the file when it -/// has been created. +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created /// /// If recursive is false, the default, the file is created only if all -/// directories in the path exist. If recursive is true, all non-existing path -/// components are created. +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. /// -/// Existing files are left untouched by create. Calling create on an existing -/// file might fail if there are restrictive permissions on the file. +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. /// /// Completes the future with a FileSystemException if the operation fails. -/// @description Checks that future is completed with a FileSystemException if -/// the operation fails +/// +/// @description Checks that future is completed with a `FileSystemException` if +/// the operation fails. Test the case when there is an existing directory with +/// the same path /// @author sgrekhov@unipro.ru import "dart:io"; @@ -26,14 +36,24 @@ main() async { await inSandbox(_main); } -_main(Directory sandbox) async { +_test(Directory sandbox, + {bool recursive = false, bool exclusive = false}) async { Directory dir = getTempDirectorySync(parent: sandbox); File file = new File(dir.path); - asyncStart(); - await file.create(recursive: false).then((File created) { - Expect.fail("FileSystemException is expected"); + await file.create(recursive: recursive, exclusive: exclusive).then( + (File created) { + Expect.fail("FileSystemException is expected." + "Recursive=$recursive, exclusive=$exclusive"); }, onError: (e) { Expect.isTrue(e is FileSystemException); asyncEnd(); }); } + +_main(Directory sandbox) async { + asyncMultiStart(4); + await _test(sandbox, recursive: false, exclusive: false); + await _test(sandbox, recursive: false, exclusive: true); + await _test(sandbox, recursive: true, exclusive: false); + await _test(sandbox, recursive: true, exclusive: true); +} diff --git a/LibTest/io/File/create_A04_t02.dart b/LibTest/io/File/create_A04_t02.dart new file mode 100644 index 0000000000..a081a80e39 --- /dev/null +++ b/LibTest/io/File/create_A04_t02.dart @@ -0,0 +1,60 @@ +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +/// @assertion Future create( +/// {bool recursive = false, +/// bool exclusive = false} +/// ) +/// Creates the file. +/// +/// Returns a Future that completes with the file when it has been created +/// +/// If recursive is false, the default, the file is created only if all +/// directories in its path already exist. If recursive is true, any +/// non-existing parent paths are created first. +/// +/// If exclusive is true and to-be-created file already exists, this operation +/// completes the future with a PathExistsException. +/// +/// If exclusive is false, existing files are left untouched by create. Calling +/// create on an existing file still might fail if there are restrictive +/// permissions on the file. +/// +/// Completes the future with a FileSystemException if the operation fails. +/// +/// @description Checks that future is completed with a `FileSystemException` if +/// the operation fails. Test the case when there is an existing link to a +/// directory on the path +/// @author sgrekhov22@gmail.com + +import "dart:io"; +import "../../../Utils/expect.dart"; +import "../file_utils.dart"; + +main() async { + await inSandbox(_main); +} + +_test(Directory sandbox, + {bool recursive = false, bool exclusive = false}) async { + Directory target = getTempDirectorySync(parent: sandbox); + Link link = getTempLinkSync(parent: sandbox, target: target.path); + File file = File(link.path); + await file.create(recursive: recursive, exclusive: exclusive).then( + (File created) { + Expect.fail("FileSystemException is expected." + "Recursive=$recursive, exclusive=$exclusive"); + }, onError: (e) { + Expect.isTrue(e is FileSystemException); + asyncEnd(); + }); +} + +_main(Directory sandbox) async { + asyncMultiStart(4); + await _test(sandbox, recursive: false, exclusive: false); + await _test(sandbox, recursive: false, exclusive: true); + await _test(sandbox, recursive: true, exclusive: false); + await _test(sandbox, recursive: true, exclusive: true); +}