Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AllDmeat committed Dec 18, 2023
1 parent e76051b commit a8c6632
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 366 deletions.
19 changes: 16 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PackageDescription

let packageName = "DBXCResultParser"
let packageTestsName = packageName + "Tests"
let packageTestHelpersName = packageName + "TestHelpers"

let package = Package(
name: packageName,
Expand All @@ -15,7 +16,12 @@ let package = Package(
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: packageName,
targets: [packageName]),
targets: [packageName]
),
.library(
name: packageTestHelpersName,
targets: [packageTestHelpersName]
),
],
dependencies: [

Expand All @@ -25,13 +31,20 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: packageName,
dependencies: []
),
.target(
name: packageTestHelpersName,
dependencies: [

.init(stringLiteral: packageName)
]
),
.testTarget(
name: packageTestsName,
dependencies: [.init(stringLiteral: packageName)],
dependencies: [
.init(stringLiteral: packageName),
.init(stringLiteral: packageTestHelpersName)
],
resources: [
.process("Resources/AllTests.xcresult"),
.process("Resources/AllTests_coverage.xcresult"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//
// Shell.swift
// DBShell.swift
//
//
// Created by Алексей Берёзка on 28.12.2021.
//

import Foundation

class Shell {
public class DBShell {
@discardableResult
static func execute(_ command: String) throws -> String {
public static func execute(_ command: String) throws -> String {
let task = Process()
let pipe = Pipe()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// TextFormatter.swift
// DBXCTextFormatter.swift
//
//
// Created by Алексей Берёзка on 31.12.2021.
//

import Foundation

extension TextFormatter {
extension DBXCTextFormatter {
public enum Format {
case list
case count
}
}

class TextFormatter: FormatterProtocol {
public class DBXCTextFormatter {
public let format: Format
public let locale: Locale?

Expand All @@ -27,8 +27,8 @@ class TextFormatter: FormatterProtocol {
}

public func format(
_ report: ReportModel,
testResults: [ReportModel.Module.File.RepeatableTest.Test.Status] = .allCases
_ report: DBXCReportModel,
testResults: [DBXCReportModel.Module.File.RepeatableTest.Test.Status] = .allCases
) -> String {
let files = report.modules
.flatMap { Array($0.files) }
Expand Down Expand Up @@ -62,8 +62,8 @@ class TextFormatter: FormatterProtocol {
}
}

extension ReportModel.Module.File {
func report(testResults: [ReportModel.Module.File.RepeatableTest.Test.Status],
extension DBXCReportModel.Module.File {
func report(testResults: [DBXCReportModel.Module.File.RepeatableTest.Test.Status],
formatter: MeasurementFormatter) -> String? {
let tests = repeatableTests.filtered(testResults: testResults).sorted { $0.name < $1.name }

Expand All @@ -83,7 +83,7 @@ extension ReportModel.Module.File {
}
}

fileprivate extension ReportModel.Module.File.RepeatableTest {
fileprivate extension DBXCReportModel.Module.File.RepeatableTest {
func report(formatter: MeasurementFormatter) -> String {
[
combinedStatus.icon,
Expand Down
15 changes: 0 additions & 15 deletions Sources/DBXCResultParser/Formatters/FormatterProtocol.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// ReportModel+Convenience.swift
// DBXCReportModel+Convenience.swift
//
//
// Created by Aleksey Berezka on 15.12.2023.
//

import Foundation

extension ReportModel {
extension DBXCReportModel {
public init(xcresultPath: URL) throws {
let overviewReport = try OverviewReportDTO(from: xcresultPath)
let detailedReport = try DetailedReportDTO(from: xcresultPath,
Expand All @@ -16,7 +16,7 @@ extension ReportModel {
let coverageDTOs = try? Array<CoverageDTO>(from: xcresultPath)
.filter { !$0.name.contains("TestHelpers") && !$0.name.contains("Tests") }

self = try ReportModel(
self = try DBXCReportModel(
overviewReportDTO: overviewReport,
detailedReportDTO: detailedReport,
coverageDTOs: coverageDTOs ?? []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
//
// ReportModel.swift
// DBXCReportModel.swift
//
//
// Created by Алексей Берёзка on 31.12.2021.
//

import Foundation

public typealias Duration = Measurement<UnitDuration>

public struct ReportModel {
public struct DBXCReportModel {
public let modules: Set<Module>
public private(set) var warningCount: Int?
}

extension ReportModel {
extension DBXCReportModel {
public struct Module: Hashable {
public let name: String
public internal(set) var files: Set<File>
Expand All @@ -30,7 +28,7 @@ extension ReportModel {
}
}

extension ReportModel.Module {
extension DBXCReportModel.Module {
public struct Coverage: Equatable {
public let name: String
public let coveredLines: Int
Expand All @@ -56,7 +54,7 @@ extension ReportModel.Module {
}
}

extension ReportModel.Module {
extension DBXCReportModel.Module {
public struct File: Hashable {
public let name: String
public internal(set) var repeatableTests: Set<RepeatableTest>
Expand All @@ -70,7 +68,7 @@ extension ReportModel.Module {
}
}
}
extension ReportModel.Module.File {
extension DBXCReportModel.Module.File {
public struct RepeatableTest: Hashable {
public let name: String
public internal(set) var tests: [Test]
Expand All @@ -85,13 +83,13 @@ extension ReportModel.Module.File {
}
}

extension ReportModel.Module.File.RepeatableTest {
extension DBXCReportModel.Module.File.RepeatableTest {
public struct Test {
public let status: Status
public let duration: Duration
public let duration: Measurement<UnitDuration>
}

var combinedStatus: Test.Status {
public var combinedStatus: Test.Status {
let statuses = tests.map { $0.status }
if statuses.elementsAreEqual {
return statuses.first ?? .success
Expand All @@ -100,7 +98,7 @@ extension ReportModel.Module.File.RepeatableTest {
}
}

var averageDuration: Duration {
public var averageDuration: Measurement<UnitDuration> {
assert(tests.map { $0.duration.unit }.elementsAreEqual)

let unit = tests.first?.duration.unit ?? Test.defaultDurationUnit
Expand All @@ -111,21 +109,21 @@ extension ReportModel.Module.File.RepeatableTest {
)
}

var totalDuration: Duration {
public var totalDuration: Measurement<UnitDuration> {
assert(tests.map { $0.duration.unit }.elementsAreEqual)
let value = tests.map { $0.duration.value }.sum()
let unit = tests.first?.duration.unit ?? Test.defaultDurationUnit
return .init(value: value, unit: unit)
}

func isSlow(_ duration: Duration) -> Bool {
public func isSlow(_ duration: Measurement<UnitDuration>) -> Bool {
let averageDuration = averageDuration
let duration = duration.converted(to: averageDuration.unit)
return averageDuration >= duration
}
}

extension ReportModel.Module.File.RepeatableTest.Test {
extension DBXCReportModel.Module.File.RepeatableTest.Test {
public enum Status: Equatable, CaseIterable {
case success
case failure
Expand All @@ -136,11 +134,11 @@ extension ReportModel.Module.File.RepeatableTest.Test {
}
}

extension Array where Element == ReportModel.Module.File.RepeatableTest.Test.Status {
static let allCases = ReportModel.Module.File.RepeatableTest.Test.Status.allCases
public extension Array where Element == DBXCReportModel.Module.File.RepeatableTest.Test.Status {
static let allCases = DBXCReportModel.Module.File.RepeatableTest.Test.Status.allCases
}

extension ReportModel {
extension DBXCReportModel {
init(overviewReportDTO: OverviewReportDTO,
detailedReportDTO: DetailedReportDTO,
coverageDTOs: [CoverageDTO]) throws {
Expand Down Expand Up @@ -175,7 +173,7 @@ extension ReportModel {
let testname = value6.name._value
var repeatableTest = file.repeatableTests[testname] ?? .init(name: testname,
tests: [])
let test = try ReportModel.Module.File.RepeatableTest.Test(value6)
let test = try DBXCReportModel.Module.File.RepeatableTest.Test(value6)
repeatableTest.tests.append(test)
file.repeatableTests.update(with: repeatableTest)
}
Expand Down Expand Up @@ -203,7 +201,7 @@ extension ReportModel {
}
}

extension ReportModel {
extension DBXCReportModel {
enum Error: Swift.Error {
case missingFilename(testName: String)
}
Expand All @@ -215,8 +213,8 @@ fileprivate extension String {
}
}

extension Set where Element == ReportModel.Module.File.RepeatableTest {
public func filtered(testResults: [ReportModel.Module.File.RepeatableTest.Test.Status]) -> Set<Element> {
extension Set where Element == DBXCReportModel.Module.File.RepeatableTest {
public func filtered(testResults: [DBXCReportModel.Module.File.RepeatableTest.Test.Status]) -> Set<Element> {
guard !testResults.isEmpty else {
return self
}
Expand Down Expand Up @@ -267,7 +265,7 @@ extension Set where Element == ReportModel.Module.File.RepeatableTest {
}
}

extension ReportModel.Module.File.RepeatableTest.Test {
extension DBXCReportModel.Module.File.RepeatableTest.Test {
init(_ test: DetailedReportDTO.Summaries.Value.TestableSummaries.Value.Tests.Value.Subtests.Value.Subtests.Value.Subtests.Value) throws {
switch test.testStatus._value {
case "Success":
Expand Down Expand Up @@ -302,34 +300,34 @@ extension Array where Element: Equatable {
}
}

extension Set where Element == ReportModel.Module.File {
extension Set where Element == DBXCReportModel.Module.File {
subscript(_ name: String) -> Element? {
first { $0.name == name }
}
}

extension Set where Element == ReportModel.Module.File.RepeatableTest {
extension Set where Element == DBXCReportModel.Module.File.RepeatableTest {
subscript(_ name: String) -> Element? {
first { $0.name == name }
}
}

extension Set where Element == ReportModel.Module {
extension Set where Element == DBXCReportModel.Module {
subscript(_ name: String) -> Element? {
first { $0.name == name }
}
}

extension Array where Element == ReportModel.Module.File.RepeatableTest {
var totalDuration: Duration {
extension Array where Element == DBXCReportModel.Module.File.RepeatableTest {
var totalDuration: Measurement<UnitDuration> {
assert(map { $0.totalDuration.unit }.elementsAreEqual)
let value = map { $0.totalDuration.value }.sum()
let unit = first?.totalDuration.unit ?? ReportModel.Module.File.RepeatableTest.Test.defaultDurationUnit
let unit = first?.totalDuration.unit ?? DBXCReportModel.Module.File.RepeatableTest.Test.defaultDurationUnit
return .init(value: value, unit: unit)
}
}

extension ReportModel.Module.File.RepeatableTest.Test.Status {
extension DBXCReportModel.Module.File.RepeatableTest.Test.Status {
var icon: String {
switch self {
case .success:
Expand Down
6 changes: 3 additions & 3 deletions Sources/DBXCResultParser/Models/DTO/DTO+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
extension OverviewReportDTO {
init(from xcresultPath: URL) throws {
let tempFilePath = try Constants.tempFilePath
try Shell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json > \(tempFilePath.relativePath)")
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json > \(tempFilePath.relativePath)")
let data = try Data(contentsOf: tempFilePath)
try FileManager.default.removeItem(atPath: tempFilePath.relativePath)
self = try JSONDecoder().decode(OverviewReportDTO.self, from: data)
Expand All @@ -21,7 +21,7 @@ extension DetailedReportDTO {
init(from xcresultPath: URL, refId: String? = nil) throws {
let refId = try (refId ?? OverviewReportDTO(from: xcresultPath).testsRefId)
let tempFilePath = try Constants.tempFilePath
try Shell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(tempFilePath.relativePath)")
try DBShell.execute("xcrun xcresulttool get --path \(xcresultPath.relativePath) --format json --id \(refId) > \(tempFilePath.relativePath)")
let data = try Data(contentsOf: tempFilePath)
try FileManager.default.removeItem(atPath: tempFilePath.relativePath)
self = try JSONDecoder().decode(DetailedReportDTO.self, from: data)
Expand All @@ -31,7 +31,7 @@ extension DetailedReportDTO {
extension Array where Element == CoverageDTO {
init(from xcresultPath: URL) throws {
let tempFilePath = try Constants.tempFilePath
try Shell.execute("xcrun xccov view --report --only-targets --json \(xcresultPath.relativePath) > \(tempFilePath.relativePath)")
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)
self = try JSONDecoder().decode(Array<CoverageDTO>.self, from: data)
Expand Down
Loading

0 comments on commit a8c6632

Please sign in to comment.