Skip to content

Commit 37e9a3a

Browse files
authored
Merge pull request #48 from Cleverlance/feature/tests-output
Add formating to Xcode console output
2 parents cfa9f3e + e6e8e4d commit 37e9a3a

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

Package.swift

+2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ let package = Package(
1212
.package(url: "https://github.com/Swinject/Swinject.git", from: "2.8.1"),
1313
.package(url: "https://github.com/Swinject/SwinjectAutoregistration", from: "2.8.1"),
1414
.package(url: "https://github.com/jakeheis/SwiftCLI", exact: "6.0.3"),
15+
.package(url: "https://github.com/raptorxcz/xcbeautify", revision: "1bd38b6a64bb447c264fbdfc1b453bf8065b8ec6"),
1516
],
1617
targets: [
1718
.target(name: "SwiftTools", dependencies: [
1819
"Swinject",
1920
"SwinjectAutoregistration",
2021
"SwiftCLI",
22+
.product(name: "XcbeautifyLib", package: "xcbeautify"),
2123
]),
2224
.testTarget(
2325
name: "SwiftToolsTests",

Sources/SwiftTools/Build/Domain/BuildInteractor.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class BuildInteractorImpl: BuildInteractor {
4747

4848
func test(with arguments: TestArguments) throws {
4949
let arguments = try makeArguments(from: arguments)
50-
try shellService.execute(arguments: arguments)
50+
try shellService.executeWithXCBeautify(arguments: arguments)
5151
}
5252

5353
func testWithLog(with arguments: TestArguments) throws -> String {

Sources/SwiftTools/Common/Shell/Platform/ShellService.swift

+31
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
//
77

88
import SwiftCLI
9+
import XcbeautifyLib
910

1011
public protocol ShellService {
1112
func execute(arguments: [String]) throws
1213
func executeWithResult(arguments: [String]) throws -> String
14+
func executeWithXCBeautify(arguments: [String]) throws
1315
}
1416

1517
final class ShellServiceImpl: ShellService {
@@ -58,4 +60,33 @@ final class ShellServiceImpl: ShellService {
5860
return captureStream
5961
}
6062
}
63+
64+
func executeWithXCBeautify(arguments: [String]) throws {
65+
let printStream = WriteStream.stdout
66+
let parser = XcbeautifyLib.Parser(
67+
colored: true,
68+
renderer: .terminal,
69+
preserveUnbeautifiedLines: true,
70+
additionalLines: { nil }
71+
)
72+
let outputStream = makeBeautifyStream(outputStream: printStream, parser: parser)
73+
74+
let command = arguments.joined(separator: " ")
75+
let task = Task(executable: "/bin/bash", arguments: ["-c", command], stdout: outputStream)
76+
let exitCode = task.runSync()
77+
78+
guard exitCode == 0 else {
79+
throw ToolsError(description: "shell command: '\(command)' failed with error")
80+
}
81+
}
82+
83+
private func makeBeautifyStream(outputStream: WritableStream, parser: XcbeautifyLib.Parser) -> ProcessingStream {
84+
return LineStream { line in
85+
if let formatted = parser.parse(line: line) {
86+
outputStream.write(formatted)
87+
} else {
88+
outputStream.write(line)
89+
}
90+
}
91+
}
6192
}

Tests/SwiftToolsTests/Build/Domain/BuildInteractorTests.swift

+19-12
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,21 @@ final class BuildInteractorTests: XCTestCase {
6262

6363
try sut.test(with: argumetns)
6464

65-
XCTAssertEqual(shellServiceSpy.execute.count, 1)
66-
XCTAssertEqual(shellServiceSpy.execute.last?.arguments[safe: 4], "\"platform=iOS Simulator,id=40164398-DEA8-4D73-8813-CF7B2AC49090\"")
65+
XCTAssertEqual(shellServiceSpy.executeWithXCBeautify.count, 1)
66+
XCTAssertEqual(shellServiceSpy.executeWithXCBeautify.last?.arguments[safe: 4], "\"platform=iOS Simulator,id=40164398-DEA8-4D73-8813-CF7B2AC49090\"")
6767
}
6868

6969
func test_givenBuildDestinationsAndmacOSPlatform_whenTest_thenParseCorrectDestination() throws {
7070
let argumetns = TestArguments(scheme: "scheme", platform: .macOS)
7171

7272
try sut.test(with: argumetns)
7373

74-
XCTAssertEqual(shellServiceSpy.execute.count, 1)
75-
XCTAssertEqual(shellServiceSpy.execute.last?.arguments[safe: 4], "\"platform=macOS,variant=Mac Catalyst,id=B717F26A-6C7A-5DE6-A1D0-9D0374071FD0\"")
74+
XCTAssertEqual(shellServiceSpy.executeWithXCBeautify.count, 1)
75+
XCTAssertEqual(shellServiceSpy.executeWithXCBeautify.last?.arguments[safe: 4], "\"platform=macOS,variant=Mac Catalyst,id=B717F26A-6C7A-5DE6-A1D0-9D0374071FD0\"")
7676
}
7777
}
7878

7979
final class ShellServiceSpy: ShellService {
80-
enum SpyError: Error {
81-
case spyError
82-
}
83-
typealias ThrowBlock = () throws -> Void
84-
8580
struct Execute {
8681
let arguments: [String]
8782
}
@@ -90,11 +85,17 @@ final class ShellServiceSpy: ShellService {
9085
let arguments: [String]
9186
}
9287

88+
struct ExecuteWithXCBeautify {
89+
let arguments: [String]
90+
}
91+
92+
var executeThrowBlock: (() throws -> Void)?
93+
var executeWithResultThrowBlock: (() throws -> Void)?
94+
var executeWithResultReturn: String
95+
var executeWithXCBeautifyThrowBlock: (() throws -> Void)?
9396
var execute = [Execute]()
94-
var executeThrowBlock: ThrowBlock?
9597
var executeWithResult = [ExecuteWithResult]()
96-
var executeWithResultThrowBlock: ThrowBlock?
97-
var executeWithResultReturn: String
98+
var executeWithXCBeautify = [ExecuteWithXCBeautify]()
9899

99100
init(executeWithResultReturn: String) {
100101
self.executeWithResultReturn = executeWithResultReturn
@@ -112,6 +113,12 @@ final class ShellServiceSpy: ShellService {
112113
try executeWithResultThrowBlock?()
113114
return executeWithResultReturn
114115
}
116+
117+
func executeWithXCBeautify(arguments: [String]) throws {
118+
let item = ExecuteWithXCBeautify(arguments: arguments)
119+
executeWithXCBeautify.append(item)
120+
try executeWithXCBeautifyThrowBlock?()
121+
}
115122
}
116123

117124
final class PrintServiceSpy: PrintService {

0 commit comments

Comments
 (0)