Skip to content

Commit

Permalink
Fix tests when not running from within Xcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Sep 11, 2021
1 parent f778aed commit 3a7c590
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ let package = Package(
dependencies: [
"Core",
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "Rainbow", package: "Rainbow"),
]
),
.testTarget(name: "CoreTests", dependencies: ["Core", "TestSupport"]),
Expand Down
26 changes: 22 additions & 4 deletions Sources/Core/AutoCorrection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public struct AutoCorrection: Codable, Equatable {
for difference in afterLines.difference(from: beforeLines).sorted() {
switch difference {
case let .insert(offset, element, _):
lines.append("+ [L\(offset + 1)] \(element)".green)
lines.append("+ [L\(offset + 1)] \(element)".coloredAsAdded)

case let .remove(offset, element, _):
lines.append("- [L\(offset + 1)] \(element)".red)
lines.append("- [L\(offset + 1)] \(element)".coloredAsRemoved)
}
}

Expand All @@ -37,8 +37,8 @@ public struct AutoCorrection: Codable, Equatable {
else {
return [
"Autocorrection applied, the diff is: (+ added, - removed)",
"- \(before.showWhitespacesAndNewlines())".red,
"+ \(after.showWhitespacesAndNewlines())".green,
"- \(before.showWhitespacesAndNewlines())".coloredAsRemoved,
"+ \(after.showWhitespacesAndNewlines())".coloredAsAdded,
]
}
}
Expand Down Expand Up @@ -86,3 +86,21 @@ extension CollectionDifference.Change: Comparable where ChangeElement == String
}
}
}

fileprivate extension String {
var coloredAsAdded: String {
#if DEBUG
self // do not color when running tests
#else
green
#endif
}

var coloredAsRemoved: String {
#if DEBUG
self // do not color when running tests
#else
red
#endif
}
}
5 changes: 3 additions & 2 deletions Sources/TestSupport/TestLogger.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import Core
import Rainbow

public final class TestLogger: Loggable {
public var loggedMessages: [String]
Expand All @@ -12,12 +13,12 @@ public final class TestLogger: Loggable {
public func message(_ message: String, level: PrintLevel, location: Location?) {
if let location = location {
loggedMessages.append(
"[\(level.rawValue)] \(location.locationMessage(pathType: .relative)) \(message)"
"[\(level.rawValue)] \(location.locationMessage(pathType: .relative)) \(message.clearColor.clearStyles)"
)
}
else {
loggedMessages.append(
"[\(level.rawValue)] \(message)"
"[\(level.rawValue)] \(message.clearColor.clearStyles)"
)
}
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/CheckersTests/LintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ final class LintTests: XCTestCase {
check: .init(id: "1", hint: "hint #1"),
regex: .init(#"(let|var) (\w+)=(\w+)"#),
matchingExamples: ["let x=4"],
includeFilters: [try! Regex(#"\#(tempDir)/*"#)],
autoCorrectReplacement: "$1 $2 = $3",
autoCorrectExamples: [.init(before: "let x=4", after: "let x = 4")]
)
Expand Down Expand Up @@ -306,7 +307,7 @@ final class LintTests: XCTestCase {
func testCheckFilePaths() throws {
let violations = try Lint.checkFilePaths(
check: .init(id: "2", hint: "hint for #2", severity: .warning),
regex: .init(#"README\.md"#),
regex: .init(#"README\.markdown"#),
violateIfNoMatchesFound: true
)

Expand Down

0 comments on commit 3a7c590

Please sign in to comment.