Skip to content

Commit ee41322

Browse files
committed
Eliminate nearly all remaining force casts in tests
Instead, make use of #require/throws to defend against runtime crashes.
1 parent 003777e commit ee41322

13 files changed

+165
-194
lines changed

Tests/SWBBuildSystemTests/BuildOperationDescriptionTests.swift

+48-77
Large diffs are not rendered by default.

Tests/SWBBuildSystemTests/BuildTaskBehaviorTests.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,10 @@ fileprivate struct BuildTaskBehaviorTests: CoreBasedTests {
415415
results.checkCapstoneEvents()
416416

417417
// Check the items in the diamond.
418-
let startTask = taskMap["START"]!.execTask as! Task
419-
let leftTask = taskMap["LEFT"]!.execTask as! Task
420-
let rightTask = taskMap["RIGHT"]!.execTask as! Task
421-
let endTask = taskMap["END"]!.execTask as! Task
418+
let startTask = try #require(taskMap["START"]?.execTask as? Task)
419+
let leftTask = try #require(taskMap["LEFT"]?.execTask as? Task)
420+
let rightTask = try #require(taskMap["RIGHT"]?.execTask as? Task)
421+
let endTask = try #require(taskMap["END"]?.execTask as? Task)
422422
results.check(event: .taskHadEvent(startTask, event: .completed), precedes: .taskHadEvent(leftTask, event: .started))
423423
results.check(event: .taskHadEvent(startTask, event: .completed), precedes: .taskHadEvent(rightTask, event: .started))
424424
results.check(event: .taskHadEvent(leftTask, event: .completed), precedes: .taskHadEvent(endTask, event: .started))
@@ -448,8 +448,8 @@ fileprivate struct BuildTaskBehaviorTests: CoreBasedTests {
448448
// NOTE: This test isn't completely deterministic, it could report a false negative, but in practice this is very unlikely. It should never have a false positive.
449449
results.checkCapstoneEvents()
450450
for name in tasksToMake {
451-
let task = taskMap[name]!.execTask as! Task
452-
results.check(event: .taskHadEvent(task, event: .completed), precedes: .taskHadEvent(gateTask.execTask as! Task, event: .started))
451+
let task = try #require(taskMap[name]?.execTask as? Task)
452+
results.check(event: .taskHadEvent(task, event: .completed), precedes: .taskHadEvent(try #require(gateTask.execTask as? Task), event: .started))
453453
}
454454
}
455455
}

Tests/SWBCorePerfTests/CommandLineSpecPerfTests.swift

+19-19
Original file line numberDiff line numberDiff line change
@@ -255,32 +255,32 @@ fileprivate struct CommandLineSpecPerfTests: CoreBasedTests, PerfTests {
255255
var (table, namespace) = clangSpec.macroTableForBuildOptionDefaults(core)
256256

257257
// We also add some other settings that are more contextual in nature.
258-
table.push(namespace.lookupMacroDeclaration("CURRENT_ARCH") as! StringMacroDeclaration, literal: "x86_64")
259-
table.push(namespace.lookupMacroDeclaration("arch") as! StringMacroDeclaration, literal: "x86_64")
260-
table.push(namespace.lookupMacroDeclaration("CURRENT_VARIANT") as! StringMacroDeclaration, literal: "normal")
261-
table.push(namespace.lookupMacroDeclaration("PRODUCT_NAME") as! StringMacroDeclaration, literal: "Product")
262-
table.push(namespace.lookupMacroDeclaration("TEMP_DIR") as! PathMacroDeclaration, literal: "/tmp")
263-
table.push(namespace.lookupMacroDeclaration("PROJECT_TEMP_DIR") as! PathMacroDeclaration, literal: "/tmp/ptmp")
264-
table.push(namespace.lookupMacroDeclaration("OBJECT_FILE_DIR") as! PathMacroDeclaration, literal: "/tmp/output/obj")
265-
table.push(namespace.lookupMacroDeclaration("BUILT_PRODUCTS_DIR") as! PathMacroDeclaration, literal: "/tmp/output/sym")
266-
table.push(namespace.lookupMacroDeclaration("GCC_PREFIX_HEADER") as! PathMacroDeclaration, literal: "/tmp/prefix.h")
267-
table.push(namespace.lookupMacroDeclaration("DERIVED_FILE_DIR") as! PathMacroDeclaration, literal: "/tmp/derived")
258+
table.push(try #require(namespace.lookupMacroDeclaration("CURRENT_ARCH") as? StringMacroDeclaration), literal: "x86_64")
259+
table.push(try #require(namespace.lookupMacroDeclaration("arch") as? StringMacroDeclaration), literal: "x86_64")
260+
table.push(try #require(namespace.lookupMacroDeclaration("CURRENT_VARIANT") as? StringMacroDeclaration), literal: "normal")
261+
table.push(try #require(namespace.lookupMacroDeclaration("PRODUCT_NAME") as? StringMacroDeclaration), literal: "Product")
262+
table.push(try #require(namespace.lookupMacroDeclaration("TEMP_DIR") as? PathMacroDeclaration), literal: "/tmp")
263+
table.push(try #require(namespace.lookupMacroDeclaration("PROJECT_TEMP_DIR") as? PathMacroDeclaration), literal: "/tmp/ptmp")
264+
table.push(try #require(namespace.lookupMacroDeclaration("OBJECT_FILE_DIR") as? PathMacroDeclaration), literal: "/tmp/output/obj")
265+
table.push(try #require(namespace.lookupMacroDeclaration("BUILT_PRODUCTS_DIR") as? PathMacroDeclaration), literal: "/tmp/output/sym")
266+
table.push(try #require(namespace.lookupMacroDeclaration("GCC_PREFIX_HEADER") as? PathMacroDeclaration), literal: "/tmp/prefix.h")
267+
table.push(try #require(namespace.lookupMacroDeclaration("DERIVED_FILE_DIR") as? PathMacroDeclaration), literal: "/tmp/derived")
268268
table.push(try namespace.declareUserDefinedMacro("PER_ARCH_CFLAGS_i386"), namespace.parseLiteralStringList(["-DX86.32"]))
269269
table.push(try namespace.declareUserDefinedMacro("PER_ARCH_CFLAGS_x86_64"), namespace.parseLiteralStringList(["-DX86.64"]))
270-
table.push(namespace.lookupMacroDeclaration("PER_ARCH_CFLAGS") as! StringListMacroDeclaration, BuiltinMacros.namespace.parseStringList("$(PER_ARCH_CFLAGS_$(CURRENT_ARCH)"))
270+
table.push(try #require(namespace.lookupMacroDeclaration("PER_ARCH_CFLAGS") as? StringListMacroDeclaration), BuiltinMacros.namespace.parseStringList("$(PER_ARCH_CFLAGS_$(CURRENT_ARCH)"))
271271
table.push(try namespace.declareUserDefinedMacro("OTHER_CFLAGS_normal"), namespace.parseLiteralStringList(["-DFrom_OTHER_CFLAGS_normal"]))
272272
table.push(try namespace.declareUserDefinedMacro("OTHER_CFLAGS_profile"), namespace.parseLiteralStringList(["-DFrom_OTHER_CFLAGS_profile"]))
273-
table.push(namespace.lookupMacroDeclaration("PER_VARIANT_CFLAGS") as! StringListMacroDeclaration, BuiltinMacros.namespace.parseStringList("$(OTHER_CFLAGS_$(CURRENT_VARIANT)"))
274-
table.push(namespace.lookupMacroDeclaration("USE_HEADERMAP") as! BooleanMacroDeclaration, literal: true)
275-
table.push(namespace.lookupMacroDeclaration("HEADERMAP_USES_VFS") as! BooleanMacroDeclaration, literal: true)
276-
table.push(namespace.lookupMacroDeclaration("CLANG_ENABLE_MODULES") as! BooleanMacroDeclaration, literal: true)
273+
table.push(try #require(namespace.lookupMacroDeclaration("PER_VARIANT_CFLAGS") as? StringListMacroDeclaration), BuiltinMacros.namespace.parseStringList("$(OTHER_CFLAGS_$(CURRENT_VARIANT)"))
274+
table.push(try #require(namespace.lookupMacroDeclaration("USE_HEADERMAP") as? BooleanMacroDeclaration), literal: true)
275+
table.push(try #require(namespace.lookupMacroDeclaration("HEADERMAP_USES_VFS") as? BooleanMacroDeclaration), literal: true)
276+
table.push(try #require(namespace.lookupMacroDeclaration("CLANG_ENABLE_MODULES") as? BooleanMacroDeclaration), literal: true)
277277
table.push(try namespace.declareStringListMacro("GLOBAL_CFLAGS"), literal: ["-DFrom_GLOBAL_CFLAGS"])
278-
table.push(namespace.lookupMacroDeclaration("GCC_GENERATE_PROFILING_CODE") as! BooleanMacroDeclaration, literal: true)
279-
table.push(namespace.lookupMacroDeclaration("GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS") as! StringListMacroDeclaration, literal: ["defn1", "defn2"])
280-
table.push(namespace.lookupMacroDeclaration("GCC_OTHER_CFLAGS_NOT_USED_IN_PRECOMPS") as! StringListMacroDeclaration, literal: ["-DFrom_GCC_OTHER_CFLAGS_NOT_USED_IN_PRECOMPS"])
278+
table.push(try #require(namespace.lookupMacroDeclaration("GCC_GENERATE_PROFILING_CODE") as? BooleanMacroDeclaration), literal: true)
279+
table.push(try #require(namespace.lookupMacroDeclaration("GCC_PREPROCESSOR_DEFINITIONS_NOT_USED_IN_PRECOMPS") as? StringListMacroDeclaration), literal: ["defn1", "defn2"])
280+
table.push(try #require(namespace.lookupMacroDeclaration("GCC_OTHER_CFLAGS_NOT_USED_IN_PRECOMPS") as? StringListMacroDeclaration), literal: ["-DFrom_GCC_OTHER_CFLAGS_NOT_USED_IN_PRECOMPS"])
281281

282282
// Override CLANG_DEBUG_MODULES, to be independent of recent changes to the Clang.xcspec.
283-
table.push(namespace.lookupMacroDeclaration("CLANG_DEBUG_MODULES") as! BooleanMacroDeclaration, literal: false)
283+
table.push(try #require(namespace.lookupMacroDeclaration("CLANG_DEBUG_MODULES") as? BooleanMacroDeclaration), literal: false)
284284

285285
let producer = try MockCommandProducer(core: core, productTypeIdentifier: "com.apple.product-type.framework", platform: nil, fs: PseudoFS())
286286

Tests/SWBCorePerfTests/FilePathResolverPerfTests.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
5151
@Test
5252
func projectRelativeSourceTree_X100000() async throws {
5353
let model = try TestGroup("SomeProject", path: "", sourceTree: .buildSetting("PROJECT_DIR")).toProtocol()
54-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
54+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
5555

5656
await measure {
5757
// We do each iteration many times in order to samples that are more likely to be statistically significant.
@@ -70,7 +70,7 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
7070
@Test
7171
func projectRelativeSourceTree_Cached_X100000() async throws {
7272
let model = try TestGroup("SomeProject", sourceTree: .buildSetting("PROJECT_DIR")).toProtocol()
73-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
73+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
7474

7575
await measure {
7676
// We do each iteration many times in order to samples that are more likely to be statistically significant.
@@ -86,7 +86,7 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
8686
@Test
8787
func absoluteSourceTree_X100000() async throws {
8888
let model = try TestGroup("SomeProject", path: "/tmp/AbsolutePath", sourceTree: .absolute).toProtocol()
89-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
89+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
9090

9191
await measure {
9292
// We do each iteration many times in order to samples that are more likely to be statistically significant.
@@ -106,8 +106,8 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
106106
func groupRelativeSourceTree_1Level_X100000() async throws {
107107
let model = try TestGroup("SomeProject", path: "/tmp/SomeProject", sourceTree: .absolute,
108108
children: [TestGroup("AllFiles")]).toProtocol()
109-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
110-
let childGroup = rootGroup.children[0] as! FileGroup
109+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
110+
let childGroup = try #require(rootGroup.children[0] as? FileGroup)
111111

112112
await measure {
113113
// We do each iteration many times in order to samples that are more likely to be statistically significant.
@@ -128,9 +128,9 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
128128
let model = try TestGroup("SomeProject", path: "/tmp/SomeProject", sourceTree: .absolute,
129129
children: [TestGroup("AllFiles",
130130
children: [TestGroup("SomeFiles")])]).toProtocol()
131-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
132-
let childGroup = rootGroup.children[0] as! FileGroup
133-
let grandchildGroup = childGroup.children[0] as! FileGroup
131+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
132+
let childGroup = try #require(rootGroup.children[0] as? FileGroup)
133+
let grandchildGroup = try #require(childGroup.children[0] as? FileGroup)
134134

135135
await measure {
136136
// We do each iteration many times in order to samples that are more likely to be statistically significant.
@@ -151,9 +151,9 @@ fileprivate struct FilePathResolverPerfTests: PerfTests {
151151
let model = try TestGroup("SomeProject", path: "/tmp/SomeProject", sourceTree: .absolute,
152152
children: [TestGroup("AllFiles",
153153
children: [TestGroup("SomeFiles")])]).toProtocol()
154-
let rootGroup = try Reference.create(model, pifLoader) as! FileGroup
155-
let childGroup = rootGroup.children[0] as! FileGroup
156-
let grandchildGroup = childGroup.children[0] as! FileGroup
154+
let rootGroup = try #require(Reference.create(model, pifLoader) as? FileGroup)
155+
let childGroup = try #require(rootGroup.children[0] as? FileGroup)
156+
let grandchildGroup = try #require(childGroup.children[0] as? FileGroup)
157157

158158
await measure {
159159
// We do each iteration many times in order to samples that are more likely to be statistically significant.

0 commit comments

Comments
 (0)