|
7 | 7 | // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
8 | 8 | //
|
9 | 9 |
|
| 10 | +import Dispatch |
| 11 | + |
10 | 12 | class TestProcess : XCTestCase {
|
11 | 13 |
|
12 | 14 | func test_exit0() throws {
|
@@ -699,6 +701,53 @@ class TestProcess : XCTestCase {
|
699 | 701 | }
|
700 | 702 | }
|
701 | 703 |
|
| 704 | + func test_currentDirectoryDoesNotChdirParentProcess() throws { |
| 705 | + // This test only behaves correctly on Linux and Windows: other platforms don't have an |
| 706 | + // appropriate API for this in posix_spawn or similar. |
| 707 | + #if os(Linux) || os(Windows) |
| 708 | + let backgroundQueue = DispatchQueue(label: "background-processor") |
| 709 | + let group = DispatchGroup() |
| 710 | + let startSemaphore = DispatchSemaphore() |
| 711 | + let currentWorkingDirectory = FileManager.shared.currentDirectoryPath |
| 712 | + var shouldRun = true |
| 713 | + let shouldRunLock = Lock() |
| 714 | + |
| 715 | + XCTAssertNotEqual(currentWorkingDirectory, "/") |
| 716 | + |
| 717 | + // Kick off the background task. This will spin on our current working directory and confirm |
| 718 | + // it doesn't change. |
| 719 | + backgroundQueue.async(group: group) { |
| 720 | + startSemaphore.signal() |
| 721 | + |
| 722 | + while true { |
| 723 | + let newCWD = FileManager.shared.currentDirectoryPath |
| 724 | + XCTAssertEqual(newCWD, currentWorkingDirectory) |
| 725 | + |
| 726 | + shouldRunLock.lock() |
| 727 | + if shouldRun { |
| 728 | + shouldRunLock.unlock() |
| 729 | + } else { |
| 730 | + shouldRunLock.unlock() |
| 731 | + break |
| 732 | + } |
| 733 | + } |
| 734 | + } |
| 735 | + |
| 736 | + startSemaphore.wait() |
| 737 | + |
| 738 | + // We run the task 50 times just to try to encourage it to fail. |
| 739 | + for _ in 0..<50 { |
| 740 | + XCTAssertNoThrow(try runTask([xdgTestHelperURL().path, "--getcwd"], currentDirectoryPath: "/")) |
| 741 | + } |
| 742 | + |
| 743 | + shouldRunLock.lock() |
| 744 | + shouldRun = false |
| 745 | + shouldRunLock.unlock() |
| 746 | + |
| 747 | + group.wait() |
| 748 | + #endif |
| 749 | + } |
| 750 | + |
702 | 751 | #if !os(Windows)
|
703 | 752 | func test_fileDescriptorsAreNotInherited() throws {
|
704 | 753 | let task = Process()
|
|
0 commit comments