Skip to content

Commit 9954eb4

Browse files
committed
Refactor validation tests to avoid command invocation
1 parent 224e1f4 commit 9954eb4

File tree

1 file changed

+142
-129
lines changed

1 file changed

+142
-129
lines changed

Tests/CommandsTests/TestCommandTests.swift

Lines changed: 142 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,22 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Foundation
13+
@testable import Commands
14+
@testable import CoreCommands
1415

16+
import Foundation
1517
import Basics
16-
import Commands
1718
import struct SPMBuildCore.BuildSystemProvider
1819
import enum PackageModel.BuildConfiguration
1920
import PackageModel
2021
import _InternalTestSupport
2122
import TSCTestSupport
2223
import Testing
2324

25+
import struct ArgumentParser.ExitCode
26+
import protocol ArgumentParser.AsyncParsableCommand
27+
import class TSCBasic.BufferedOutputByteStream
28+
2429
fileprivate func execute(
2530
_ args: [String],
2631
packagePath: AbsolutePath? = nil,
@@ -1178,165 +1183,117 @@ struct TestCommandTests {
11781183
struct LLDBTests {
11791184
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
11801185
func lldbWithParallelThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1181-
let configuration = BuildConfiguration.debug
1182-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1183-
let error = await #expect(throws: SwiftPMError.self) {
1184-
try await execute(
1185-
["--debugger", "--parallel"],
1186-
packagePath: fixturePath,
1187-
configuration: configuration,
1188-
buildSystem: buildSystem
1189-
)
1190-
}
1186+
let args = args(["--debugger", "--parallel"], for: buildSystem)
1187+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1188+
let (state, outputStream) = try commandState()
11911189

1192-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1193-
Issue.record("Incorrect error was raised.")
1194-
return
1195-
}
1196-
1197-
#expect(
1198-
stderr.contains("error: --debugger cannot be used with --parallel (debugging requires sequential execution)"),
1199-
"got stdout: \(stdout), stderr: \(stderr)",
1200-
)
1190+
let error = await #expect(throws: ExitCode.self) {
1191+
try await command.run(state)
12011192
}
1193+
1194+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
1195+
1196+
let errorDescription = outputStream.bytes.description
1197+
#expect(
1198+
errorDescription.contains("--debugger cannot be used with --parallel"),
1199+
"Expected error about incompatible flags, got: \(errorDescription)"
1200+
)
12021201
}
12031202

12041203
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
12051204
func lldbWithNumWorkersThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1206-
let configuration = BuildConfiguration.debug
1207-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1208-
let error = await #expect(throws: SwiftPMError.self) {
1209-
try await execute(
1210-
["--debugger", "--parallel", "--num-workers", "2"],
1211-
packagePath: fixturePath,
1212-
configuration: configuration,
1213-
buildSystem: buildSystem,
1214-
)
1215-
}
1216-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1217-
Issue.record("Incorrect error was raised.")
1218-
return
1219-
}
1205+
let args = args(["--debugger", "--parallel", "--num-workers", "2"], for: buildSystem)
1206+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1207+
let (state, outputStream) = try commandState()
12201208

1221-
// Should hit the --parallel error first since validation is done in order
1222-
#expect(
1223-
stderr.contains("error: --debugger cannot be used with --parallel (debugging requires sequential execution)"),
1224-
"got stdout: \(stdout), stderr: \(stderr)",
1225-
)
1209+
let error = await #expect(throws: ExitCode.self) {
1210+
try await command.run(state)
12261211
}
1212+
1213+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
1214+
1215+
let errorDescription = outputStream.bytes.description
1216+
// Should hit the --parallel error first since validation is done in order
1217+
#expect(
1218+
errorDescription.contains("--debugger cannot be used with --parallel"),
1219+
"Expected error about incompatible flags, got: \(errorDescription)"
1220+
)
12271221
}
12281222

12291223
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
12301224
func lldbWithNumWorkersOnlyThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1231-
let configuration = BuildConfiguration.debug
1232-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1233-
let error = await #expect(throws: SwiftPMError.self) {
1234-
try await execute(
1235-
["--debugger", "--num-workers", "2"],
1236-
packagePath: fixturePath,
1237-
configuration: configuration,
1238-
buildSystem: buildSystem,
1239-
)
1240-
}
1241-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1242-
Issue.record("Incorrect error was raised.")
1243-
return
1244-
}
1225+
let args = args(["--debugger", "--num-workers", "2"], for: buildSystem)
1226+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1227+
let (state, outputStream) = try commandState()
12451228

1246-
#expect(
1247-
stderr.contains("error: --debugger cannot be used with --num-workers (debugging requires sequential execution)"),
1248-
"got stdout: \(stdout), stderr: \(stderr)",
1249-
)
1229+
let error = await #expect(throws: ExitCode.self) {
1230+
try await command.run(state)
12501231
}
1232+
1233+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
1234+
1235+
let errorDescription = outputStream.bytes.description
1236+
#expect(
1237+
errorDescription.contains("--debugger cannot be used with --num-workers"),
1238+
"Expected error about incompatible flags, got: \(errorDescription)"
1239+
)
12511240
}
12521241

12531242
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
12541243
func lldbWithListTestsThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1255-
let configuration = BuildConfiguration.debug
1256-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1257-
let error = await #expect(throws: SwiftPMError.self) {
1258-
try await execute(
1259-
["--debugger", "--list-tests"],
1260-
packagePath: fixturePath,
1261-
configuration: configuration,
1262-
buildSystem: buildSystem,
1263-
)
1264-
}
1265-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1266-
Issue.record("Incorrect error was raised.")
1267-
return
1268-
}
1244+
let args = args(["--debugger", "--list-tests"], for: buildSystem)
1245+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1246+
let (state, outputStream) = try commandState()
12691247

1270-
#expect(
1271-
stderr.contains("error: --debugger cannot be used with --list-tests (use 'swift test list' for listing tests)"),
1272-
"got stdout: \(stdout), stderr: \(stderr)",
1273-
)
1248+
let error = await #expect(throws: ExitCode.self) {
1249+
try await command.run(state)
12741250
}
1251+
1252+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
1253+
1254+
let errorDescription = outputStream.bytes.description
1255+
#expect(
1256+
errorDescription.contains("--debugger cannot be used with --list-tests"),
1257+
"Expected error about incompatible flags, got: \(errorDescription)"
1258+
)
12751259
}
12761260

12771261
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
12781262
func lldbWithShowCodecovPathThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1279-
let configuration = BuildConfiguration.debug
1280-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1281-
let error = await #expect(throws: SwiftPMError.self) {
1282-
try await execute(
1283-
["--debugger", "--show-codecov-path"],
1284-
packagePath: fixturePath,
1285-
configuration: configuration,
1286-
buildSystem: buildSystem,
1287-
)
1288-
}
1289-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1290-
Issue.record("Incorrect error was raised.")
1291-
return
1292-
}
1263+
let args = args(["--debugger", "--show-codecov-path"], for: buildSystem)
1264+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1265+
let (state, outputStream) = try commandState()
12931266

1294-
#expect(
1295-
stderr.contains("error: --debugger cannot be used with --show-codecov-path (debugging session cannot show paths)"),
1296-
"got stdout: \(stdout), stderr: \(stderr)",
1297-
)
1267+
let error = await #expect(throws: ExitCode.self) {
1268+
try await command.run(state)
12981269
}
1270+
1271+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
1272+
1273+
let errorDescription = outputStream.bytes.description
1274+
#expect(
1275+
errorDescription.contains("--debugger cannot be used with --show-codecov-path"),
1276+
"Expected error about incompatible flags, got: \(errorDescription)"
1277+
)
12991278
}
13001279

13011280
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
13021281
func lldbWithReleaseConfigurationThrowsError(buildSystem: BuildSystemProvider.Kind) async throws {
1303-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1304-
let error = await #expect(throws: SwiftPMError.self) {
1305-
try await execute(
1306-
["--debugger", "-c", "release"],
1307-
packagePath: fixturePath,
1308-
configuration: .release,
1309-
buildSystem: buildSystem,
1310-
)
1311-
}
1312-
guard case let SwiftPMError.executionFailure(_, stdout, stderr) = try #require(error) else {
1313-
Issue.record("Incorrect error was raised.")
1314-
return
1315-
}
1282+
let args = args(["--debugger"], for: buildSystem, buildConfiguration: .release)
1283+
let command = try #require(SwiftTestCommand.parseAsRoot(args) as? SwiftTestCommand)
1284+
let (state, outputStream) = try commandState()
13161285

1317-
#expect(
1318-
stderr.contains("error: --debugger cannot be used with release configuration (debugging requires debug symbols)"),
1319-
"got stdout: \(stdout), stderr: \(stderr)",
1320-
)
1286+
let error = await #expect(throws: ExitCode.self) {
1287+
try await command.run(state)
13211288
}
1322-
}
13231289

1324-
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
1325-
func lldbWithCompatibleFlagsDoesNotThrowValidationError(buildSystem: BuildSystemProvider.Kind) async throws {
1326-
let configuration = BuildConfiguration.debug
1327-
try await fixture(name: "Miscellaneous/EchoExecutable") { fixturePath in
1328-
let (stdout, stderr) = try await execute(
1329-
["--debugger", "--filter", ".*", "--skip", "sometest", "--enable-testable-imports"],
1330-
packagePath: fixturePath,
1331-
configuration: configuration,
1332-
buildSystem: buildSystem,
1333-
)
1290+
#expect(error == ExitCode.failure, "Expected ExitCode.failure, got \(String(describing: error))")
13341291

1335-
#expect(
1336-
!stderr.contains("error: --debugger cannot be used with"),
1337-
"got stdout: \(stdout), stderr: \(stderr)",
1338-
)
1339-
}
1292+
let errorDescription = outputStream.bytes.description
1293+
#expect(
1294+
errorDescription.contains("--debugger cannot be used with release configuration"),
1295+
"Expected error about incompatible flags, got: \(errorDescription)"
1296+
)
13401297
}
13411298

13421299
@Test(arguments: SupportedBuildSystemOnAllPlatforms)
@@ -1355,8 +1312,14 @@ struct TestCommandTests {
13551312
"got stdout: \(stdout), stderr: \(stderr)",
13561313
)
13571314

1315+
#if os(macOS)
1316+
let targetName = "xctest"
1317+
#else
1318+
let targetName = buildSystem == .swiftbuild ? "test-runner" : "xctest"
1319+
#endif
1320+
13581321
#expect(
1359-
stdout.contains("target create") && stdout.contains("xctest"),
1322+
stdout.contains("target create") && stdout.contains(targetName),
13601323
"Expected LLDB to target xctest binary, got stdout: \(stdout), stderr: \(stderr)",
13611324
)
13621325

@@ -1383,8 +1346,14 @@ struct TestCommandTests {
13831346
"got stdout: \(stdout), stderr: \(stderr)",
13841347
)
13851348

1349+
#if os(macOS)
1350+
let targetName = "swiftpm-testing-helper"
1351+
#else
1352+
let targetName = "TestDebuggingTests-test-runner"
1353+
#endif
1354+
13861355
#expect(
1387-
stdout.contains("target create") && stdout.contains("swiftpm-testing-helper"),
1356+
stdout.contains("target create") && stdout.contains(targetName),
13881357
"Expected LLDB to target swiftpm-testing-helper binary, got stdout: \(stdout), stderr: \(stderr)",
13891358
)
13901359

@@ -1427,5 +1396,49 @@ struct TestCommandTests {
14271396
)
14281397
}
14291398
}
1399+
1400+
func args(_ args: [String], for buildSystem: BuildSystemProvider.Kind, buildConfiguration: BuildConfiguration = .debug) -> [String] {
1401+
return args + buildConfiguration.args + getBuildSystemArgs(for: buildSystem)
1402+
}
1403+
1404+
func commandState() throws -> (SwiftCommandState, BufferedOutputByteStream) {
1405+
let outputStream = BufferedOutputByteStream()
1406+
1407+
let state = try SwiftCommandState(
1408+
outputStream: outputStream,
1409+
options: try GlobalOptions.parse([]),
1410+
toolWorkspaceConfiguration: .init(shouldInstallSignalHandlers: false),
1411+
workspaceDelegateProvider: {
1412+
CommandWorkspaceDelegate(
1413+
observabilityScope: $0,
1414+
outputHandler: $1,
1415+
progressHandler: $2,
1416+
inputHandler: $3
1417+
)
1418+
},
1419+
workspaceLoaderProvider: {
1420+
XcodeWorkspaceLoader(
1421+
fileSystem: $0,
1422+
observabilityScope: $1
1423+
)
1424+
},
1425+
createPackagePath: false
1426+
)
1427+
return (state, outputStream)
1428+
}
14301429
}
14311430
}
1431+
1432+
fileprivate extension BuildConfiguration {
1433+
var args: [String] {
1434+
var args = ["--configuration"]
1435+
switch self {
1436+
case .debug:
1437+
args.append("debug")
1438+
case .release:
1439+
args.append("release")
1440+
}
1441+
return args
1442+
}
1443+
}
1444+

0 commit comments

Comments
 (0)