Skip to content

Commit bdc0a1c

Browse files
committed
Use NSTemporaryDirectory helper function instead of hard coding /tmp (it properly checks the environment variable TMP)
1 parent 7d680cd commit bdc0a1c

8 files changed

+40
-40
lines changed

TestFoundation/TestNSArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class TestNSArray : XCTestCase {
500500
}
501501

502502
private func createTestFile(_ path: String, _contents: Data) -> String? {
503-
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
503+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
504504
do {
505505
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
506506
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents, attributes: nil) {

TestFoundation/TestNSBundle.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class TestNSBundle : XCTestCase {
108108

109109
private func _setupPlayground() -> String? {
110110
// Make sure the directory is uniquely named
111-
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
111+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
112112

113113
do {
114114
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
@@ -179,7 +179,7 @@ class TestNSBundle : XCTestCase {
179179
}
180180

181181
func test_bundleWithInvalidPath(){
182-
let bundleInvalid = Bundle(path: "/tmp/test.playground")
182+
let bundleInvalid = Bundle(path: NSTemporaryDirectory() + "test.playground")
183183
XCTAssertNil(bundleInvalid)
184184
}
185185

TestFoundation/TestNSDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ class TestNSDictionary : XCTestCase {
205205
}
206206

207207
private func createTestFile(_ path: String, _contents: Data) -> String? {
208-
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
208+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
209209
do {
210210
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
211211
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,

TestFoundation/TestNSFileManager.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class TestNSFileManager : XCTestCase {
3838

3939
func test_createDirectory() {
4040
let fm = FileManager.default
41-
let path = "/tmp/testdir\(NSUUID().uuidString)"
41+
let path = NSTemporaryDirectory() + "testdir\(NSUUID().uuidString)"
4242

4343
ignoreError { try fm.removeItem(atPath: path) }
4444

@@ -63,7 +63,7 @@ class TestNSFileManager : XCTestCase {
6363

6464
func test_createFile() {
6565
let fm = FileManager.default
66-
let path = "/tmp/testfile\(NSUUID().uuidString)"
66+
let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)"
6767

6868
ignoreError { try fm.removeItem(atPath: path) }
6969

@@ -83,8 +83,8 @@ class TestNSFileManager : XCTestCase {
8383

8484
func test_moveFile() {
8585
let fm = FileManager.default
86-
let path = "/tmp/testfile\(NSUUID().uuidString)"
87-
let path2 = "/tmp/testfile2\(NSUUID().uuidString)"
86+
let path = NSTemporaryDirectory() + "testfile\(NSUUID().uuidString)"
87+
let path2 = NSTemporaryDirectory() + "testfile2\(NSUUID().uuidString)"
8888

8989
func cleanup() {
9090
ignoreError { try fm.removeItem(atPath: path) }
@@ -114,7 +114,7 @@ class TestNSFileManager : XCTestCase {
114114

115115
func test_fileAttributes() {
116116
let fm = FileManager.default
117-
let path = "/tmp/test_fileAttributes\(NSUUID().uuidString)"
117+
let path = NSTemporaryDirectory() + "test_fileAttributes\(NSUUID().uuidString)"
118118

119119
ignoreError { try fm.removeItem(atPath: path) }
120120

@@ -161,7 +161,7 @@ class TestNSFileManager : XCTestCase {
161161
}
162162

163163
func test_setFileAttributes() {
164-
let path = "/tmp/test_setFileAttributes\(NSUUID().uuidString)"
164+
let path = NSTemporaryDirectory() + "test_setFileAttributes\(NSUUID().uuidString)"
165165
let fm = FileManager.default
166166

167167
ignoreError { try fm.removeItem(atPath: path) }
@@ -189,10 +189,10 @@ class TestNSFileManager : XCTestCase {
189189
func test_pathEnumerator() {
190190
let fm = FileManager.default
191191
let testDirName = "testdir\(NSUUID().uuidString)"
192-
let basePath = "/tmp/\(testDirName)"
193-
let itemPath = "/tmp/\(testDirName)/item"
194-
let basePath2 = "/tmp/\(testDirName)/path2"
195-
let itemPath2 = "/tmp/\(testDirName)/path2/item"
192+
let basePath = NSTemporaryDirectory() + "\(testDirName)"
193+
let itemPath = NSTemporaryDirectory() + "\(testDirName)/item"
194+
let basePath2 = NSTemporaryDirectory() + "\(testDirName)/path2"
195+
let itemPath2 = NSTemporaryDirectory() + "\(testDirName)/path2/item"
196196

197197
ignoreError { try fm.removeItem(atPath: basePath) }
198198

@@ -222,8 +222,8 @@ class TestNSFileManager : XCTestCase {
222222
func test_directoryEnumerator() {
223223
let fm = FileManager.default
224224
let testDirName = "testdir\(NSUUID().uuidString)"
225-
let path = "/tmp/\(testDirName)"
226-
let itemPath = "/tmp/\(testDirName)/item"
225+
let path = NSTemporaryDirectory() + "\(testDirName)"
226+
let itemPath = NSTemporaryDirectory() + "\(testDirName)/item"
227227

228228
ignoreError { try fm.removeItem(atPath: path) }
229229

@@ -244,8 +244,8 @@ class TestNSFileManager : XCTestCase {
244244
XCTFail()
245245
}
246246

247-
let subDirPath = "/tmp/\(testDirName)/testdir2"
248-
let subDirItemPath = "/tmp/\(testDirName)/testdir2/item"
247+
let subDirPath = NSTemporaryDirectory() + "\(testDirName)/testdir2"
248+
let subDirItemPath = NSTemporaryDirectory() + "\(testDirName)/testdir2/item"
249249
do {
250250
try fm.createDirectory(atPath: subDirPath, withIntermediateDirectories: false, attributes: nil)
251251
let _ = fm.createFile(atPath: subDirItemPath, contents: Data(), attributes: nil)
@@ -320,9 +320,9 @@ class TestNSFileManager : XCTestCase {
320320
func test_contentsOfDirectoryAtPath() {
321321
let fm = FileManager.default
322322
let testDirName = "testdir\(NSUUID().uuidString)"
323-
let path = "/tmp/\(testDirName)"
324-
let itemPath1 = "/tmp/\(testDirName)/item"
325-
let itemPath2 = "/tmp/\(testDirName)/item2"
323+
let path = NSTemporaryDirectory() + "\(testDirName)"
324+
let itemPath1 = NSTemporaryDirectory() + "\(testDirName)/item"
325+
let itemPath2 = NSTemporaryDirectory() + "\(testDirName)/item2"
326326

327327
ignoreError { try fm.removeItem(atPath: path) }
328328

@@ -363,11 +363,11 @@ class TestNSFileManager : XCTestCase {
363363

364364
func test_subpathsOfDirectoryAtPath() {
365365
let fm = FileManager.default
366-
let path = "/tmp/testdir"
367-
let path2 = "/tmp/testdir/sub"
368-
let itemPath1 = "/tmp/testdir/item"
369-
let itemPath2 = "/tmp/testdir/item2"
370-
let itemPath3 = "/tmp/testdir/sub/item3"
366+
let path = NSTemporaryDirectory() + "testdir"
367+
let path2 = NSTemporaryDirectory() + "testdir/sub"
368+
let itemPath1 = NSTemporaryDirectory() + "testdir/item"
369+
let itemPath2 = NSTemporaryDirectory() + "testdir/item2"
370+
let itemPath3 = NSTemporaryDirectory() + "testdir/sub/item3"
371371

372372
ignoreError { try fm.removeItem(atPath: path) }
373373

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ extension TestNSJSONSerialization {
942942
}
943943

944944
private func createTestFile(_ path: String,_contents: Data) -> String? {
945-
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
945+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
946946
do {
947947
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
948948
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,

TestFoundation/TestNSStream.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class TestNSStream : XCTestCase {
118118
}
119119

120120
func test_InputStreamInvalidPath() {
121-
let fileStream: InputStream = InputStream(fileAtPath: "/tmp/file.txt")!
121+
let fileStream: InputStream = InputStream(fileAtPath: NSTemporaryDirectory() + "file.txt")!
122122
XCTAssertEqual(Stream.Status.notOpen, fileStream.streamStatus)
123123
fileStream.open()
124124
XCTAssertEqual(Stream.Status.error, fileStream.streamStatus)
@@ -217,7 +217,7 @@ class TestNSStream : XCTestCase {
217217
}
218218

219219
private func createTestFile(_ path: String, _contents: Data) -> String? {
220-
let tempDir = "/tmp/TestFoundation_Playground_" + NSUUID().uuidString + "/"
220+
let tempDir = NSTemporaryDirectory() + "TestFoundation_Playground_" + NSUUID().uuidString + "/"
221221
do {
222222
try FileManager.default.createDirectory(atPath: tempDir, withIntermediateDirectories: false, attributes: nil)
223223
if FileManager.default.createFile(atPath: tempDir + "/" + path, contents: _contents,

TestFoundation/TestNSString.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,11 @@ class TestNSString : XCTestCase {
419419

420420
func test_completePathIntoString() {
421421
let fileNames = [
422-
"/tmp/Test_completePathIntoString_01",
423-
"/tmp/test_completePathIntoString_02",
424-
"/tmp/test_completePathIntoString_01.txt",
425-
"/tmp/test_completePathIntoString_01.dat",
426-
"/tmp/test_completePathIntoString_03.DAT"
422+
NSTemporaryDirectory() + "Test_completePathIntoString_01",
423+
NSTemporaryDirectory() + "test_completePathIntoString_02",
424+
NSTemporaryDirectory() + "test_completePathIntoString_01.txt",
425+
NSTemporaryDirectory() + "test_completePathIntoString_01.dat",
426+
NSTemporaryDirectory() + "test_completePathIntoString_03.DAT"
427427
]
428428

429429
guard ensureFiles(fileNames) else {
@@ -432,7 +432,7 @@ class TestNSString : XCTestCase {
432432
}
433433

434434
let tmpPath = { (path: String) -> String in
435-
return "/tmp/\(path)"
435+
return NSTemporaryDirectory() + "\(path)"
436436
}
437437

438438
do {
@@ -463,9 +463,9 @@ class TestNSString : XCTestCase {
463463
}
464464

465465
let fileNames2 = [
466-
"/tmp/ABC/",
467-
"/tmp/ABCD/",
468-
"/tmp/abcde"
466+
NSTemporaryDirectory() + "ABC/",
467+
NSTemporaryDirectory() + "ABCD/",
468+
NSTemporaryDirectory() + "abcde"
469469
]
470470

471471
guard ensureFiles(fileNames2) else {
@@ -571,7 +571,7 @@ class TestNSString : XCTestCase {
571571

572572
// Next check has no sense on Linux due to case sensitive file system.
573573
#if os(OSX)
574-
guard ensureFiles(["/tmp/ABC/temp.txt"]) else {
574+
guard ensureFiles([NSTemporaryDirectory() + "ABC/temp.txt"]) else {
575575
XCTAssert(false, "Could not create temp files for testing.")
576576
return
577577
}

TestFoundation/TestNSURL.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class TestNSURL : XCTestCase {
352352

353353
func test_URLByResolvingSymlinksInPath() {
354354
let files = [
355-
"/tmp/ABC/test_URLByResolvingSymlinksInPath"
355+
NSTemporaryDirectory() + "ABC/test_URLByResolvingSymlinksInPath"
356356
]
357357

358358
guard ensureFiles(files) else {

0 commit comments

Comments
 (0)