Skip to content

Commit 52b38e0

Browse files
authored
#2291. Add more Link.createSync() tests. Part 2 (#2316)
Add more `Link.createSync()` tests. Part 2
1 parent 24aa67e commit 52b38e0

24 files changed

+1421
-55
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2023, 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+
/// @assertion void createSync(
6+
/// String target,
7+
/// {bool recursive = false}
8+
/// )
9+
/// Synchronously create the link. Calling createSync on an existing link will
10+
/// throw an exception.
11+
///
12+
/// If recursive is false, the default, the link is created only if all
13+
/// directories in its path exist. If recursive is true, all non-existing parent
14+
/// paths are created first. The directories in the path of target are not
15+
/// affected, unless they are also in path.
16+
///
17+
/// On the Windows platform, this call will create a true symbolic link instead
18+
/// of a junction. The link represents a file or directory and does not change
19+
/// its type after creation. If [target] exists then the type of the link will
20+
/// match the type [target], otherwise a file symlink is created.
21+
///
22+
/// In order to create a symbolic link on Windows, Dart must be run in
23+
/// Administrator mode or the system must have Developer Mode enabled,
24+
/// otherwise a [FileSystemException] will be raised with
25+
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
26+
///
27+
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
28+
/// containing the string target. If target is a relative path, it will be
29+
/// interpreted relative to the directory containing the link.
30+
///
31+
/// @description Checks that if `target` exists then the type of the link will
32+
/// match the type `target`. Test [Link] pointing to a not existing entity as a
33+
/// target (expect `file` on Windows and `notFound` on other platforms).
34+
/// @author [email protected]
35+
36+
import "dart:io";
37+
import "../../../Utils/expect.dart";
38+
import "../file_utils.dart";
39+
40+
main() {
41+
inSandbox(_main);
42+
}
43+
44+
_main(Directory sandbox) {
45+
Link target = getTempLinkSync(
46+
parent: sandbox, target: getTempFilePath(parent: sandbox));
47+
Link link = Link(getTempFilePath(parent: sandbox));
48+
link.createSync(target.path);
49+
if (Platform.isWindows) {
50+
Expect.equals(
51+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
52+
File file = File(link.targetSync());
53+
file.createSync();
54+
Expect.equals(
55+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
56+
} else {
57+
Expect.equals(
58+
FileSystemEntityType.notFound, FileSystemEntity.typeSync(link.path));
59+
}
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2023, 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+
/// @assertion void createSync(
6+
/// String target,
7+
/// {bool recursive = false}
8+
/// )
9+
/// Synchronously create the link. Calling createSync on an existing link will
10+
/// throw an exception.
11+
///
12+
/// If recursive is false, the default, the link is created only if all
13+
/// directories in its path exist. If recursive is true, all non-existing parent
14+
/// paths are created first. The directories in the path of target are not
15+
/// affected, unless they are also in path.
16+
///
17+
/// On the Windows platform, this call will create a true symbolic link instead
18+
/// of a junction. The link represents a file or directory and does not change
19+
/// its type after creation. If [target] exists then the type of the link will
20+
/// match the type [target], otherwise a file symlink is created.
21+
///
22+
/// In order to create a symbolic link on Windows, Dart must be run in
23+
/// Administrator mode or the system must have Developer Mode enabled,
24+
/// otherwise a [FileSystemException] will be raised with
25+
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
26+
///
27+
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
28+
/// containing the string target. If target is a relative path, it will be
29+
/// interpreted relative to the directory containing the link.
30+
///
31+
/// @description Checks that if `target` does not exist then the type of the link
32+
/// will be `file` on Windows and `notFound` on other platforms.
33+
/// @author [email protected]
34+
35+
import "dart:io";
36+
import "../../../Utils/expect.dart";
37+
import "../file_utils.dart";
38+
39+
main() {
40+
inSandbox(_main);
41+
}
42+
43+
_main(Directory sandbox) {
44+
Link link = Link(getTempFilePath(parent: sandbox));
45+
link.createSync(getTempFilePath(parent: sandbox));
46+
if (Platform.isWindows) {
47+
Expect.equals(
48+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
49+
File file = File(link.targetSync());
50+
file.createSync();
51+
Expect.equals(
52+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
53+
} else {
54+
Expect.equals(
55+
FileSystemEntityType.notFound, FileSystemEntity.typeSync(link.path));
56+
}
57+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2023, 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+
/// @assertion void createSync(
6+
/// String target,
7+
/// {bool recursive = false}
8+
/// )
9+
/// Synchronously create the link. Calling createSync on an existing link will
10+
/// throw an exception.
11+
///
12+
/// If recursive is false, the default, the link is created only if all
13+
/// directories in its path exist. If recursive is true, all non-existing parent
14+
/// paths are created first. The directories in the path of target are not
15+
/// affected, unless they are also in path.
16+
///
17+
/// On the Windows platform, this call will create a true symbolic link instead
18+
/// of a junction. The link represents a file or directory and does not change
19+
/// its type after creation. If [target] exists then the type of the link will
20+
/// match the type [target], otherwise a file symlink is created.
21+
///
22+
/// In order to create a symbolic link on Windows, Dart must be run in
23+
/// Administrator mode or the system must have Developer Mode enabled,
24+
/// otherwise a [FileSystemException] will be raised with
25+
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
26+
///
27+
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
28+
/// containing the string target. If target is a relative path, it will be
29+
/// interpreted relative to the directory containing the link.
30+
///
31+
/// @description Checks that if a link with the target directory was created and
32+
/// then this directory was deleted and created again, then the link stays valid
33+
/// @author [email protected]
34+
35+
import "dart:io";
36+
import "../../../Utils/expect.dart";
37+
import "../file_utils.dart";
38+
39+
main() {
40+
inSandbox(_main);
41+
}
42+
43+
_main(Directory sandbox) {
44+
Directory target1 = getTempDirectorySync(parent: sandbox);
45+
Link link = Link(getTempFilePath(parent: sandbox));
46+
link.createSync(target1.path);
47+
Expect.equals(
48+
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
49+
target1.deleteSync();
50+
if (Platform.isWindows) {
51+
Expect.equals(
52+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
53+
} else {
54+
Expect.equals(
55+
FileSystemEntityType.notFound, FileSystemEntity.typeSync(link.path));
56+
}
57+
Directory target2 = Directory(target1.path);
58+
target2.createSync();
59+
Expect.equals(
60+
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
61+
target2.deleteSync();
62+
63+
Link target3 = Link(target1.path);
64+
Directory linkTarget = getTempDirectorySync();
65+
target3.createSync(linkTarget.path);
66+
Expect.equals(
67+
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
68+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2023, 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+
/// @assertion void createSync(
6+
/// String target,
7+
/// {bool recursive = false}
8+
/// )
9+
/// Synchronously create the link. Calling createSync on an existing link will
10+
/// throw an exception.
11+
///
12+
/// If recursive is false, the default, the link is created only if all
13+
/// directories in its path exist. If recursive is true, all non-existing parent
14+
/// paths are created first. The directories in the path of target are not
15+
/// affected, unless they are also in path.
16+
///
17+
/// On the Windows platform, this call will create a true symbolic link instead
18+
/// of a junction. The link represents a file or directory and does not change
19+
/// its type after creation. If [target] exists then the type of the link will
20+
/// match the type [target], otherwise a file symlink is created.
21+
///
22+
/// In order to create a symbolic link on Windows, Dart must be run in
23+
/// Administrator mode or the system must have Developer Mode enabled,
24+
/// otherwise a [FileSystemException] will be raised with
25+
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
26+
///
27+
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
28+
/// containing the string target. If target is a relative path, it will be
29+
/// interpreted relative to the directory containing the link.
30+
///
31+
/// @description Checks that if a link with the target directory was created and
32+
/// then this directory was deleted and a file with the same name created, then
33+
/// on Windows the link became invalid on other platforms it changes its type
34+
/// @author [email protected]
35+
36+
import "dart:io";
37+
import "../../../Utils/expect.dart";
38+
import "../file_utils.dart";
39+
40+
main() {
41+
inSandbox(_main);
42+
}
43+
44+
_main(Directory sandbox) {
45+
Directory target1 = getTempDirectorySync(parent: sandbox);
46+
Link link = Link(getTempFilePath(parent: sandbox));
47+
link.createSync(target1.path);
48+
Expect.equals(
49+
FileSystemEntityType.directory, FileSystemEntity.typeSync(link.path));
50+
target1.deleteSync();
51+
if (Platform.isWindows) {
52+
Expect.equals(
53+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
54+
} else {
55+
Expect.equals(
56+
FileSystemEntityType.notFound, FileSystemEntity.typeSync(link.path));
57+
}
58+
File target2 = File(target1.path);
59+
target2.createSync();
60+
if (Platform.isWindows) {
61+
Expect.equals(
62+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
63+
} else {
64+
Expect.equals(
65+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
66+
}
67+
target2.deleteSync();
68+
69+
Link target3 = Link(target1.path);
70+
File linkTarget = getTempFileSync();
71+
target3.createSync(linkTarget.path);
72+
if (Platform.isWindows) {
73+
Expect.equals(
74+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
75+
} else {
76+
Expect.equals(
77+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
78+
}
79+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) 2023, 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+
/// @assertion void createSync(
6+
/// String target,
7+
/// {bool recursive = false}
8+
/// )
9+
/// Synchronously create the link. Calling createSync on an existing link will
10+
/// throw an exception.
11+
///
12+
/// If recursive is false, the default, the link is created only if all
13+
/// directories in its path exist. If recursive is true, all non-existing parent
14+
/// paths are created first. The directories in the path of target are not
15+
/// affected, unless they are also in path.
16+
///
17+
/// On the Windows platform, this call will create a true symbolic link instead
18+
/// of a junction. The link represents a file or directory and does not change
19+
/// its type after creation. If [target] exists then the type of the link will
20+
/// match the type [target], otherwise a file symlink is created.
21+
///
22+
/// In order to create a symbolic link on Windows, Dart must be run in
23+
/// Administrator mode or the system must have Developer Mode enabled,
24+
/// otherwise a [FileSystemException] will be raised with
25+
/// `ERROR_PRIVILEGE_NOT_HELD` set as the errno when this call is made.
26+
///
27+
/// On other platforms, the POSIX symlink() call is used to make a symbolic link
28+
/// containing the string target. If target is a relative path, it will be
29+
/// interpreted relative to the directory containing the link.
30+
///
31+
/// @description Checks that if a link with the target file was created and
32+
/// then this file was deleted and created again, then the link stays valid
33+
/// @author [email protected]
34+
35+
import "dart:io";
36+
import "../../../Utils/expect.dart";
37+
import "../file_utils.dart";
38+
39+
main() {
40+
inSandbox(_main);
41+
}
42+
43+
_main(Directory sandbox) {
44+
File target1 = getTempFileSync(parent: sandbox);
45+
Link link = Link(getTempFilePath(parent: sandbox));
46+
link.createSync(target1.path);
47+
Expect.equals(
48+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
49+
target1.deleteSync();
50+
if (Platform.isWindows) {
51+
Expect.equals(
52+
FileSystemEntityType.link, FileSystemEntity.typeSync(link.path));
53+
} else {
54+
Expect.equals(
55+
FileSystemEntityType.notFound, FileSystemEntity.typeSync(link.path));
56+
}
57+
File target2 = File(target1.path);
58+
target2.createSync();
59+
Expect.equals(
60+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
61+
target2.deleteSync();
62+
63+
Link target3 = Link(target1.path);
64+
File linkTarget = getTempFileSync();
65+
target3.createSync(linkTarget.path);
66+
Expect.equals(
67+
FileSystemEntityType.file, FileSystemEntity.typeSync(link.path));
68+
}

0 commit comments

Comments
 (0)