|
1 | 1 | import Foundation |
2 | 2 |
|
3 | | -public struct FileCreationHarnessCase: Codable, Equatable, Sendable { |
4 | | - public var name: String |
5 | | - public var templateID: String |
6 | | - public var requestedFileName: String? |
7 | | - public var existingFileNames: [String] |
8 | | - public var expectedFileName: String |
9 | | - public var expectedContentContains: [String] |
| 3 | +/// Constructible only through the shared loader. No unchecked array runner exists. |
| 4 | +public struct HarnessSuite: Sendable { |
| 5 | + public static let maximumBytes = 1_048_576 |
| 6 | + public static let maximumDepth = 32 |
| 7 | + public static let maximumCases = 1_000 |
| 8 | + let cases: [HarnessCase] |
| 9 | + public var caseCount: Int { cases.count } |
10 | 10 |
|
11 | | - public init( |
12 | | - name: String, |
13 | | - templateID: String, |
14 | | - requestedFileName: String?, |
15 | | - existingFileNames: [String], |
16 | | - expectedFileName: String, |
17 | | - expectedContentContains: [String] |
18 | | - ) { |
19 | | - self.name = name |
20 | | - self.templateID = templateID |
21 | | - self.requestedFileName = requestedFileName |
22 | | - self.existingFileNames = existingFileNames |
23 | | - self.expectedFileName = expectedFileName |
24 | | - self.expectedContentContains = expectedContentContains |
| 11 | + private init(cases: [HarnessCase]) { self.cases = cases } |
| 12 | + |
| 13 | + public static func load(data: Data) throws -> HarnessSuite { |
| 14 | + guard data.count <= maximumBytes else { throw sizeError } |
| 15 | + var json = try HarnessJSON(data) |
| 16 | + try json.validate() |
| 17 | + let document: HarnessDocument |
| 18 | + do { |
| 19 | + document = try JSONDecoder().decode(HarnessDocument.self, from: data) |
| 20 | + } catch { |
| 21 | + throw HarnessDiagnostic.decoding(error) |
| 22 | + } |
| 23 | + guard !document.cases.isEmpty, document.cases.count <= maximumCases else { |
| 24 | + throw HarnessDiagnostic(code: "caseCount", path: "$.cases", message: "Expected 1...1000 cases") |
| 25 | + } |
| 26 | + var identifiers = Set<String>() |
| 27 | + for (index, item) in document.cases.enumerated() { |
| 28 | + let path = "$.cases[\(index)]" |
| 29 | + guard !item.id.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, |
| 30 | + identifiers.insert(item.id).inserted else { |
| 31 | + throw HarnessDiagnostic(code: "caseID", path: path + ".id", message: "Case IDs must be nonblank and unique") |
| 32 | + } |
| 33 | + guard !item.name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { |
| 34 | + throw HarnessDiagnostic(code: "caseName", path: path + ".name", message: "Case name must not be blank") |
| 35 | + } |
| 36 | + guard TemplateCatalog.template(withID: item.templateID) != nil else { |
| 37 | + throw HarnessDiagnostic(code: "template", path: path + ".templateID", message: "Unknown built-in template") |
| 38 | + } |
| 39 | + try validateFileName(item.expect.fileName, path: path + ".expect.fileName") |
| 40 | + var names = Set<String>() |
| 41 | + for (fileIndex, file) in item.existingFiles.enumerated() { |
| 42 | + let filePath = "\(path).existingFiles[\(fileIndex)].name" |
| 43 | + try validateFileName(file.name, path: filePath) |
| 44 | + guard names.insert(file.name).inserted else { |
| 45 | + throw HarnessDiagnostic(code: "fixtureName", path: filePath, message: "Duplicate fixture filename") |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + return HarnessSuite(cases: document.cases) |
| 50 | + } |
| 51 | + |
| 52 | + public static func load(from url: URL) throws -> HarnessSuite { |
| 53 | + let handle: FileHandle |
| 54 | + do { handle = try FileHandle(forReadingFrom: url) } |
| 55 | + catch { throw HarnessDiagnostic(code: "inputRead", path: "$", message: "Cannot open suite input") } |
| 56 | + defer { try? handle.close() } |
| 57 | + var data = Data() |
| 58 | + do { |
| 59 | + while data.count <= maximumBytes { |
| 60 | + let chunk = try handle.read(upToCount: min(65_536, maximumBytes + 1 - data.count)) ?? Data() |
| 61 | + if chunk.isEmpty { break } |
| 62 | + data.append(chunk) |
| 63 | + } |
| 64 | + } catch { |
| 65 | + throw HarnessDiagnostic(code: "inputRead", path: "$", message: "Cannot read suite input") |
| 66 | + } |
| 67 | + return try load(data: data) |
| 68 | + } |
| 69 | + |
| 70 | + private static var sizeError: HarnessDiagnostic { |
| 71 | + HarnessDiagnostic(code: "sizeLimit", path: "$", message: "Suite input exceeds 1 MiB") |
25 | 72 | } |
26 | | -} |
27 | 73 |
|
28 | | -public struct HarnessCaseResult: Equatable, Sendable { |
29 | | - public var name: String |
30 | | - public var createdFileName: String |
31 | | - public var passed: Bool |
| 74 | + private static func validateFileName(_ name: String, path: String) throws { |
| 75 | + let forbidden = CharacterSet(charactersIn: "/\\:").union(.controlCharacters) |
| 76 | + guard !name.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, |
| 77 | + name != ".", name != "..", name.unicodeScalars.allSatisfy({ !forbidden.contains($0) }) else { |
| 78 | + throw HarnessDiagnostic(code: "fileName", path: path, message: "Expected a single nonblank filename without separators or controls") |
| 79 | + } |
| 80 | + } |
32 | 81 | } |
33 | 82 |
|
34 | | -public enum HarnessRunner { |
35 | | - public static func run( |
36 | | - cases: [FileCreationHarnessCase], |
37 | | - workspaceRoot: URL, |
38 | | - fileManager: FileManager = .default |
39 | | - ) throws -> [HarnessCaseResult] { |
40 | | - let service = FileCreationService(fileManager: fileManager) |
41 | | - let fixedDate = Date(timeIntervalSince1970: 1_704_067_200) |
| 83 | +private struct HarnessDocument: Decodable { |
| 84 | + let cases: [HarnessCase] |
| 85 | + init(from decoder: any Decoder) throws { |
| 86 | + let c = try harnessContainer(decoder, allowing: ["schemaVersion", "cases"]) |
| 87 | + guard try c.decode(Int.self, forKey: HarnessKey("schemaVersion")) == 1 else { |
| 88 | + throw HarnessDiagnostic(code: "schemaVersion", path: "$.schemaVersion", message: "Expected schemaVersion 1") |
| 89 | + } |
| 90 | + cases = try c.decode([HarnessCase].self, forKey: HarnessKey("cases")) |
| 91 | + } |
| 92 | +} |
42 | 93 |
|
43 | | - return try cases.map { testCase in |
44 | | - let caseRoot = workspaceRoot.appendingPathComponent(UUID().uuidString, isDirectory: true) |
45 | | - try fileManager.createDirectory(at: caseRoot, withIntermediateDirectories: true) |
| 94 | +struct HarnessCase: Decodable, Sendable { |
| 95 | + let id: String |
| 96 | + let name: String |
| 97 | + let templateID: String |
| 98 | + let requestedFileName: String? |
| 99 | + let existingFiles: [HarnessFixture] |
| 100 | + let expect: HarnessExpectation |
46 | 101 |
|
47 | | - for existingFileName in testCase.existingFileNames { |
48 | | - let existingURL = caseRoot.appendingPathComponent(existingFileName, isDirectory: false) |
49 | | - fileManager.createFile(atPath: existingURL.path, contents: Data(), attributes: nil) |
50 | | - } |
| 102 | + init(from decoder: any Decoder) throws { |
| 103 | + let c = try harnessContainer(decoder, allowing: ["id", "name", "templateID", "requestedFileName", "existingFiles", "expect"]) |
| 104 | + id = try c.decode(String.self, forKey: HarnessKey("id")) |
| 105 | + name = try c.decode(String.self, forKey: HarnessKey("name")) |
| 106 | + templateID = try c.decode(String.self, forKey: HarnessKey("templateID")) |
| 107 | + requestedFileName = try c.decodeIfPresent(String.self, forKey: HarnessKey("requestedFileName")) |
| 108 | + existingFiles = try c.decode([HarnessFixture].self, forKey: HarnessKey("existingFiles")) |
| 109 | + expect = try c.decode(HarnessExpectation.self, forKey: HarnessKey("expect")) |
| 110 | + } |
| 111 | +} |
51 | 112 |
|
52 | | - guard let template = TemplateCatalog.template(withID: testCase.templateID) else { |
53 | | - throw FileMintError.templateNotFound(testCase.templateID) |
54 | | - } |
| 113 | +struct HarnessFixture: Decodable, Sendable { |
| 114 | + let name: String |
| 115 | + let content: String |
| 116 | + init(from decoder: any Decoder) throws { |
| 117 | + let c = try harnessContainer(decoder, allowing: ["name", "content"]) |
| 118 | + name = try c.decode(String.self, forKey: HarnessKey("name")) |
| 119 | + content = try c.decode(String.self, forKey: HarnessKey("content")) |
| 120 | + } |
| 121 | +} |
55 | 122 |
|
56 | | - let result = try service.createFile( |
57 | | - FileCreationRequest( |
58 | | - destinationDirectory: caseRoot, |
59 | | - template: template, |
60 | | - requestedFileName: testCase.requestedFileName, |
61 | | - collisionStrategy: .increment |
62 | | - ), |
63 | | - now: fixedDate |
64 | | - ) |
| 123 | +struct HarnessExpectation: Decodable, Sendable { |
| 124 | + let fileName: String |
| 125 | + let content: HarnessContent |
| 126 | + init(from decoder: any Decoder) throws { |
| 127 | + let c = try harnessContainer(decoder, allowing: ["fileName", "content"]) |
| 128 | + fileName = try c.decode(String.self, forKey: HarnessKey("fileName")) |
| 129 | + content = try c.decode(HarnessContent.self, forKey: HarnessKey("content")) |
| 130 | + } |
| 131 | +} |
65 | 132 |
|
66 | | - let content = try String(contentsOf: result.createdURL, encoding: .utf8) |
67 | | - let contentMatches = testCase.expectedContentContains.allSatisfy { content.contains($0) } |
68 | | - let fileNameMatches = result.createdURL.lastPathComponent == testCase.expectedFileName |
| 133 | +enum HarnessContent: Decodable, Sendable { |
| 134 | + case exact(String) |
| 135 | + case contains([String]) |
69 | 136 |
|
70 | | - return HarnessCaseResult( |
71 | | - name: testCase.name, |
72 | | - createdFileName: result.createdURL.lastPathComponent, |
73 | | - passed: fileNameMatches && contentMatches |
74 | | - ) |
| 137 | + init(from decoder: any Decoder) throws { |
| 138 | + let c = try harnessContainer(decoder, allowing: ["mode", "value", "values"]) |
| 139 | + let mode = try c.decode(String.self, forKey: HarnessKey("mode")) |
| 140 | + switch mode { |
| 141 | + case "exact": |
| 142 | + try harnessKeys(c, allowing: ["mode", "value"]) |
| 143 | + self = .exact(try c.decode(String.self, forKey: HarnessKey("value"))) |
| 144 | + case "contains": |
| 145 | + try harnessKeys(c, allowing: ["mode", "values"]) |
| 146 | + let values = try c.decode([String].self, forKey: HarnessKey("values")) |
| 147 | + guard !values.isEmpty, values.allSatisfy({ !$0.isEmpty }) else { |
| 148 | + throw HarnessDiagnostic(code: "content", path: harnessPath(c.codingPath) + ".values", message: "Expected nonempty UTF-8 fragments") |
| 149 | + } |
| 150 | + self = .contains(values) |
| 151 | + default: |
| 152 | + throw HarnessDiagnostic(code: "contentMode", path: harnessPath(c.codingPath) + ".mode", message: "Expected exact or contains") |
75 | 153 | } |
76 | 154 | } |
77 | 155 | } |
0 commit comments