From 680cf092a4cd27b4c704b72bf5e08134c3eb30b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20K=C3=B6tte?= Date: Sun, 23 May 2021 19:20:57 -0700 Subject: [PATCH] Resolve dependencies at the beginning This allows the tool to run on a repository which was freshly checked out and never run. --- .../ResolvedPackage.swift | 10 ++++++ .../ResolvedPackageTests.swift | 31 +++++++------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/Sources/SwiftDependencyUpdaterLibrary/ResolvedPackage.swift b/Sources/SwiftDependencyUpdaterLibrary/ResolvedPackage.swift index 06cd9c9..3f45612 100644 --- a/Sources/SwiftDependencyUpdaterLibrary/ResolvedPackage.swift +++ b/Sources/SwiftDependencyUpdaterLibrary/ResolvedPackage.swift @@ -1,5 +1,6 @@ import Foundation import Releases +import ShellOut struct ResolvedVersion: Decodable { @@ -50,6 +51,7 @@ private struct Wrapper: Decodable { } enum ResolvedPackageError: Error, Equatable { + case resolvingFailed(String) case readingFailed(String) case parsingFailed(String, String) } @@ -62,6 +64,12 @@ struct ResolvedPackage: Decodable { let dependencies: [ResolvedDependency] static func loadResolvedPackage(from folder: URL) throws -> ResolvedPackage { + do { + try shellOut(to: "swift", arguments: ["package", "resolve", "--package-path", "\"\(folder.path)\"" ]) + } catch { + let error = error as! ShellOutError // swiftlint:disable:this force_cast + throw ResolvedPackageError.resolvingFailed(error.message) + } let data = try readResolvedPackageData(from: folder) let decoder = JSONDecoder() do { @@ -86,6 +94,8 @@ struct ResolvedPackage: Decodable { extension ResolvedPackageError: LocalizedError { public var errorDescription: String? { switch self { + case let .resolvingFailed(error): + return "Running swift package resolved failed: \(error)" case let .readingFailed(error): return "Could not read Package.resolved file: \(error)" case let .parsingFailed(error, packageData): diff --git a/Tests/SwiftDependencyUpdaterLibraryTests/ResolvedPackageTests.swift b/Tests/SwiftDependencyUpdaterLibraryTests/ResolvedPackageTests.swift index 8045ee9..3ed7b2c 100644 --- a/Tests/SwiftDependencyUpdaterLibraryTests/ResolvedPackageTests.swift +++ b/Tests/SwiftDependencyUpdaterLibraryTests/ResolvedPackageTests.swift @@ -6,40 +6,30 @@ class ResolvedPackageTests: XCTestCase { func testEmptyFolder() { let folder = emptyFolderURL() - #if os(Linux) assert( try ResolvedPackage.loadResolvedPackage(from: folder), - throws: ResolvedPackageError.readingFailed("The operation could not be completed. No such file or directory") + throws: ResolvedPackageError.resolvingFailed("error: root manifest not found") ) - #else - assert( - try ResolvedPackage.loadResolvedPackage(from: folder), - throws: ResolvedPackageError.readingFailed("The file “Package.resolved” couldn’t be opened because there is no such file.") - ) - #endif } func testInvalidFile() { let folder = emptyFolderURL() - let file = temporaryFileURL(in: folder, name: "Package.resolved") - createFile(at: file, content: "\n") - #if os(Linux) - assert( - try ResolvedPackage.loadResolvedPackage(from: folder), - throws: ResolvedPackageError.parsingFailed("The operation could not be completed. The data isn’t in the correct format.", "\n") - ) - #else + let resolvedFile = temporaryFileURL(in: folder, name: "Package.resolved") + createFile(at: resolvedFile, content: "\n") + let packageFile = temporaryFileURL(in: folder, name: "Package.swift") + createFile(at: packageFile, content: TestUtils.emptyPackageSwiftFileContent) assert( try ResolvedPackage.loadResolvedPackage(from: folder), - throws: ResolvedPackageError.parsingFailed("The data couldn’t be read because it isn’t in the correct format.", "\n") + throws: ResolvedPackageError.resolvingFailed("error: Package.resolved file is corrupted or malformed; fix or delete the file to continue: malformed") ) - #endif } func testParsing() { let folder = emptyFolderURL() - let file = temporaryFileURL(in: folder, name: "Package.resolved") - createFile(at: file, content: TestUtils.packageResolvedFileContent) + let resolvedFile = temporaryFileURL(in: folder, name: "Package.resolved") + createFile(at: resolvedFile, content: TestUtils.packageResolvedFileContent) + let packageFile = temporaryFileURL(in: folder, name: "Package.swift") + createFile(at: packageFile, content: TestUtils.emptyPackageSwiftFileContent) let result = try! ResolvedPackage.loadResolvedPackage(from: folder) XCTAssertEqual(result.dependencies.count, 3) @@ -95,6 +85,7 @@ class ResolvedPackageTests: XCTestCase { } func testResolvedPackageErrorString() { + XCTAssertEqual("\(ResolvedPackageError.resolvingFailed("abc").localizedDescription)", "Running swift package resolved failed: abc") XCTAssertEqual("\(ResolvedPackageError.readingFailed("abc").localizedDescription)", "Could not read Package.resolved file: abc") XCTAssertEqual("\(ResolvedPackageError.parsingFailed("abc", "def").localizedDescription)", "Could not parse package data: abc\n\nPackage Data: def") }