Skip to content

Commit

Permalink
dart-lang#2291. Add more Link.createSync() tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrekhov committed Oct 18, 2023
1 parent 2ae23ce commit b1e9aef
Show file tree
Hide file tree
Showing 10 changed files with 561 additions and 55 deletions.
44 changes: 21 additions & 23 deletions LibTest/io/Link/createSync_A05_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,33 @@
// BSD-style license that can be found in the LICENSE file.

/// @assertion void createSync(
/// String target, {
/// bool recursive: false
/// })
/// String target,
/// {bool recursive = false}
/// )
/// Synchronously create the link. Calling createSync on an existing link will
/// throw an exception.
///
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing path
/// components are created. The directories in the path of target are not
/// affected, unless they are also in path.
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing parent
/// paths are created first. The directories in the path of target are not
/// affected, unless they are also in path.
///
/// On the Windows platform, this call will create a true symbolic link instead
/// of a Junction. In order to create a symbolic link on Windows, Dart must be
/// run in Administrator mode or the system must have Developer Mode enabled,
/// otherwise a FileSystemException will be raised with ERROR_PRIVILEGE_NOT_HELD
/// set as the errno when this call is made.
/// On the Windows platform, this call will create a true symbolic link instead
/// of a junction. The link represents a file or directory and does not change
/// its type after creation. If [target] exists then the type of the link will
/// match the type [target], otherwise a file symlink is created.
///
/// On other platforms, the posix symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
/// @description Checks that on the Windows platform relative paths to the target
/// is not converted to absolute paths (as it was in the past)
///
/// @note The test should run with the Administrator priveleges on Windows.
/// Dart API Spec reads:
/// In order to create a symbolic link on Windows, Dart must be run in
/// Administrator mode or the system must have Developer Mode enabled, otherwise
/// a FileSystemException will be raised with ERROR_PRIVILEGE_NOT_HELD set as
/// the errno when this call is made.
/// Administrator mode or the system must have Developer Mode enabled,
/// otherwise a [FileSystemException] will be raised with
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
///
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
///
/// @description Checks that on the Windows platform relative paths to the
/// target is not converted to absolute paths (as it was in the past)
///
/// @author sgrekhov@unipro.ru
Expand Down
78 changes: 48 additions & 30 deletions LibTest/io/Link/createSync_A06_t01.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,66 @@
// BSD-style license that can be found in the LICENSE file.

/// @assertion void createSync(
/// String target, {
/// bool recursive: false
/// })
/// String target,
/// {bool recursive = false}
/// )
/// Synchronously create the link. Calling createSync on an existing link will
/// throw an exception.
///
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing path
/// components are created. The directories in the path of target are not
/// affected, unless they are also in path.
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing parent
/// paths are created first. The directories in the path of target are not
/// affected, unless they are also in path.
///
/// On the Windows platform, this will only work with directories, and the
/// target directory must exist. The link will be created as a Junction. Only
/// absolute links will be created, and relative paths to the target will be
/// converted to absolute paths.
/// On the Windows platform, this call will create a true symbolic link instead
/// of a junction. The link represents a file or directory and does not change
/// its type after creation. If [target] exists then the type of the link will
/// match the type [target], otherwise a file symlink is created.
///
/// On other platforms, the posix symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
/// @description Checks that on non-Windows platforms only absolute links will be
/// created, and relative paths to the target will be interpreted relative to the
/// directory containing the link.
/// In order to create a symbolic link on Windows, Dart must be run in
/// Administrator mode or the system must have Developer Mode enabled,
/// otherwise a [FileSystemException] will be raised with
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
///
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
///
/// @description Checks that relative paths to the target will be interpreted
/// relative to the directory containing the link. Test relative path to
/// [Directory]
/// @author sgrekhov@unipro.ru
/// @issue 53689
import "dart:io";
import "../../../Utils/expect.dart";
import "../file_utils.dart";

main() async {
await inSandbox(_main);
main() {
inSandbox(_main);
}

_main(Directory sandbox) async {
if (!Platform.isWindows) {
Directory dir = getTempDirectorySync(parent: sandbox);
String dirName = getTempDirectoryName();
Directory target = new Directory(dir.path +
Platform.pathSeparator + dirName);
target.createSync();
Link link = new Link(dir.path + Platform.pathSeparator +
getTempFileName(extension: "lnk"));
link.createSync(dirName);
Expect.equals(dirName, link.targetSync());
_main(Directory sandbox) {
String dirName = getTempDirectoryName();
Directory target = Directory(sandbox.path + Platform.pathSeparator + dirName);
target.createSync();
Link link = Link(sandbox.path +
Platform.pathSeparator +
getTempFileName(extension: "lnk"));
link.createSync(dirName);
Expect.equals(dirName, link.targetSync());
Expect.equals(
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
// Now create a directory and move the link into it. Its relative target
// should point to a not existing entity after it
Directory dir = getTempDirectorySync(parent: sandbox);
Link moved = link.renameSync(dir.path + Platform.pathSeparator + "moved.lnk");
Expect.equals(dirName, moved.targetSync());
if (Platform.isWindows) {
Expect.equals(
FileSystemEntityType.link, FileSystemEntity.typeSync(moved.path));
} else {
Expect.equals(
FileSystemEntityType.notFound, FileSystemEntity.typeSync(moved.path));
}
}
66 changes: 66 additions & 0 deletions LibTest/io/Link/createSync_A06_t02.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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(
/// String target,
/// {bool recursive = false}
/// )
/// Synchronously create the link. Calling createSync on an existing link will
/// throw an exception.
///
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing parent
/// paths are created first. The directories in the path of target are not
/// affected, unless they are also in path.
///
/// On the Windows platform, this call will create a true symbolic link instead
/// of a junction. The link represents a file or directory and does not change
/// its type after creation. If [target] exists then the type of the link will
/// match the type [target], otherwise a file symlink is created.
///
/// In order to create a symbolic link on Windows, Dart must be run in
/// Administrator mode or the system must have Developer Mode enabled,
/// otherwise a [FileSystemException] will be raised with
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
///
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
///
/// @description Checks that relative paths to the target will be interpreted
/// relative to the directory containing the link. Test relative path to [File]
/// @author sgrekhov22@gmail.com
import "dart:io";
import "../../../Utils/expect.dart";
import "../file_utils.dart";

main() {
inSandbox(_main);
}

_main(Directory sandbox) {
String fileName = getTempFileName();
File target = new File(sandbox.path + Platform.pathSeparator + fileName);
target.createSync();
Link link = new Link(sandbox.path +
Platform.pathSeparator +
getTempFileName(extension: "lnk"));
link.createSync(fileName);
Expect.equals(fileName, link.targetSync());
Expect.equals(
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
// Now create a directory and move the link into it. Its relative target
// should point to a not existing entity after it
Directory dir = getTempDirectorySync(parent: sandbox);
Link moved = link.renameSync(dir.path + Platform.pathSeparator + "moved.lnk");
Expect.equals(fileName, moved.targetSync());
if (Platform.isWindows) {
Expect.equals(
FileSystemEntityType.link, FileSystemEntity.typeSync(moved.path));
} else {
Expect.equals(
FileSystemEntityType.notFound, FileSystemEntity.typeSync(moved.path));
}
}
68 changes: 68 additions & 0 deletions LibTest/io/Link/createSync_A06_t03.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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(
/// String target,
/// {bool recursive = false}
/// )
/// Synchronously create the link. Calling createSync on an existing link will
/// throw an exception.
///
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing parent
/// paths are created first. The directories in the path of target are not
/// affected, unless they are also in path.
///
/// On the Windows platform, this call will create a true symbolic link instead
/// of a junction. The link represents a file or directory and does not change
/// its type after creation. If [target] exists then the type of the link will
/// match the type [target], otherwise a file symlink is created.
///
/// In order to create a symbolic link on Windows, Dart must be run in
/// Administrator mode or the system must have Developer Mode enabled,
/// otherwise a [FileSystemException] will be raised with
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
///
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
///
/// @description Checks that relative paths to the target will be interpreted
/// relative to the directory containing the link. Test relative path to [Link]
/// pointing to [Directory]
/// @author sgrekhov22@gmail.com
/// @issue 53689
import "dart:io";
import "../../../Utils/expect.dart";
import "../file_utils.dart";

main() {
inSandbox(_main);
}

_main(Directory sandbox) {
String linkName = getTempFileName();
Link target = new Link(sandbox.path + Platform.pathSeparator + linkName);
target.createSync(sandbox.path);
Link link = new Link(sandbox.path +
Platform.pathSeparator +
getTempFileName(extension: "lnk"));
link.createSync(linkName);
Expect.equals(linkName, link.targetSync());
Expect.equals(
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
// Now create a directory and move the link into it. Its relative target
// should point to a not existing entity after it
Directory dir = getTempDirectorySync(parent: sandbox);
Link moved = link.renameSync(dir.path + Platform.pathSeparator + "moved.lnk");
Expect.equals(linkName, moved.targetSync());
if (Platform.isWindows) {
Expect.equals(
FileSystemEntityType.link, FileSystemEntity.typeSync(moved.path));
} else {
Expect.equals(
FileSystemEntityType.notFound, FileSystemEntity.typeSync(moved.path));
}
}
68 changes: 68 additions & 0 deletions LibTest/io/Link/createSync_A06_t04.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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(
/// String target,
/// {bool recursive = false}
/// )
/// Synchronously create the link. Calling createSync on an existing link will
/// throw an exception.
///
/// If recursive is false, the default, the link is created only if all
/// directories in its path exist. If recursive is true, all non-existing parent
/// paths are created first. The directories in the path of target are not
/// affected, unless they are also in path.
///
/// On the Windows platform, this call will create a true symbolic link instead
/// of a junction. The link represents a file or directory and does not change
/// its type after creation. If [target] exists then the type of the link will
/// match the type [target], otherwise a file symlink is created.
///
/// In order to create a symbolic link on Windows, Dart must be run in
/// Administrator mode or the system must have Developer Mode enabled,
/// otherwise a [FileSystemException] will be raised with
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
///
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
/// containing the string target. If target is a relative path, it will be
/// interpreted relative to the directory containing the link.
///
/// @description Checks that relative paths to the target will be interpreted
/// relative to the directory containing the link. Test relative path to [Link]
/// pointing to [File]
/// @author sgrekhov22@gmail.com
import "dart:io";
import "../../../Utils/expect.dart";
import "../file_utils.dart";

main() {
inSandbox(_main);
}

_main(Directory sandbox) {
String linkName = getTempFileName();
File file = getTempFileSync(parent: sandbox);
Link target = Link(sandbox.path + Platform.pathSeparator + linkName);
target.createSync(file.path);
Link link = Link(sandbox.path +
Platform.pathSeparator +
getTempFileName(extension: "lnk"));
link.createSync(linkName);
Expect.equals(linkName, link.targetSync());
Expect.equals(
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
// Now create a directory and move the link into it. Its relative target
// should point to a not existing entity after it
Directory dir = getTempDirectorySync(parent: sandbox);
Link moved = link.renameSync(dir.path + Platform.pathSeparator + "moved.lnk");
Expect.equals(linkName, moved.targetSync());
if (Platform.isWindows) {
Expect.equals(
FileSystemEntityType.link, FileSystemEntity.typeSync(moved.path));
} else {
Expect.equals(
FileSystemEntityType.notFound, FileSystemEntity.typeSync(moved.path));
}
}

0 comments on commit b1e9aef

Please sign in to comment.