Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/SwiftDependencyUpdaterLibrary/ResolvedPackage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import Releases
import ShellOut

struct ResolvedVersion: Decodable {

Expand Down Expand Up @@ -50,6 +51,7 @@ private struct Wrapper: Decodable {
}

enum ResolvedPackageError: Error, Equatable {
case resolvingFailed(String)
case readingFailed(String)
case parsingFailed(String, String)
}
Expand All @@ -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 {
Expand All @@ -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):
Expand Down
31 changes: 11 additions & 20 deletions Tests/SwiftDependencyUpdaterLibraryTests/ResolvedPackageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
}
Expand Down