From aa0f5834f1da2c296af34bbdb21cb70804c479c3 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Sun, 30 Apr 2023 12:39:26 -0700 Subject: [PATCH] Tests: prefer to use `first` rather than indexing 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. --- Tests/SPMBuildCoreTests/PluginInvocationTests.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/SPMBuildCoreTests/PluginInvocationTests.swift b/Tests/SPMBuildCoreTests/PluginInvocationTests.swift index e8246cb6710..c14fc027e2a 100644 --- a/Tests/SPMBuildCoreTests/PluginInvocationTests.swift +++ b/Tests/SPMBuildCoreTests/PluginInvocationTests.swift @@ -299,7 +299,7 @@ class PluginInvocationTests: XCTestCase { XCTAssert(packageGraph.packages.count == 1, "\(packageGraph.packages)") // Find the build tool plugin. - let buildToolPlugin = try XCTUnwrap(packageGraph.packages[0].targets.map(\.underlyingTarget).first{ $0.name == "MyPlugin" } as? PluginTarget) + let buildToolPlugin = try XCTUnwrap(packageGraph.packages.first?.targets.map(\.underlyingTarget).first{ $0.name == "MyPlugin" } as? PluginTarget) XCTAssertEqual(buildToolPlugin.name, "MyPlugin") XCTAssertEqual(buildToolPlugin.capability, .buildTool) @@ -873,7 +873,7 @@ class PluginInvocationTests: XCTestCase { XCTAssert(packageGraph.packages.count == 1, "\(packageGraph.packages)") // Find the build tool plugin. - let buildToolPlugin = try XCTUnwrap(packageGraph.packages[0].targets.map(\.underlyingTarget).filter{ $0.name == "X" }.first as? PluginTarget) + let buildToolPlugin = try XCTUnwrap(packageGraph.packages.first?.targets.map(\.underlyingTarget).filter{ $0.name == "X" }.first as? PluginTarget) XCTAssertEqual(buildToolPlugin.name, "X") XCTAssertEqual(buildToolPlugin.capability, .buildTool) @@ -1203,7 +1203,7 @@ class PluginInvocationTests: XCTestCase { XCTAssertNoDiagnostics(observability.diagnostics) // Find the build tool plugin. - let buildToolPlugin = try XCTUnwrap(packageGraph.packages[0].targets + let buildToolPlugin = try XCTUnwrap(packageGraph.packages.first?.targets .map(\.underlyingTarget) .filter { $0.name == "Foo" } .first as? PluginTarget)