Skip to content

Commit 9f2a73e

Browse files
committed
Fixup rebase
1 parent dc936c4 commit 9f2a73e

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

Sources/_InternalTestSupport/SwiftTesting+Helpers.swift

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,38 @@ import Testing
2020
/// - sourceLocation: The source location where the expectation is made.
2121
public func expectFileExists(
2222
at path: AbsolutePath,
23+
_ comment: Comment? = nil,
2324
sourceLocation: SourceLocation = #_sourceLocation,
2425
) {
2526
#expect(
2627
localFileSystem.exists(path),
27-
"Files '\(path)' does not exist.",
28+
wrapMessage(
29+
"Files '\(path)' does not exist.",
30+
comment: comment,
31+
directoryPath: path.parentDirectory
32+
),
2833
sourceLocation: sourceLocation,
2934
)
3035
}
3136

3237
/// Verifies that a file does not exist at the specified path.
3338
///
3439
/// - Parameters:
35-
/// - fixturePath: The absolute path to check for file non-existence.
40+
/// - path: The absolute path to check for file non-existence.
3641
/// - comment: An optional comment to include in the failure message.
3742
/// - sourceLocation: The source location where the expectation is made.
3843
public func expectFileDoesNotExists(
3944
at path: AbsolutePath,
4045
_ comment: Comment? = nil,
4146
sourceLocation: SourceLocation = #_sourceLocation,
4247
) {
43-
let commentPrefix =
44-
if let comment {
45-
"\(comment): "
46-
} else {
47-
""
48-
}
49-
let msgSuffix: String
50-
do {
51-
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path.parentDirectory))"
52-
} catch {
53-
msgSuffix = ""
54-
}
5548
#expect(
5649
!localFileSystem.exists(path),
57-
"\(commentPrefix)File: '\(path)' was not expected to exist, but does.\(msgSuffix))",
50+
wrapMessage(
51+
"File: '\(path)' was not expected to exist, but does.",
52+
comment: comment,
53+
directoryPath: path.parentDirectory
54+
),
5855
sourceLocation: sourceLocation,
5956
)
6057
}
@@ -70,15 +67,9 @@ public func expectFileIsExecutable(
7067
_ comment: Comment? = nil,
7168
sourceLocation: SourceLocation = #_sourceLocation,
7269
) {
73-
let commentPrefix =
74-
if let comment {
75-
"\(comment): "
76-
} else {
77-
""
78-
}
7970
#expect(
8071
localFileSystem.isExecutableFile(fixturePath),
81-
"\(commentPrefix)File '\(fixturePath)' expected to be executable, but is not.",
72+
wrapMessage("File '\(fixturePath)' expected to be executable, but is not.", comment: comment),
8273
sourceLocation: sourceLocation,
8374
)
8475
}
@@ -90,17 +81,12 @@ public func expectFileIsExecutable(
9081
/// - sourceLocation: The source location where the expectation is made.
9182
public func expectDirectoryExists(
9283
at path: AbsolutePath,
84+
_ comment: Comment? = nil,
9385
sourceLocation: SourceLocation = #_sourceLocation,
9486
) {
95-
let msgSuffix: String
96-
do {
97-
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path))"
98-
} catch {
99-
msgSuffix = ""
100-
}
10187
#expect(
10288
localFileSystem.isDirectory(path),
103-
"Expected directory doesn't exist: '\(path)'. \(msgSuffix)",
89+
wrapMessage("Expected directory doesn't exist: '\(path)'", comment: comment, directoryPath: path),
10490
sourceLocation: sourceLocation,
10591
)
10692
}
@@ -112,21 +98,47 @@ let msgSuffix: String
11298
/// - sourceLocation: The source location where the expectation is made.
11399
public func expectDirectoryDoesNotExist(
114100
at path: AbsolutePath,
101+
_ comment: Comment? = nil,
115102
sourceLocation: SourceLocation = #_sourceLocation,
116103
) {
117-
let msgSuffix: String
118-
do {
119-
msgSuffix = try "Directory contents: \(localFileSystem.getDirectoryContents(path))"
120-
} catch {
121-
msgSuffix = ""
122-
}
123104
#expect(
124105
!localFileSystem.isDirectory(path),
125-
"Directory exists unexpectedly: '\(path)'.\(msgSuffix)",
106+
wrapMessage("Directory exists unexpectedly: '\(path)'", comment: comment, directoryPath: path),
126107
sourceLocation: sourceLocation,
127108
)
128109
}
129110

111+
/// Wraps a message with an optional comment prefix and directory contents suffix.
112+
///
113+
/// - Parameters:
114+
/// - message: The base message to wrap.
115+
/// - comment: An optional comment to prefix the message with.
116+
/// - directoryPath: An optional path to a folder whose contents will be appended to the message.
117+
/// - Returns: The formatted message with prefix and suffix.
118+
private func wrapMessage(
119+
_ message: Comment,
120+
comment: Comment? = nil,
121+
directoryPath: AbsolutePath? = nil
122+
) -> Comment {
123+
let commentPrefix =
124+
if let comment {
125+
"\(comment): "
126+
} else {
127+
""
128+
}
129+
130+
var msgSuffix = ""
131+
if let directoryPath {
132+
do {
133+
msgSuffix = try " Directory contents: \(localFileSystem.getDirectoryContents(directoryPath))"
134+
} catch {
135+
// Silently ignore errors when getting directory contents
136+
}
137+
}
138+
139+
return "\(commentPrefix)\(message)\(msgSuffix)"
140+
}
141+
130142
/// Expects that the expression throws a CommandExecutionError and passes it to the provided throwing error handler.
131143
/// - Parameters:
132144
/// - expression: The expression expected to throw

0 commit comments

Comments
 (0)