Skip to content

Commit a758053

Browse files
authored
Merge pull request #2279 from brentdax/pipe-dreams
Prevent pipe-based FileHandle tests from hanging
2 parents b520d55 + 1d63aae commit a758053

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

TestFoundation/TestFileHandle.swift

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,24 @@ class TestFileHandle : XCTestCase {
6161
emperor of Blefuscu, and sue for peace
6262
""".data(using: .utf8)!
6363
}()
64-
65-
func createFileHandle() -> FileHandle {
64+
65+
func createTemporaryFile(containing data: Data = Data()) -> URL {
6666
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(ProcessInfo.processInfo.globallyUniqueString)
67-
68-
expectDoesNotThrow({ try content.write(to: url) }, "Couldn't write file at \(url.path) for testing")
69-
67+
68+
allTemporaryFileURLs.append(url)
69+
70+
expectDoesNotThrow({ try data.write(to: url) }, "Couldn't create file at \(url.path) for testing")
71+
72+
return url
73+
}
74+
75+
func createFileHandle() -> FileHandle {
76+
let url = createTemporaryFile(containing: content)
77+
7078
var fh: FileHandle?
7179
expectDoesNotThrow({ fh = try FileHandle(forReadingFrom: url) }, "Couldn't create file handle.")
72-
80+
7381
allHandles.append(fh!)
74-
allTemporaryFileURLs.append(url)
7582
return fh!
7683
}
7784

@@ -227,22 +234,22 @@ class TestFileHandle : XCTestCase {
227234
}
228235
#endif
229236

230-
func createPipe() -> Pipe {
231-
let pipe = Pipe()
232-
allHandles.append(pipe.fileHandleForWriting)
233-
allHandles.append(pipe.fileHandleForReading)
234-
return pipe
235-
}
236-
237237
func performWriteTest<T: DataProtocol>(with data: T, expecting expectation: Data? = nil) {
238-
let pipe = createPipe()
239-
let writer = pipe.fileHandleForWriting
240-
let reader = pipe.fileHandleForReading
241-
242-
expectDoesNotThrow({ try writer.write(contentsOf: data) }, "Writing must succeed")
243-
expectDoesNotThrow({
244-
expectEqual(try reader.read(upToCount: data.count), expectation ?? content, "The content must be the same")
245-
}, "Reading must succeed")
238+
let url = createTemporaryFile()
239+
240+
var maybeFH: FileHandle?
241+
expectDoesNotThrow({ maybeFH = try FileHandle(forWritingTo: url) }, "Opening write handle must succeed")
242+
guard let fh = maybeFH else { return }
243+
allHandles.append(fh)
244+
245+
expectDoesNotThrow({ try fh.write(contentsOf: data) }, "Writing must succeed")
246+
247+
expectDoesNotThrow({ try fh.close() }, "Closing write handle must succeed")
248+
249+
var readData: Data?
250+
expectDoesNotThrow({ readData = try Data(contentsOf: url) }, "Must be able to read data")
251+
252+
expectEqual(readData, expectation ?? content, "The content must be the same")
246253
}
247254

248255
func testWritingWithData() {

0 commit comments

Comments
 (0)