File tree Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -16,13 +16,20 @@ let validPathSeps: [Character] = ["/"]
16
16
#endif
17
17
18
18
public func NSTemporaryDirectory( ) -> String {
19
+ func normalizedPath( with path: String ) -> String {
20
+ if validPathSeps. contains ( where: { path. hasSuffix ( String ( $0) ) } ) {
21
+ return path
22
+ } else {
23
+ return path + String( validPathSeps. last!)
24
+ }
25
+ }
19
26
#if os(Windows)
20
27
let cchLength : DWORD = GetTempPathW ( 0 , nil )
21
28
var wszPath : [ WCHAR ] = Array < WCHAR > ( repeating: 0 , count: Int ( cchLength + 1 ) )
22
29
guard GetTempPathW ( DWORD ( wszPath. count) , & wszPath) <= cchLength else {
23
30
preconditionFailure ( " GetTempPathW mutation race " )
24
31
}
25
- return String ( decodingCString: wszPath, as: UTF16 . self) . standardizingPath
32
+ return normalizedPath ( with : String ( decodingCString: wszPath, as: UTF16 . self) . standardizingPath)
26
33
#else
27
34
#if canImport(Darwin)
28
35
let safe_confstr = { ( name: Int32 , buf: UnsafeMutablePointer < Int8 > ? , len: Int ) -> Int in
@@ -57,11 +64,7 @@ public func NSTemporaryDirectory() -> String {
57
64
}
58
65
#endif
59
66
if let tmpdir = ProcessInfo . processInfo. environment [ " TMPDIR " ] {
60
- if !validPathSeps. contains ( where: { tmpdir. hasSuffix ( String ( $0) ) } ) {
61
- return tmpdir + " / "
62
- } else {
63
- return tmpdir
64
- }
67
+ return normalizedPath ( with: tmpdir)
65
68
}
66
69
#if os(Android)
67
70
// Bionic uses /data/local/tmp/ as temporary directory. TMPDIR is rarely
Original file line number Diff line number Diff line change @@ -24,6 +24,11 @@ class TestFileManager : XCTestCase {
24
24
let pathSep = " / "
25
25
#endif
26
26
27
+ func test_NSTemporaryDirectory( ) {
28
+ let tempDir = NSTemporaryDirectory ( )
29
+ XCTAssertTrue ( validPathSeps. contains ( where: { tempDir. hasSuffix ( String ( $0) ) } ) , " Temporary directory path must end with path separator " )
30
+ }
31
+
27
32
func test_createDirectory( ) {
28
33
let fm = FileManager . default
29
34
let path = NSTemporaryDirectory ( ) + " testdir \( NSUUID ( ) . uuidString) "
@@ -1974,6 +1979,7 @@ VIDEOS=StopgapVideos
1974
1979
/* ⚠️ */ ( " test_replacement " , testExpectedToFail ( test_replacement,
1975
1980
/* ⚠️ */ " <https://bugs.swift.org/browse/SR-10819> Re-enable Foundation test TestFileManager.test_replacement " ) ) ,
1976
1981
( " test_concurrentGetItemReplacementDirectory " , test_concurrentGetItemReplacementDirectory) ,
1982
+ ( " test_NSTemporaryDirectory " , test_NSTemporaryDirectory) ,
1977
1983
]
1978
1984
1979
1985
#if !DEPLOYMENT_RUNTIME_OBJC && NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Android)
You can’t perform that action at this time.
0 commit comments