Skip to content

Commit 0a0b867

Browse files
committed
Fixes for running tests on Redhat.
- Process: When setting the current directory for a sub-process, set the PWD environment variable to this directory if the default environment is used. This matches Darwin's behaviour. - Use xdgTestHelper as an external test program for TestProcess, taking options for getcwd() and getenv("PWD"). - SR-7748: Use /tmp as a test directory and run it through realpath() to test against the result of getcwd(). On macOS, /tmp is a symlink to private/tmp. - SR-7747: FileManager.attributesOfFileSystem(forPath:) may return .systemNumber as 0 on some systems (eg tmpfs on Redhat). Remove this invalid test.
1 parent e524412 commit 0a0b867

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

Foundation/Process.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -292,25 +292,25 @@ open class Process: NSObject {
292292
}
293293
argv.deallocate()
294294
}
295-
296-
let envp: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>
297-
298-
if let env = environment {
299-
let nenv = env.count
300-
envp = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1 + nenv)
301-
envp.initialize(from: env.map { strdup("\($0)=\($1)") }, count: nenv)
302-
envp[env.count] = nil
295+
296+
var env: [String: String]
297+
if let e = environment {
298+
env = e
303299
} else {
304-
envp = _CFEnviron()
300+
env = ProcessInfo.processInfo.environment
301+
env["PWD"] = currentDirectoryURL.path
305302
}
306303

304+
let nenv = env.count
305+
let envp = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1 + nenv)
306+
envp.initialize(from: env.map { strdup("\($0)=\($1)") }, count: nenv)
307+
envp[env.count] = nil
308+
307309
defer {
308-
if let env = environment {
309-
for pair in envp ..< envp + env.count {
310-
free(UnsafeMutableRawPointer(pair.pointee))
311-
}
312-
envp.deallocate()
310+
for pair in envp ..< envp + env.count {
311+
free(UnsafeMutableRawPointer(pair.pointee))
313312
}
313+
envp.deallocate()
314314
}
315315

316316
var taskSocketPair : [Int32] = [0, 0]

TestFoundation/TestFileManager.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ class TestFileManager : XCTestCase {
279279

280280
let systemNumber = attrs[.systemNumber] as? NSNumber
281281
XCTAssertNotNil(systemNumber)
282-
XCTAssertNotEqual(systemNumber!.uint64Value, 0)
283282

284283
let systemFreeSize = attrs[.systemFreeSize] as? NSNumber
285284
XCTAssertNotNil(systemFreeSize)

TestFoundation/TestHTTPCookieStorage.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ class TestHTTPCookieStorage: XCTestCase {
299299
// Test by setting the environmental variable
300300
let task = Process()
301301
task.executableURL = xdgTestHelperURL()
302+
task.arguments = ["--xdgcheck"]
302303
var environment = ProcessInfo.processInfo.environment
303304
let testPath = NSHomeDirectory() + "/TestXDG"
304305
environment["XDG_DATA_HOME"] = testPath

TestFoundation/TestProcess.swift

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,72 @@ class TestProcess : XCTestCase {
275275
}
276276
}
277277

278+
private func realpathOf(path: String) -> String? {
279+
let fm = FileManager.default
280+
let length = Int(PATH_MAX) + 1
281+
var buf = [Int8](repeating: 0, count: length)
282+
let fsRep = fm.fileSystemRepresentation(withPath: path)
283+
#if !DARWIN_COMPATIBILITY_TESTS
284+
defer { fsRep.deallocate() }
285+
#endif
286+
guard let result = realpath(fsRep, &buf) else {
287+
return nil
288+
}
289+
return fm.string(withFileSystemRepresentation: result, length: strlen(result))
290+
}
291+
278292
func test_current_working_directory() {
293+
let tmpDir = "/tmp"
294+
295+
guard let resolvedTmpDir = realpathOf(path: tmpDir) else {
296+
XCTFail("Cant find realpath of /tmp")
297+
return
298+
}
299+
300+
let fm = FileManager.default
301+
let previousWorkingDirectory = fm.currentDirectoryPath
302+
303+
// Test that getcwd() returns the currentDirectoryPath
304+
do {
305+
let (pwd, _) = try runTask([xdgTestHelperURL().path, "--getcwd"], currentDirectoryPath: tmpDir)
306+
// Check the sub-process used the correct directory
307+
XCTAssertEqual(pwd.trimmingCharacters(in: .newlines), resolvedTmpDir)
308+
} catch let error {
309+
XCTFail("Test failed: \(error)")
310+
}
311+
312+
// Test that $PWD by default is set to currentDirectoryPath
279313
do {
280-
let previousWorkingDirectory = FileManager.default.currentDirectoryPath
314+
let (pwd, _) = try runTask([xdgTestHelperURL().path, "--echo-PWD"], currentDirectoryPath: tmpDir)
315+
// Check the sub-process used the correct directory
316+
XCTAssertEqual(pwd.trimmingCharacters(in: .newlines), tmpDir)
317+
} catch let error {
318+
XCTFail("Test failed: \(error)")
319+
}
281320

282-
// Darwin Foundation requires the full path to the executable (.launchPath)
283-
let (output, _) = try runTask(["/bin/bash", "-c", "pwd"], currentDirectoryPath: "/bin")
284-
XCTAssertEqual(output.trimmingCharacters(in: .newlines), "/bin")
285-
XCTAssertEqual(previousWorkingDirectory, FileManager.default.currentDirectoryPath)
321+
// Test that $PWD can be over-ridden
322+
do {
323+
var env = ProcessInfo.processInfo.environment
324+
env["PWD"] = "/bin"
325+
let (pwd, _) = try runTask([xdgTestHelperURL().path, "--echo-PWD"], environment: env, currentDirectoryPath: tmpDir)
326+
// Check the sub-process used the correct directory
327+
XCTAssertEqual(pwd.trimmingCharacters(in: .newlines), "/bin")
286328
} catch let error {
287329
XCTFail("Test failed: \(error)")
288330
}
331+
332+
// Test that $PWD can be set to empty
333+
do {
334+
var env = ProcessInfo.processInfo.environment
335+
env["PWD"] = ""
336+
let (pwd, _) = try runTask([xdgTestHelperURL().path, "--echo-PWD"], environment: env, currentDirectoryPath: tmpDir)
337+
// Check the sub-process used the correct directory
338+
XCTAssertEqual(pwd.trimmingCharacters(in: .newlines), "")
339+
} catch let error {
340+
XCTFail("Test failed: \(error)")
341+
}
342+
343+
XCTAssertEqual(previousWorkingDirectory, fm.currentDirectoryPath)
289344
}
290345

291346
func test_run() {
@@ -360,7 +415,7 @@ private func runTask(_ arguments: [String], environment: [String: String]? = nil
360415
let stderrPipe = Pipe()
361416
process.standardOutput = stdoutPipe
362417
process.standardError = stderrPipe
363-
process.launch()
418+
try process.run()
364419
process.waitUntilExit()
365420

366421
guard process.terminationStatus == 0 else {

TestFoundation/xdgTestHelper/main.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,15 @@ class XDGCheck {
5050
}
5151
}
5252

53-
XDGCheck.run()
53+
if let arg = ProcessInfo.processInfo.arguments.last {
54+
if arg == "--xdgcheck" {
55+
XDGCheck.run()
56+
}
57+
if arg == "--getcwd" {
58+
print(FileManager.default.currentDirectoryPath)
59+
}
60+
if arg == "--echo-PWD" {
61+
print(ProcessInfo.processInfo.environment["PWD"] ?? "")
62+
}
63+
}
5464

0 commit comments

Comments
 (0)