Skip to content

Commit 1fa303d

Browse files
authored
Make the commands tests robust against any working directory changes done by the tool being tested in the same process (as for example --package-path does). (#3868)
1 parent cdc2e95 commit 1fa303d

9 files changed

+41
-8
lines changed

Tests/CommandsTests/APIDiffTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import TSCBasic
1818
import Workspace
1919
import XCTest
2020

21-
final class APIDiffTests: XCTestCase {
21+
final class APIDiffTests: CommandsTestCase {
2222
@discardableResult
2323
private func execute(
2424
_ args: [String],

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct BuildResult {
2525
let binContents: [String]
2626
}
2727

28-
final class BuildToolTests: XCTestCase {
28+
final class BuildToolTests: CommandsTestCase {
2929
@discardableResult
3030
private func execute(
3131
_ args: [String],
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import TSCBasic
12+
import XCTest
13+
14+
class CommandsTestCase: XCTestCase {
15+
16+
/// Original working directory before the test ran (if known).
17+
private var originalWorkingDirectory: AbsolutePath? = .none
18+
19+
override func setUp() {
20+
originalWorkingDirectory = localFileSystem.currentWorkingDirectory
21+
}
22+
23+
override func tearDown() {
24+
if let originalWorkingDirectory = originalWorkingDirectory {
25+
try? localFileSystem.changeCurrentWorkingDirectory(to: originalWorkingDirectory)
26+
}
27+
}
28+
29+
// FIXME: We should also hoist the `execute()` helper function that the various test suites implement, but right now they all seem to have slightly different implementations, so that's a later project.
30+
}

Tests/CommandsTests/MultiRootSupportTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import TSCBasic
1515
import Workspace
1616
import XCTest
1717

18-
final class MultiRootSupportTests: XCTestCase {
18+
final class MultiRootSupportTests: CommandsTestCase {
1919

2020
func testWorkspaceLoader() throws {
2121
let fs = InMemoryFileSystem(emptyFiles: [

Tests/CommandsTests/PackageRegistryToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import XCTest
1919
let defaultRegistryBaseURL = URL(string: "https://packages.example.com")!
2020
let customRegistryBaseURL = URL(string: "https://custom.packages.example.com")!
2121

22-
final class PackageRegistryToolTests: XCTestCase {
22+
final class PackageRegistryToolTests: CommandsTestCase {
2323
@discardableResult
2424
private func execute(
2525
_ args: [String],

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Workspace
2222
import Xcodeproj
2323
import XCTest
2424

25-
final class PackageToolTests: XCTestCase {
25+
final class PackageToolTests: CommandsTestCase {
2626
@discardableResult
2727
private func execute(
2828
_ args: [String],

Tests/CommandsTests/RunToolTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import SPMTestSupport
1414
import Commands
1515
import TSCBasic
1616

17-
final class RunToolTests: XCTestCase {
17+
final class RunToolTests: CommandsTestCase {
18+
1819
private func execute(
1920
_ args: [String],
2021
packagePath: AbsolutePath? = nil

Tests/CommandsTests/SwiftToolTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import SPMTestSupport
1414
import TSCBasic
1515
import XCTest
1616

17-
final class SwiftToolTests: XCTestCase {
17+
final class SwiftToolTests: CommandsTestCase {
18+
1819
func testVerbosityLogLevel() throws {
1920
fixture(name: "Miscellaneous/Simple") { packageRoot in
2021
do {

Tests/CommandsTests/TestToolTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import XCTest
1313
import SPMTestSupport
1414
import Commands
1515

16-
final class TestToolTests: XCTestCase {
16+
final class TestToolTests: CommandsTestCase {
17+
1718
private func execute(_ args: [String]) throws -> (stdout: String, stderr: String) {
1819
return try SwiftPMProduct.SwiftTest.execute(args)
1920
}

0 commit comments

Comments
 (0)