Skip to content

TestFoundation: change CWD to TMPDIR and restore previous CWD when test finishes #2606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions TestFoundation/TestURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class TestURL : XCTestCase {
static var gBaseCurrentWorkingDirectoryPath : String {
return FileManager.default.currentDirectoryPath
}
static var gSavedPath = ""
static var gRelativeOffsetFromBaseCurrentWorkingDirectory: UInt = 0
static let gFileExistsName = "TestCFURL_file_exists\(ProcessInfo.processInfo.globallyUniqueString)"
static let gFileDoesNotExistName = "TestCFURL_file_does_not_exist"
Expand Down Expand Up @@ -339,9 +340,8 @@ class TestURL : XCTestCase {
}
}

#if os(Android)
FileManager.default.changeCurrentDirectoryPath("/data/local/tmp")
#endif
TestURL.gSavedPath = FileManager.default.currentDirectoryPath
FileManager.default.changeCurrentDirectoryPath(NSTemporaryDirectory())

let cwd = FileManager.default.currentDirectoryPath
let cwdURL = URL(fileURLWithPath: cwd, isDirectory: true)
Expand All @@ -358,6 +358,7 @@ class TestURL : XCTestCase {
let error = strerror(errno)!
XCTFail("Failed to set up test paths: \(String(cString: error))")
}
defer { FileManager.default.changeCurrentDirectoryPath(TestURL.gSavedPath) }

// test with file that exists
var path = TestURL.gFileExistsPath
Expand Down Expand Up @@ -403,6 +404,7 @@ class TestURL : XCTestCase {
let error = strerror(errno)!
XCTFail("Failed to set up test paths: \(String(cString: error))")
}
defer { FileManager.default.changeCurrentDirectoryPath(TestURL.gSavedPath) }

// test with file that exists
var path = TestURL.gFileExistsPath
Expand Down