Skip to content

Commit 785709b

Browse files
authored
Fix broken Windows tests (#350) (#386)
The test runners for Windows were failing to load the paths from disk for test vectors. This patch cleans up the loading logic. (cherry picked from commit 1f483f4)
1 parent e8d6eba commit 785709b

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

Tests/CryptoTests/Utils/RFCVector.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,22 @@ struct RFCVectorDecoder {
2929
let bundle = Bundle(for: type(of: bundleType))
3030
let fileURL = bundle.url(forResource: fileName, withExtension: "txt")
3131
#else
32-
let testsDirectory: String = URL(fileURLWithPath: "\(#file)").pathComponents.dropLast(3).joined(separator: "/")
33-
let fileURL: URL? = URL(fileURLWithPath: "\(testsDirectory)/Test Vectors/\(fileName).txt")
32+
var fileURL: URL? = URL(fileURLWithPath: "\(#file)")
33+
for _ in 0..<3 {
34+
fileURL!.deleteLastPathComponent()
35+
}
36+
#if compiler(>=6.0)
37+
if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1, macCatalyst 16, *) {
38+
fileURL!.append(path: "Test Vectors", directoryHint: .isDirectory)
39+
fileURL!.append(path: "\(fileName).txt", directoryHint: .notDirectory)
40+
} else {
41+
fileURL! = fileURL!.appendingPathComponent("Test Vectors", isDirectory: true)
42+
fileURL! = fileURL!.appendingPathComponent("\(fileName).txt", isDirectory: false)
43+
}
44+
#else
45+
fileURL! = fileURL!.appendingPathComponent("Test Vectors", isDirectory: true)
46+
fileURL! = fileURL!.appendingPathComponent("\(fileName).txt", isDirectory: false)
47+
#endif
3448
#endif
3549

3650
let rfcVectorData = try Data(contentsOf: fileURL!)

Tests/CryptoTests/Utils/Wycheproof.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ extension XCTestCase {
2525
let bundle = Bundle(for: type(of: bundleType))
2626
let fileURL = bundle.url(forResource: jsonName, withExtension: "json")
2727
#else
28-
let testsDirectory: String = URL(fileURLWithPath: "\(#file)").pathComponents.dropLast(3).joined(separator: "/")
29-
let fileURL: URL? = URL(fileURLWithPath: "\(testsDirectory)/Test Vectors/\(jsonName).json")
28+
var fileURL = URL(fileURLWithPath: "\(#file)")
29+
for _ in 0..<3 {
30+
fileURL.deleteLastPathComponent()
31+
}
32+
#if compiler(>=6.0)
33+
if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1, macCatalyst 16, *) {
34+
fileURL.append(path: "Test Vectors", directoryHint: .isDirectory)
35+
fileURL.append(path: "\(jsonName).json", directoryHint: .notDirectory)
36+
} else {
37+
fileURL = fileURL.appendingPathComponent("Test Vectors", isDirectory: true)
38+
fileURL = fileURL.appendingPathComponent("\(jsonName).json", isDirectory: false)
39+
}
40+
#else
41+
fileURL = fileURL.appendingPathComponent("Test Vectors", isDirectory: true)
42+
fileURL = fileURL.appendingPathComponent("\(jsonName).json", isDirectory: false)
43+
#endif
3044
#endif
3145

3246
let data = try orFail(file: file, line: line) { try Data(contentsOf: unwrap(fileURL, file: file, line: line)) }

Tests/_CryptoExtrasTests/Utils/RFCVector.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,22 @@ struct RFCVectorDecoder {
2525
private var index: Int?
2626

2727
init(bundleType: AnyObject, fileName: String) throws {
28-
let testsDirectory: String = URL(fileURLWithPath: "\(#file)").pathComponents.dropLast(3).joined(separator: "/")
29-
let fileURL: URL? = URL(fileURLWithPath: "\(testsDirectory)/_CryptoExtrasVectors/\(fileName).txt")
28+
var fileURL: URL? = URL(fileURLWithPath: "\(#file)")
29+
for _ in 0..<3 {
30+
fileURL!.deleteLastPathComponent()
31+
}
32+
#if compiler(>=6.0)
33+
if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1, macCatalyst 16, *) {
34+
fileURL!.append(path: "_CryptoExtrasVectors", directoryHint: .isDirectory)
35+
fileURL!.append(path: "\(fileName).txt", directoryHint: .notDirectory)
36+
} else {
37+
fileURL! = fileURL!.appendingPathComponent("_CryptoExtrasVectors", isDirectory: true)
38+
fileURL! = fileURL!.appendingPathComponent("\(fileName).txt", isDirectory: false)
39+
}
40+
#else
41+
fileURL! = fileURL!.appendingPathComponent("_CryptoExtrasVectors", isDirectory: true)
42+
fileURL! = fileURL!.appendingPathComponent("\(fileName).txt", isDirectory: false)
43+
#endif
3044

3145
let rfcVectorData = try Data(contentsOf: fileURL!)
3246
self.rfcVectorData = String(decoding: rfcVectorData, as: Unicode.UTF8.self)

Tests/_CryptoExtrasTests/Utils/Wycheproof.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,24 @@ struct WycheproofTest<T: Codable>: Codable {
2121

2222
extension XCTestCase {
2323
func wycheproofTest<T: Codable>(jsonName: String, file: StaticString = #file, line: UInt = #line, testFunction: (T) throws -> Void) throws {
24-
let testsDirectory: String = URL(fileURLWithPath: "\(#file)").pathComponents.dropLast(3).joined(separator: "/")
25-
let fileURL: URL? = URL(fileURLWithPath: "\(testsDirectory)/_CryptoExtrasVectors/\(jsonName).json")
24+
var fileURL = URL(fileURLWithPath: "\(#file)")
25+
for _ in 0..<3 {
26+
fileURL.deleteLastPathComponent()
27+
}
28+
#if compiler(>=6.0)
29+
if #available(macOS 13, iOS 16, watchOS 9, tvOS 16, visionOS 1, macCatalyst 16, *) {
30+
fileURL.append(path: "_CryptoExtrasVectors", directoryHint: .isDirectory)
31+
fileURL.append(path: "\(jsonName).json", directoryHint: .notDirectory)
32+
} else {
33+
fileURL = fileURL.appendingPathComponent("_CryptoExtrasVectors", isDirectory: true)
34+
fileURL = fileURL.appendingPathComponent("\(jsonName).json", isDirectory: false)
35+
}
36+
#else
37+
fileURL = fileURL.appendingPathComponent("_CryptoExtrasVectors", isDirectory: true)
38+
fileURL = fileURL.appendingPathComponent("\(jsonName).json", isDirectory: false)
39+
#endif
2640

27-
let data = try Data(contentsOf: fileURL!)
41+
let data = try Data(contentsOf: fileURL)
2842

2943
let decoder = JSONDecoder()
3044
let wpTest = try decoder.decode(WycheproofTest<T>.self, from: data)

0 commit comments

Comments
 (0)