Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing without file usage #27

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let formatterTestsTargetName = formatterTargetName + "Tests"
let package = Package(
name: packageName,
platforms: [
.macOS(.v10_15)
.macOS(.v13)
],
products: [
.library(
Expand Down
52 changes: 0 additions & 52 deletions Sources/DBXCResultParser/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,4 @@ import Foundation

struct Constants {
private static let packageName = "DBXCResultParser"

private static var cachesDirectory: URL {
get throws {
guard let cachesDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
throw Error.noCachesDirectory
}

return cachesDirectory
}
}

private static var appCachesDirectory: URL {
get throws {
let appCachesDirectory = try cachesDirectory.appendingPathComponent(packageName, isDirectory: true)
if !FileManager.default.fileExists(atPath: appCachesDirectory.path) {
try FileManager.default.createDirectory(at: appCachesDirectory,
withIntermediateDirectories: true,
attributes: nil)
}
return appCachesDirectory
}
}

static var actionsInvocationRecord: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionsInvocationRecord", isDirectory: false)
.appendingPathExtension("json")
}
}

static var actionTestPlanRunSummaries: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionTestPlanRunSummaries", isDirectory: false)
.appendingPathExtension("json")
}
}

static var actionTestSummary: URL {
get throws {
try appCachesDirectory
.appendingPathComponent("ActionTestSummary", isDirectory: false)
.appendingPathExtension("json")
}
}
}

extension Constants {
enum Error: Swift.Error {
case noCachesDirectory
}
}
42 changes: 26 additions & 16 deletions Sources/DBXCResultParser/Models/DTO/DTO+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,52 @@ import Foundation

extension ActionsInvocationRecordDTO {
init(from xcresultPath: URL) throws {
let filePath = try Constants.actionsInvocationRecord
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell
.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json")
.trimmingPrefix { $0 != "{" } // xcodebuild may produce unwanted output prior to json
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionsInvocationRecordDTO.self, from: data)
}
}

extension ActionTestPlanRunSummariesDTO {
init(from xcresultPath: URL, refId: String? = nil) throws {
let refId = try (refId ?? ActionsInvocationRecordDTO(from: xcresultPath).testsRefId)
let filePath = try Constants.actionTestPlanRunSummaries
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell
.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId)")
.trimmingPrefix { $0 != "{" } // xcodebuild may produce unwanted output prior to json
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionTestPlanRunSummariesDTO.self, from: data)
}
}

extension ActionTestSummaryDTO {
init(from xcresultPath: URL, refId: String? = nil) throws {
let refId = try (refId ?? ActionsInvocationRecordDTO(from: xcresultPath).testsRefId)
let filePath = try Constants.actionTestSummary
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(filePath.relativePath)")
let data = try Data(contentsOf: filePath)
try FileManager.default.removeItem(atPath: filePath.relativePath)
let result = try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId)")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(ActionTestSummaryDTO.self, from: data)
}
}

extension Array where Element == CoverageDTO {
init(from xcresultPath: URL) throws {
let tempFilePath = try Constants.actionsInvocationRecord
try DBShell.execute("xcrun xccov view --report --only-targets --json \(xcresultPath.relativePath) > \(tempFilePath.relativePath)")
let data = try Data(contentsOf: tempFilePath)
try FileManager.default.removeItem(atPath: tempFilePath.relativePath)
let result = try DBShell.execute("xcrun xccov view --report --only-targets --json \(xcresultPath.relativePath)")
let data = try result.data(using: .utf8) ?! UnwrapError.valueIsNil
self = try JSONDecoder().decode(Array<CoverageDTO>.self, from: data)
}
}

infix operator ?!: NilCoalescingPrecedence

/// Throws the right hand side error if the left hand side optional is `nil`.
func ?!<T>(value: T?, error: @autoclosure () -> Error) throws -> T {
guard let value = value else {
throw error()
}
return value
}

enum UnwrapError: Swift.Error {
case valueIsNil
}
Loading