Skip to content

Commit 6453251

Browse files
MaxDesiatovkateinoigakukun
authored andcommitted
Remove remaining uses of canImport(Testing)
1 parent 1f31fab commit 6453251

File tree

12 files changed

+977
-1002
lines changed

12 files changed

+977
-1002
lines changed

Tests/WASITests/IntegrationTests.swift

Lines changed: 190 additions & 193 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,40 @@
1-
#if canImport(Testing)
2-
import Testing
3-
import SystemPackage
1+
import Testing
2+
import SystemPackage
43

5-
@testable import WASI
4+
@testable import WASI
65

7-
@Suite
8-
struct OpenParentTests {
9-
@Test
10-
func testSplitParent() {
11-
func check(
12-
_ lhs: (FilePath, FilePath.Component)?,
13-
_ rhs: (FilePath, FilePath.Component)?,
14-
sourceLocation: SourceLocation = #_sourceLocation
15-
) {
16-
switch (lhs, rhs) {
17-
case (.none, .none): return
18-
case (.some(let lhs), .some(let rhs)):
19-
#expect(lhs.0 == rhs.0, sourceLocation: sourceLocation)
20-
#expect(lhs.1 == rhs.1, sourceLocation: sourceLocation)
21-
default:
22-
#expect((false), "\(String(describing: lhs)) and \(String(describing: rhs)) are not equal", sourceLocation: sourceLocation)
23-
}
6+
@Suite
7+
struct OpenParentTests {
8+
@Test
9+
func testSplitParent() {
10+
func check(
11+
_ lhs: (FilePath, FilePath.Component)?,
12+
_ rhs: (FilePath, FilePath.Component)?,
13+
sourceLocation: SourceLocation = #_sourceLocation
14+
) {
15+
switch (lhs, rhs) {
16+
case (.none, .none): return
17+
case (.some(let lhs), .some(let rhs)):
18+
#expect(lhs.0 == rhs.0, sourceLocation: sourceLocation)
19+
#expect(lhs.1 == rhs.1, sourceLocation: sourceLocation)
20+
default:
21+
#expect((false), "\(String(describing: lhs)) and \(String(describing: rhs)) are not equal", sourceLocation: sourceLocation)
2422
}
23+
}
2524

26-
check(splitParent(path: ""), nil)
25+
check(splitParent(path: ""), nil)
2726

28-
check(splitParent(path: "/"), (FilePath("/"), FilePath.Component(".")))
29-
check(splitParent(path: "/."), (FilePath("/."), FilePath.Component(".")))
30-
check(splitParent(path: "/a"), (FilePath("/"), FilePath.Component("a")))
31-
check(splitParent(path: "/a/"), (FilePath("/a"), FilePath.Component(".")))
32-
check(splitParent(path: "/a/."), (FilePath("/a/."), FilePath.Component(".")))
33-
check(splitParent(path: "/a/.."), (FilePath("/a/.."), FilePath.Component(".")))
27+
check(splitParent(path: "/"), (FilePath("/"), FilePath.Component(".")))
28+
check(splitParent(path: "/."), (FilePath("/."), FilePath.Component(".")))
29+
check(splitParent(path: "/a"), (FilePath("/"), FilePath.Component("a")))
30+
check(splitParent(path: "/a/"), (FilePath("/a"), FilePath.Component(".")))
31+
check(splitParent(path: "/a/."), (FilePath("/a/."), FilePath.Component(".")))
32+
check(splitParent(path: "/a/.."), (FilePath("/a/.."), FilePath.Component(".")))
3433

35-
check(splitParent(path: "b"), (FilePath(""), FilePath.Component("b")))
36-
check(splitParent(path: "b/."), (FilePath("b/."), FilePath.Component(".")))
37-
check(splitParent(path: "b/.."), (FilePath("b/.."), FilePath.Component(".")))
34+
check(splitParent(path: "b"), (FilePath(""), FilePath.Component("b")))
35+
check(splitParent(path: "b/."), (FilePath("b/."), FilePath.Component(".")))
36+
check(splitParent(path: "b/.."), (FilePath("b/.."), FilePath.Component(".")))
3837

39-
check(splitParent(path: "../c"), (FilePath(".."), FilePath.Component("c")))
40-
}
38+
check(splitParent(path: "../c"), (FilePath(".."), FilePath.Component("c")))
4139
}
42-
#endif
40+
}
Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1-
#if canImport(Testing)
2-
import Testing
1+
import Testing
32

4-
@testable import WASI
3+
@testable import WASI
54

6-
@Suite
7-
struct RandomBufferGeneratorTests {
8-
struct DeterministicGenerator: RandomNumberGenerator, RandomBufferGenerator {
9-
var items: [UInt64]
5+
@Suite
6+
struct RandomBufferGeneratorTests {
7+
struct DeterministicGenerator: RandomNumberGenerator, RandomBufferGenerator {
8+
var items: [UInt64]
109

11-
mutating func next() -> UInt64 {
12-
items.removeFirst()
13-
}
10+
mutating func next() -> UInt64 {
11+
items.removeFirst()
1412
}
15-
@Test
16-
func defaultFill() {
17-
var generator = DeterministicGenerator(items: [
18-
0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210, 0xdead_beef_badd_cafe,
19-
])
20-
for (bufferSize, expectedBytes): (Int, [UInt8]) in [
21-
(10, [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x10, 0x32]),
22-
(2, [0xfe, 0xca]),
23-
(0, []),
24-
] {
25-
var buffer: [UInt8] = Array(repeating: 0, count: bufferSize)
26-
buffer.withUnsafeMutableBufferPointer {
27-
generator.fill(buffer: $0)
28-
}
29-
let expected: [UInt8]
30-
#if _endian(little)
31-
expected = expectedBytes
32-
#else
33-
expected = Array(expectedBytes.reversed())
34-
#endif
35-
#expect(buffer == expected)
13+
}
14+
@Test
15+
func defaultFill() {
16+
var generator = DeterministicGenerator(items: [
17+
0x0123_4567_89ab_cdef, 0xfedc_ba98_7654_3210, 0xdead_beef_badd_cafe,
18+
])
19+
for (bufferSize, expectedBytes): (Int, [UInt8]) in [
20+
(10, [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, 0x10, 0x32]),
21+
(2, [0xfe, 0xca]),
22+
(0, []),
23+
] {
24+
var buffer: [UInt8] = Array(repeating: 0, count: bufferSize)
25+
buffer.withUnsafeMutableBufferPointer {
26+
generator.fill(buffer: $0)
3627
}
28+
let expected: [UInt8]
29+
#if _endian(little)
30+
expected = expectedBytes
31+
#else
32+
expected = Array(expectedBytes.reversed())
33+
#endif
34+
#expect(buffer == expected)
3735
}
3836
}
37+
}
3938

40-
#endif

Tests/WASITests/WASITests.swift

Lines changed: 114 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
1-
#if canImport(Testing)
2-
import Testing
3-
4-
@testable import WASI
5-
6-
@Suite
7-
struct WASITests {
8-
#if !os(Windows)
9-
@Test
10-
func pathOpen() throws {
11-
let t = try TestSupport.TemporaryDirectory()
12-
13-
try t.createDir(at: "External")
14-
try t.createDir(at: "External/secret-dir-b")
15-
try t.createFile(at: "External/secret-a.txt", contents: "Secret A")
16-
try t.createFile(at: "External/secret-dir-b/secret-c.txt", contents: "Secret C")
17-
try t.createDir(at: "Sandbox")
18-
try t.createFile(at: "Sandbox/hello.txt", contents: "Hello")
19-
try t.createSymlink(at: "Sandbox/link-hello.txt", to: "hello.txt")
20-
try t.createDir(at: "Sandbox/world.dir")
21-
try t.createSymlink(at: "Sandbox/link-world.dir", to: "world.dir")
22-
try t.createSymlink(at: "Sandbox/link-external-secret-a.txt", to: "../External/secret-a.txt")
23-
try t.createSymlink(at: "Sandbox/link-secret-dir-b", to: "../External/secret-dir-b")
24-
try t.createSymlink(at: "Sandbox/link-updown-hello.txt", to: "../Sandbox/link-updown-hello.txt")
25-
try t.createSymlink(at: "Sandbox/link-external-non-existent.txt", to: "../External/non-existent.txt")
26-
try t.createSymlink(at: "Sandbox/link-root", to: "/")
27-
try t.createSymlink(at: "Sandbox/link-loop.txt", to: "link-loop.txt")
28-
29-
let wasi = try WASIBridgeToHost(
30-
preopens: ["/Sandbox": t.url.appendingPathComponent("Sandbox").path]
1+
import Testing
2+
3+
@testable import WASI
4+
5+
@Suite
6+
struct WASITests {
7+
#if !os(Windows)
8+
@Test
9+
func pathOpen() throws {
10+
let t = try TestSupport.TemporaryDirectory()
11+
12+
try t.createDir(at: "External")
13+
try t.createDir(at: "External/secret-dir-b")
14+
try t.createFile(at: "External/secret-a.txt", contents: "Secret A")
15+
try t.createFile(at: "External/secret-dir-b/secret-c.txt", contents: "Secret C")
16+
try t.createDir(at: "Sandbox")
17+
try t.createFile(at: "Sandbox/hello.txt", contents: "Hello")
18+
try t.createSymlink(at: "Sandbox/link-hello.txt", to: "hello.txt")
19+
try t.createDir(at: "Sandbox/world.dir")
20+
try t.createSymlink(at: "Sandbox/link-world.dir", to: "world.dir")
21+
try t.createSymlink(at: "Sandbox/link-external-secret-a.txt", to: "../External/secret-a.txt")
22+
try t.createSymlink(at: "Sandbox/link-secret-dir-b", to: "../External/secret-dir-b")
23+
try t.createSymlink(at: "Sandbox/link-updown-hello.txt", to: "../Sandbox/link-updown-hello.txt")
24+
try t.createSymlink(at: "Sandbox/link-external-non-existent.txt", to: "../External/non-existent.txt")
25+
try t.createSymlink(at: "Sandbox/link-root", to: "/")
26+
try t.createSymlink(at: "Sandbox/link-loop.txt", to: "link-loop.txt")
27+
28+
let wasi = try WASIBridgeToHost(
29+
preopens: ["/Sandbox": t.url.appendingPathComponent("Sandbox").path]
30+
)
31+
let mntFd: WASIAbi.Fd = 3
32+
33+
func assertResolve(_ path: String, followSymlink: Bool, directory: Bool = false) throws {
34+
let fd = try wasi.path_open(
35+
dirFd: mntFd,
36+
dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [],
37+
path: path,
38+
oflags: directory ? [.DIRECTORY] : [],
39+
fsRightsBase: .DIRECTORY_BASE_RIGHTS,
40+
fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS,
41+
fdflags: []
3142
)
32-
let mntFd: WASIAbi.Fd = 3
43+
try wasi.fd_close(fd: fd)
44+
}
3345

34-
func assertResolve(_ path: String, followSymlink: Bool, directory: Bool = false) throws {
35-
let fd = try wasi.path_open(
46+
func assertNotResolve(
47+
_ path: String,
48+
followSymlink: Bool,
49+
directory: Bool = false,
50+
sourceLocation: SourceLocation = #_sourceLocation,
51+
_ checkError: ((WASIAbi.Errno) throws -> Void)?
52+
) throws {
53+
do {
54+
_ = try wasi.path_open(
3655
dirFd: mntFd,
3756
dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [],
3857
path: path,
@@ -41,99 +60,78 @@
4160
fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS,
4261
fdflags: []
4362
)
44-
try wasi.fd_close(fd: fd)
45-
}
46-
47-
func assertNotResolve(
48-
_ path: String,
49-
followSymlink: Bool,
50-
directory: Bool = false,
51-
sourceLocation: SourceLocation = #_sourceLocation,
52-
_ checkError: ((WASIAbi.Errno) throws -> Void)?
53-
) throws {
54-
do {
55-
_ = try wasi.path_open(
56-
dirFd: mntFd,
57-
dirFlags: followSymlink ? [.SYMLINK_FOLLOW] : [],
58-
path: path,
59-
oflags: directory ? [.DIRECTORY] : [],
60-
fsRightsBase: .DIRECTORY_BASE_RIGHTS,
61-
fsRightsInheriting: .DIRECTORY_INHERITING_RIGHTS,
62-
fdflags: []
63-
)
64-
#expect((false), "Expected not to be able to open \(path)", sourceLocation: sourceLocation)
65-
} catch {
66-
guard let error = error as? WASIAbi.Errno else {
67-
#expect((false), "Expected WASIAbi.Errno error but got \(error)", sourceLocation: sourceLocation)
68-
return
69-
}
70-
try checkError?(error)
63+
#expect((false), "Expected not to be able to open \(path)", sourceLocation: sourceLocation)
64+
} catch {
65+
guard let error = error as? WASIAbi.Errno else {
66+
#expect((false), "Expected WASIAbi.Errno error but got \(error)", sourceLocation: sourceLocation)
67+
return
7168
}
69+
try checkError?(error)
7270
}
71+
}
7372

74-
try assertNotResolve("non-existent.txt", followSymlink: false) { error in
75-
#expect(error == .ENOENT)
76-
}
73+
try assertNotResolve("non-existent.txt", followSymlink: false) { error in
74+
#expect(error == .ENOENT)
75+
}
7776

78-
try assertResolve("link-hello.txt", followSymlink: true)
79-
try assertNotResolve("link-hello.txt", followSymlink: false) { error in
80-
#expect(error == .ELOOP)
81-
}
82-
try assertNotResolve("link-hello.txt", followSymlink: true, directory: true) { error in
83-
#expect(error == .ENOTDIR)
84-
}
77+
try assertResolve("link-hello.txt", followSymlink: true)
78+
try assertNotResolve("link-hello.txt", followSymlink: false) { error in
79+
#expect(error == .ELOOP)
80+
}
81+
try assertNotResolve("link-hello.txt", followSymlink: true, directory: true) { error in
82+
#expect(error == .ENOTDIR)
83+
}
8584

86-
try assertNotResolve("link-hello.txt/", followSymlink: true) { error in
87-
#expect(error == .ENOTDIR)
88-
}
85+
try assertNotResolve("link-hello.txt/", followSymlink: true) { error in
86+
#expect(error == .ENOTDIR)
87+
}
8988

90-
try assertResolve("link-world.dir", followSymlink: true)
91-
try assertNotResolve("link-world.dir", followSymlink: false) { error in
92-
#expect(error == .ELOOP)
93-
}
89+
try assertResolve("link-world.dir", followSymlink: true)
90+
try assertNotResolve("link-world.dir", followSymlink: false) { error in
91+
#expect(error == .ELOOP)
92+
}
9493

95-
try assertNotResolve("link-external-secret-a.txt", followSymlink: true) { error in
96-
#expect(error == .EPERM)
97-
}
98-
try assertNotResolve("link-external-secret-a.txt", followSymlink: false) { error in
99-
#expect(error == .ELOOP)
100-
}
94+
try assertNotResolve("link-external-secret-a.txt", followSymlink: true) { error in
95+
#expect(error == .EPERM)
96+
}
97+
try assertNotResolve("link-external-secret-a.txt", followSymlink: false) { error in
98+
#expect(error == .ELOOP)
99+
}
101100

102-
try assertNotResolve("link-external-non-existent.txt", followSymlink: true) { error in
103-
#expect(error == .EPERM)
104-
}
105-
try assertNotResolve("link-external-non-existent.txt", followSymlink: false) { error in
106-
#expect(error == .ELOOP)
107-
}
101+
try assertNotResolve("link-external-non-existent.txt", followSymlink: true) { error in
102+
#expect(error == .EPERM)
103+
}
104+
try assertNotResolve("link-external-non-existent.txt", followSymlink: false) { error in
105+
#expect(error == .ELOOP)
106+
}
108107

109-
try assertNotResolve("link-updown-hello.txt", followSymlink: true) { error in
110-
#expect(error == .EPERM)
111-
}
112-
try assertNotResolve("link-updown-hello.txt", followSymlink: false) { error in
113-
#expect(error == .ELOOP)
114-
}
108+
try assertNotResolve("link-updown-hello.txt", followSymlink: true) { error in
109+
#expect(error == .EPERM)
110+
}
111+
try assertNotResolve("link-updown-hello.txt", followSymlink: false) { error in
112+
#expect(error == .ELOOP)
113+
}
115114

116-
try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: true) { error in
117-
#expect(error == .EPERM)
118-
}
119-
try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: false) { error in
120-
#expect(error == .ENOTDIR)
121-
}
115+
try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: true) { error in
116+
#expect(error == .EPERM)
117+
}
118+
try assertNotResolve("link-secret-dir-b/secret-c.txt", followSymlink: false) { error in
119+
#expect(error == .ENOTDIR)
120+
}
122121

123-
try assertNotResolve("link-root", followSymlink: true) { error in
124-
#expect(error == .EPERM)
125-
}
126-
try assertNotResolve("link-root", followSymlink: false) { error in
127-
#expect(error == .ELOOP)
128-
}
122+
try assertNotResolve("link-root", followSymlink: true) { error in
123+
#expect(error == .EPERM)
124+
}
125+
try assertNotResolve("link-root", followSymlink: false) { error in
126+
#expect(error == .ELOOP)
127+
}
129128

130-
try assertNotResolve("link-loop.txt", followSymlink: false) { error in
131-
#expect(error == .ELOOP)
132-
}
133-
try assertNotResolve("link-loop.txt", followSymlink: true) { error in
134-
#expect(error == .ELOOP)
135-
}
129+
try assertNotResolve("link-loop.txt", followSymlink: false) { error in
130+
#expect(error == .ELOOP)
131+
}
132+
try assertNotResolve("link-loop.txt", followSymlink: true) { error in
133+
#expect(error == .ELOOP)
136134
}
137-
#endif
138-
}
139-
#endif
135+
}
136+
#endif
137+
}

0 commit comments

Comments
 (0)