Skip to content

Commit 6905e30

Browse files
authored
Tests: prefer to use first rather than indexing (#6503)
In the case of a failure in the test, we would previously continue to perform the slice operation which will fail in the case of an Asserts runtime due to an out of bounds access. The use of `first?` here would unwrap or fail, allowing tests to continue execution.
1 parent 62633ac commit 6905e30

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Tests/PackageLoadingTests/PD_4_2_LoadingTests.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -596,17 +596,17 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
596596

597597
let manifestLoader = ManifestLoader(toolchain: try UserToolchain.default, cacheDir: path, delegate: delegate)
598598

599-
func check(loader: ManifestLoader, expectCached: Bool) {
599+
func check(loader: ManifestLoader, expectCached: Bool) throws {
600600
delegate.clear()
601601
delegate.prepare(expectParsing: !expectCached)
602602

603-
let manifest = try! loader.load(
603+
let manifest = try XCTUnwrap(loader.load(
604604
manifestPath: manifestPath,
605605
packageKind: .fileSystem(manifestPath.parentDirectory),
606606
toolsVersion: .v4_2,
607607
fileSystem: fs,
608608
observabilityScope: observability.topScope
609-
)
609+
))
610610

611611
XCTAssertNoDiagnostics(observability.diagnostics)
612612
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
@@ -615,20 +615,20 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
615615
XCTAssertEqual(manifest.targets[0].name, "foo")
616616
}
617617

618-
check(loader: manifestLoader, expectCached: false)
619-
check(loader: manifestLoader, expectCached: true)
618+
try check(loader: manifestLoader, expectCached: false)
619+
try check(loader: manifestLoader, expectCached: true)
620620

621621
try withCustomEnv(["SWIFTPM_MANIFEST_CACHE_TEST": "1"]) {
622-
check(loader: manifestLoader, expectCached: false)
623-
check(loader: manifestLoader, expectCached: true)
622+
try check(loader: manifestLoader, expectCached: false)
623+
try check(loader: manifestLoader, expectCached: true)
624624
}
625625

626626
try withCustomEnv(["SWIFTPM_MANIFEST_CACHE_TEST": "2"]) {
627-
check(loader: manifestLoader, expectCached: false)
628-
check(loader: manifestLoader, expectCached: true)
627+
try check(loader: manifestLoader, expectCached: false)
628+
try check(loader: manifestLoader, expectCached: true)
629629
}
630630

631-
check(loader: manifestLoader, expectCached: true)
631+
try check(loader: manifestLoader, expectCached: true)
632632
}
633633
}
634634

@@ -658,17 +658,17 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
658658

659659
let manifestLoader = ManifestLoader(toolchain: try UserToolchain.default, cacheDir: path, delegate: delegate)
660660

661-
func check(loader: ManifestLoader, expectCached: Bool) {
661+
func check(loader: ManifestLoader, expectCached: Bool) throws {
662662
delegate.clear()
663663
delegate.prepare(expectParsing: !expectCached)
664664

665-
let manifest = try! loader.load(
665+
let manifest = try XCTUnwrap(loader.load(
666666
manifestPath: manifestPath,
667667
packageKind: .fileSystem(manifestPath.parentDirectory),
668668
toolsVersion: .v4_2,
669669
fileSystem: fs,
670670
observabilityScope: observability.topScope
671-
)
671+
))
672672

673673
XCTAssertNoDiagnostics(observability.diagnostics)
674674
XCTAssertEqual(try delegate.loaded(timeout: .now() + 1), [manifestPath])
@@ -677,9 +677,9 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
677677
XCTAssertEqual(manifest.targets[0].name, "foo")
678678
}
679679

680-
check(loader: manifestLoader, expectCached: false)
680+
try check(loader: manifestLoader, expectCached: false)
681681
for _ in 0..<2 {
682-
check(loader: manifestLoader, expectCached: true)
682+
try check(loader: manifestLoader, expectCached: true)
683683
}
684684

685685
try fs.writeFileContents(
@@ -700,14 +700,14 @@ class PackageDescription4_2LoadingTests: PackageDescriptionLoadingTests {
700700
"""
701701
)
702702

703-
check(loader: manifestLoader, expectCached: false)
703+
try check(loader: manifestLoader, expectCached: false)
704704
for _ in 0..<2 {
705-
check(loader: manifestLoader, expectCached: true)
705+
try check(loader: manifestLoader, expectCached: true)
706706
}
707707

708708
let noCacheLoader = ManifestLoader(toolchain: try UserToolchain.default, delegate: delegate)
709709
for _ in 0..<2 {
710-
check(loader: noCacheLoader, expectCached: false)
710+
try check(loader: noCacheLoader, expectCached: false)
711711
}
712712

713713
// Resetting the cache should allow us to remove the cache

0 commit comments

Comments
 (0)