Skip to content

Commit fb29b40

Browse files
committed
Changed signature of createTemporaryDirectorry method. improved a way to create utf8 strings
Formatting fixes
1 parent 62354a7 commit fb29b40

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

Sources/Basics/TemporaryFile.swift

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ import TSCLibc
3333
/// - Throws: `MakeDirectoryError` and rethrows all errors from `body`.
3434
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
3535
public func withTemporaryDirectory<Result>(
36-
dir: AbsolutePath? = nil, prefix: String = "TemporaryDirectory" , _ body: (AbsolutePath, @escaping (AbsolutePath) -> Void) async throws -> Result
36+
dir: AbsolutePath? = nil,
37+
prefix: String = "TemporaryDirectory",
38+
_ body: (AbsolutePath, @escaping (AbsolutePath) -> Void) async throws -> Result
3739
) async throws -> Result {
38-
let template = try createTemporaryDirectoryTemplate(dir: dir, prefix: prefix)
39-
return try await body(AbsolutePath(String(cString: template))) { path in
40-
_ = try? FileManager.default.removeItem(atPath: path.pathString)
41-
}
40+
let temporaryDirectory = try createTemporaryDirectory(dir: dir, prefix: prefix)
41+
return try await body(temporaryDirectory) { path in
42+
_ = try? FileManager.default.removeItem(atPath: path.pathString)
43+
}
4244
}
4345

4446
/// Creates a temporary directory and evaluates a closure with the directory path as an argument.
@@ -59,27 +61,30 @@ public func withTemporaryDirectory<Result>(
5961
/// - Throws: `MakeDirectoryError` and rethrows all errors from `body`.
6062
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
6163
public func withTemporaryDirectory<Result>(
62-
dir: AbsolutePath? = nil, prefix: String = "TemporaryDirectory", removeTreeOnDeinit: Bool = false , _ body: (AbsolutePath) async throws -> Result
64+
dir: AbsolutePath? = nil,
65+
prefix: String = "TemporaryDirectory",
66+
removeTreeOnDeinit: Bool = false ,
67+
_ body: (AbsolutePath) async throws -> Result
6368
) async throws -> Result {
64-
try await withTemporaryDirectory(dir: dir, prefix: prefix) { path, cleanup in
65-
defer { if removeTreeOnDeinit { cleanup(path) } }
66-
return try await body(path)
67-
}
69+
try await withTemporaryDirectory(dir: dir, prefix: prefix) { path, cleanup in
70+
defer { if removeTreeOnDeinit { cleanup(path) } }
71+
return try await body(path)
72+
}
6873
}
6974

70-
private func createTemporaryDirectoryTemplate(dir: AbsolutePath?, prefix: String) throws -> [Int8] {
75+
private func createTemporaryDirectory(dir: AbsolutePath?, prefix: String) throws -> AbsolutePath {
7176
// Construct path to the temporary directory.
7277
let templatePath = try AbsolutePath(prefix + ".XXXXXX", relativeTo: determineTempDirectory(dir))
73-
78+
7479
// Convert templatePath to a C style string terminating with null char to be an valid input
7580
// to mkdtemp method. The XXXXXX in this string will be replaced by a random string
7681
// which will be the actual path to the temporary directory.
77-
var template = [UInt8](templatePath.pathString.utf8).map({ Int8($0) }) + [Int8(0)]
78-
82+
var template = templatePath.pathString.utf8CString.map { $0 }
83+
7984
if TSCLibc.mkdtemp(&template) == nil {
8085
throw MakeDirectoryError(errno: errno)
8186
}
82-
return template
87+
return AbsolutePath(String(cString: template))
8388
}
8489

8590
private extension MakeDirectoryError {

Tests/BasicsTests/TemporaryFileTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
This source file is part of the Swift.org open source project
3-
3+
44
Copyright (c) 2022 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
6-
6+
77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import XCTest
1212

@@ -31,7 +31,7 @@ class TemporaryAsyncFileTests: XCTestCase {
3131
return tempDirPath
3232
}
3333
XCTAssertFalse(localFileSystem.isDirectory(path1))
34-
34+
3535
// Test temp directory is not removed when its not empty.
3636
let path2: AbsolutePath = try await withTemporaryDirectory { tempDirPath in
3737
XCTAssertTrue(localFileSystem.isDirectory(tempDirPath))
@@ -42,15 +42,15 @@ class TemporaryAsyncFileTests: XCTestCase {
4242
return
4343
}
4444
await task.value
45-
45+
4646
try localFileSystem.writeFileContents(filePath, bytes: ByteString())
4747
return tempDirPath
4848
}
4949
XCTAssertTrue(localFileSystem.isDirectory(path2))
5050
// Cleanup.
5151
try FileManager.default.removeItem(atPath: path2.pathString)
5252
XCTAssertFalse(localFileSystem.isDirectory(path2))
53-
53+
5454
// Test temp directory is removed when its not empty and removeTreeOnDeinit is enabled.
5555
let path3: AbsolutePath = try await withTemporaryDirectory(removeTreeOnDeinit: true) { tempDirPath in
5656
XCTAssertTrue(localFileSystem.isDirectory(tempDirPath))
@@ -60,13 +60,13 @@ class TemporaryAsyncFileTests: XCTestCase {
6060
return
6161
}
6262
await task.value
63-
63+
6464
try localFileSystem.writeFileContents(filePath, bytes: ByteString())
6565
return tempDirPath
6666
}
6767
XCTAssertFalse(localFileSystem.isDirectory(path3))
6868
}
69-
69+
7070
func testCanCreateUniqueTempDirectories() async throws {
7171
let (pathOne, pathTwo): (AbsolutePath, AbsolutePath) = try await withTemporaryDirectory(removeTreeOnDeinit: true) { pathOne in
7272
let pathTwo: AbsolutePath = try await withTemporaryDirectory(removeTreeOnDeinit: true) { pathTwo in
@@ -75,7 +75,7 @@ class TemporaryAsyncFileTests: XCTestCase {
7575
return
7676
}
7777
await task.value
78-
78+
7979
XCTAssertTrue(localFileSystem.isDirectory(pathOne))
8080
XCTAssertTrue(localFileSystem.isDirectory(pathTwo))
8181
// Their paths should be different.

0 commit comments

Comments
 (0)