Skip to content

Commit 12ba04b

Browse files
committed
Restore compilation on Linux by fixing tests
1 parent 8e3b8a4 commit 12ba04b

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

Sources/CornucopiaCore/Types/RollingTimestamp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extension Cornucopia.Core {
4141
#if canImport(ObjectiveC)
4242
return string as String
4343
#else
44-
return String(string)
44+
return String(describing: string)
4545
#endif
4646
}
4747
}

Tests/CornucopiaCoreTests/Extensions/Sequence/SequenceUniqueTests.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Cornucopia – (C) Dr. Lauer Information Technology
33
//
44
import XCTest
5+
#if canImport(Darwin)
6+
import Darwin
7+
#endif
58
@testable import CornucopiaCore
69

710
final class SequenceUniqueTests: XCTestCase {
@@ -262,10 +265,9 @@ final class SequenceUniqueTests: XCTestCase {
262265
// MARK: - Helper Methods
263266

264267
private func getMemoryUsage() -> Int {
268+
#if canImport(Darwin)
265269
var info = mach_task_basic_info()
266-
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4
267-
268-
#if canImport(ObjectiveC)
270+
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 4
269271
let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
270272
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
271273
task_info(mach_task_self_,
@@ -274,15 +276,11 @@ final class SequenceUniqueTests: XCTestCase {
274276
&count)
275277
}
276278
}
277-
278-
if kerr == KERN_SUCCESS {
279-
return Int(info.resident_size)
280-
} else {
281-
return 0
282-
}
283-
#else
279+
280+
return kerr == KERN_SUCCESS ? Int(info.resident_size) : 0
281+
#else
284282
return 0
285-
#endif
283+
#endif
286284
}
287285

288286
static var allTests = [

Tests/CornucopiaCoreTests/Features/CacheTests.swift

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Cornucopia – (C) Dr. Lauer Information Technology
33
//
44
import XCTest
5+
#if canImport(FoundationNetworking)
6+
import FoundationNetworking
7+
#endif
58
@testable import CornucopiaCore
69

710
final class CacheTests: XCTestCase {
@@ -329,7 +332,18 @@ final class CacheTests: XCTestCase {
329332
func testCacheWithSpecialCharacters() {
330333
let expectation = XCTestExpectation(description: "Special characters in URL")
331334

332-
let specialCharsURL = URL(string: "https://example.com/path with spaces/special?param=value&other=123")!
335+
var components = URLComponents()
336+
components.scheme = "https"
337+
components.host = "example.com"
338+
components.path = "/path with spaces/special"
339+
components.queryItems = [
340+
URLQueryItem(name: "param", value: "value"),
341+
URLQueryItem(name: "other", value: "123"),
342+
]
343+
guard let specialCharsURL = components.url else {
344+
XCTFail("Failed to build URL with special characters")
345+
return
346+
}
333347
cache.loadDataFor(url: specialCharsURL) { data in
334348
XCTAssertNil(data)
335349
expectation.fulfill()
@@ -342,18 +356,32 @@ final class CacheTests: XCTestCase {
342356

343357
func testMemoryLeaks() {
344358
weak var weakCache: Cornucopia.Core.Cache?
345-
359+
360+
#if canImport(ObjectiveC)
346361
autoreleasepool {
347362
let tempCache = Cornucopia.Core.Cache(name: "TempCache")
348363
weakCache = tempCache
349-
364+
365+
// Use the cache
366+
let expectation = XCTestExpectation(description: "Memory leak test")
367+
tempCache.loadDataFor(url: testURL) { _ in
368+
expectation.fulfill()
369+
}
370+
wait(for: [expectation], timeout: 5.0)
371+
}
372+
#else
373+
do {
374+
let tempCache = Cornucopia.Core.Cache(name: "TempCache")
375+
weakCache = tempCache
376+
350377
// Use the cache
351378
let expectation = XCTestExpectation(description: "Memory leak test")
352379
tempCache.loadDataFor(url: testURL) { _ in
353380
expectation.fulfill()
354381
}
355382
wait(for: [expectation], timeout: 5.0)
356383
}
384+
#endif
357385

358386
// Cache should be deallocated
359387
XCTAssertNil(weakCache, "Cache should be deallocated")

Tests/CornucopiaCoreTests/Features/SpinnerTests.swift

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
//
22
// Cornucopia – (C) Dr. Lauer Information Technology
33
//
4+
import Foundation
45
import XCTest
56
@testable import CornucopiaCore
67

78
final class SpinnerTests: XCTestCase {
89

10+
private final class OutputCollector: @unchecked Sendable {
11+
private var items: [String] = []
12+
private let lock = NSLock()
13+
14+
func append(_ value: String) {
15+
self.lock.lock()
16+
self.items.append(value)
17+
self.lock.unlock()
18+
}
19+
20+
func snapshot() -> [String] {
21+
self.lock.lock()
22+
let snapshot = self.items
23+
self.lock.unlock()
24+
return snapshot
25+
}
26+
}
27+
928
func testSpinnerRendersAndStopsOnSuccess() async {
10-
var output: [String] = []
29+
let output = OutputCollector()
1130
let spinner = Cornucopia.Core.Spinner("Working", style: .line) { text, terminator in
1231
output.append(text + terminator)
1332
}
@@ -16,13 +35,14 @@ final class SpinnerTests: XCTestCase {
1635
try? await Task.sleep(for: .milliseconds(120))
1736
await spinner.stop(success: true)
1837

19-
XCTAssertTrue(output.contains { $0.contains("✓ Working") }, "Expected success indicator in output")
20-
XCTAssertTrue(output.contains { $0.contains("- Working") || $0.contains("\\ Working") || $0.contains("| Working") || $0.contains("/ Working") }, "Expected at least one rendered spinner frame")
38+
let outputSnapshot = output.snapshot()
39+
XCTAssertTrue(outputSnapshot.contains { $0.contains("✓ Working") }, "Expected success indicator in output")
40+
XCTAssertTrue(outputSnapshot.contains { $0.contains("- Working") || $0.contains("\\ Working") || $0.contains("| Working") || $0.contains("/ Working") }, "Expected at least one rendered spinner frame")
2141
}
2242

2343
func testSpinnerRunPropagatesErrorsAndSignalsFailure() async {
2444
enum SpinnerFailure: Error { case boom }
25-
var output: [String] = []
45+
let output = OutputCollector()
2646

2747
do {
2848
_ = try await Cornucopia.Core.Spinner.run("Failing", style: .dots, output: { text, terminator in
@@ -35,6 +55,7 @@ final class SpinnerTests: XCTestCase {
3555
XCTAssertTrue(error is SpinnerFailure)
3656
}
3757

38-
XCTAssertTrue(output.contains { $0.contains("✗ Failing") }, "Expected failure indicator in output")
58+
let outputSnapshot = output.snapshot()
59+
XCTAssertTrue(outputSnapshot.contains { $0.contains("✗ Failing") }, "Expected failure indicator in output")
3960
}
4061
}

Tests/CornucopiaCoreTests/Logging/LogFile.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,10 @@ class Logging: XCTestCase {
108108

109109
XCTAssertEqual(5, differencesInMs.count)
110110
let expectedDifferences = [100, 5.0, 50.0, 500.0, 1000.0]
111+
let toleranceMs = 1.0
111112
for (index, difference) in differencesInMs.enumerated() {
112113
let expectedDifference = expectedDifferences[index]
113-
let lowerBound = expectedDifference
114+
let lowerBound = max(0.0, expectedDifference - toleranceMs)
114115
let upperBound = expectedDifference * 1.6
115116
XCTAssert(difference >= lowerBound && difference <= upperBound)
116117
}

0 commit comments

Comments
 (0)