Skip to content

Commit b9ecbed

Browse files
[Test] Further avoid try! in the tests
Get rid of two `try!` in the tests that can actually throw in error conditions. When error throws, it should results in test failures, not crashing the entire XCTest suite.
1 parent 89c80d8 commit b9ecbed

File tree

2 files changed

+68
-60
lines changed

2 files changed

+68
-60
lines changed

Tests/SwiftDriverTests/CachingBuildTests.swift

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -674,38 +674,42 @@ final class CachingBuildTests: XCTestCase {
674674
// FIXME: We need to differentiate the scanning action hash,
675675
// though the module-name above should be sufficient.
676676
"-I/tmp/foo/bar/\(index)"]
677-
let dependencyGraph =
678-
try! dependencyOracle.getDependencies(workingDirectory: path,
679-
commandLine: iterationCommand)
680-
681-
// The _Concurrency and _StringProcessing modules are automatically
682-
// imported in newer versions of the Swift compiler. If they happened to
683-
// be provided, adjust our expectations accordingly.
684-
let hasConcurrencyModule = dependencyGraph.modules.keys.contains {
685-
$0.moduleName == "_Concurrency"
686-
}
687-
let hasConcurrencyShimsModule = dependencyGraph.modules.keys.contains {
688-
$0.moduleName == "_SwiftConcurrencyShims"
689-
}
690-
let hasStringProcessingModule = dependencyGraph.modules.keys.contains {
691-
$0.moduleName == "_StringProcessing"
692-
}
693-
let adjustedExpectedNumberOfDependencies =
694-
expectedNumberOfDependencies +
695-
(hasConcurrencyModule ? 1 : 0) +
696-
(hasConcurrencyShimsModule ? 1 : 0) +
697-
(hasStringProcessingModule ? 1 : 0)
698-
699-
if (dependencyGraph.modules.count != adjustedExpectedNumberOfDependencies) {
700-
lock.lock()
701-
print("Unexpected Dependency Scanning Result (\(dependencyGraph.modules.count) modules):")
702-
dependencyGraph.modules.forEach {
703-
print($0.key.moduleName)
677+
do {
678+
let dependencyGraph =
679+
try dependencyOracle.getDependencies(workingDirectory: path,
680+
commandLine: iterationCommand)
681+
682+
// The _Concurrency and _StringProcessing modules are automatically
683+
// imported in newer versions of the Swift compiler. If they happened to
684+
// be provided, adjust our expectations accordingly.
685+
let hasConcurrencyModule = dependencyGraph.modules.keys.contains {
686+
$0.moduleName == "_Concurrency"
687+
}
688+
let hasConcurrencyShimsModule = dependencyGraph.modules.keys.contains {
689+
$0.moduleName == "_SwiftConcurrencyShims"
690+
}
691+
let hasStringProcessingModule = dependencyGraph.modules.keys.contains {
692+
$0.moduleName == "_StringProcessing"
693+
}
694+
let adjustedExpectedNumberOfDependencies =
695+
expectedNumberOfDependencies +
696+
(hasConcurrencyModule ? 1 : 0) +
697+
(hasConcurrencyShimsModule ? 1 : 0) +
698+
(hasStringProcessingModule ? 1 : 0)
699+
700+
if (dependencyGraph.modules.count != adjustedExpectedNumberOfDependencies) {
701+
lock.lock()
702+
print("Unexpected Dependency Scanning Result (\(dependencyGraph.modules.count) modules):")
703+
dependencyGraph.modules.forEach {
704+
print($0.key.moduleName)
705+
}
706+
lock.unlock()
704707
}
705-
lock.unlock()
708+
XCTAssertTrue(dependencyGraph.modules.count ==
709+
adjustedExpectedNumberOfDependencies)
710+
} catch {
711+
XCTFail("Unexpected error: \(error)")
706712
}
707-
XCTAssertTrue(dependencyGraph.modules.count ==
708-
adjustedExpectedNumberOfDependencies)
709713
}
710714

711715
// Change CAS path is an error.

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,38 +1441,42 @@ final class ExplicitModuleBuildTests: XCTestCase {
14411441
// FIXME: We need to differentiate the scanning action hash,
14421442
// though the module-name above should be sufficient.
14431443
"-I/tmp/foo/bar/\(index)"]
1444-
let dependencyGraph =
1445-
try! dependencyOracle.getDependencies(workingDirectory: path,
1446-
commandLine: iterationCommand)
1447-
1448-
// The _Concurrency and _StringProcessing modules are automatically
1449-
// imported in newer versions of the Swift compiler. If they happened to
1450-
// be provided, adjust our expectations accordingly.
1451-
let hasConcurrencyModule = dependencyGraph.modules.keys.contains {
1452-
$0.moduleName == "_Concurrency"
1453-
}
1454-
let hasConcurrencyShimsModule = dependencyGraph.modules.keys.contains {
1455-
$0.moduleName == "_SwiftConcurrencyShims"
1456-
}
1457-
let hasStringProcessingModule = dependencyGraph.modules.keys.contains {
1458-
$0.moduleName == "_StringProcessing"
1459-
}
1460-
let adjustedExpectedNumberOfDependencies =
1461-
expectedNumberOfDependencies +
1462-
(hasConcurrencyModule ? 1 : 0) +
1463-
(hasConcurrencyShimsModule ? 1 : 0) +
1464-
(hasStringProcessingModule ? 1 : 0)
1465-
1466-
if (dependencyGraph.modules.count != adjustedExpectedNumberOfDependencies) {
1467-
lock.lock()
1468-
print("Unexpected Dependency Scanning Result (\(dependencyGraph.modules.count) modules):")
1469-
dependencyGraph.modules.forEach {
1470-
print($0.key.moduleName)
1444+
do {
1445+
let dependencyGraph =
1446+
try dependencyOracle.getDependencies(workingDirectory: path,
1447+
commandLine: iterationCommand)
1448+
1449+
// The _Concurrency and _StringProcessing modules are automatically
1450+
// imported in newer versions of the Swift compiler. If they happened to
1451+
// be provided, adjust our expectations accordingly.
1452+
let hasConcurrencyModule = dependencyGraph.modules.keys.contains {
1453+
$0.moduleName == "_Concurrency"
1454+
}
1455+
let hasConcurrencyShimsModule = dependencyGraph.modules.keys.contains {
1456+
$0.moduleName == "_SwiftConcurrencyShims"
1457+
}
1458+
let hasStringProcessingModule = dependencyGraph.modules.keys.contains {
1459+
$0.moduleName == "_StringProcessing"
1460+
}
1461+
let adjustedExpectedNumberOfDependencies =
1462+
expectedNumberOfDependencies +
1463+
(hasConcurrencyModule ? 1 : 0) +
1464+
(hasConcurrencyShimsModule ? 1 : 0) +
1465+
(hasStringProcessingModule ? 1 : 0)
1466+
1467+
if (dependencyGraph.modules.count != adjustedExpectedNumberOfDependencies) {
1468+
lock.lock()
1469+
print("Unexpected Dependency Scanning Result (\(dependencyGraph.modules.count) modules):")
1470+
dependencyGraph.modules.forEach {
1471+
print($0.key.moduleName)
1472+
}
1473+
lock.unlock()
14711474
}
1472-
lock.unlock()
1475+
XCTAssertTrue(dependencyGraph.modules.count ==
1476+
adjustedExpectedNumberOfDependencies)
1477+
} catch {
1478+
XCTFail("Unexpected error: \(error)")
14731479
}
1474-
XCTAssertTrue(dependencyGraph.modules.count ==
1475-
adjustedExpectedNumberOfDependencies)
14761480
}
14771481
}
14781482
}

0 commit comments

Comments
 (0)