Skip to content

Commit e50dfae

Browse files
authored
refactor(editor): remove stdout print on theme load failure (#101)
1 parent cb1c520 commit e50dfae

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

Sources/Zero/Views/CodeEditorView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ class HighlightedTextView: NSTextView {
8282
super.mouseDown(with: event)
8383
}
8484

85-
func setup(language: String) {
85+
func setup(language: String, themeName: String = "xcode") {
8686
self.currentLanguage = language
8787

8888
highlightr = Highlightr()
89-
if highlightr?.setTheme(to: "xcode") == false {
90-
print("Failed to load theme: xcode")
89+
if highlightr?.setTheme(to: themeName) == false {
90+
AppLogStore.shared.append("Failed to load theme: \(themeName)")
9191
}
9292

9393
isEditable = true
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import XCTest
2+
import Darwin
3+
import Foundation
4+
@testable import Zero
5+
6+
@MainActor
7+
final class CodeEditorViewTests: XCTestCase {
8+
override func setUp() {
9+
super.setUp()
10+
AppLogStore.shared.clear()
11+
}
12+
13+
override func tearDown() {
14+
AppLogStore.shared.clear()
15+
super.tearDown()
16+
}
17+
18+
func testSetupWithInvalidThemeDoesNotPrintToStdout() {
19+
let textView = HighlightedTextView(frame: .zero)
20+
21+
let stdout = captureStdout {
22+
textView.setup(language: "swift", themeName: "__invalid_theme__")
23+
}
24+
25+
XCTAssertTrue(stdout.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).isEmpty)
26+
}
27+
28+
func testSetupWithInvalidThemeAppendsFailureToAppLogStore() {
29+
let textView = HighlightedTextView(frame: .zero)
30+
31+
textView.setup(language: "swift", themeName: "__invalid_theme__")
32+
33+
let logEntries = AppLogStore.shared.recentEntries()
34+
XCTAssertTrue(logEntries.contains { entry in
35+
entry.contains("Failed to load theme") && entry.contains("__invalid_theme__")
36+
})
37+
}
38+
39+
private func captureStdout(_ operation: () -> Void) -> String {
40+
fflush(stdout)
41+
42+
let outputPipe = Pipe()
43+
let originalStdout = dup(STDOUT_FILENO)
44+
dup2(outputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
45+
46+
operation()
47+
48+
fflush(stdout)
49+
dup2(originalStdout, STDOUT_FILENO)
50+
close(originalStdout)
51+
52+
outputPipe.fileHandleForWriting.closeFile()
53+
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
54+
outputPipe.fileHandleForReading.closeFile()
55+
56+
return String(data: outputData, encoding: .utf8) ?? ""
57+
}
58+
}

0 commit comments

Comments
 (0)