|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | + |
| 4 | +@testable import PackageToJS |
| 5 | + |
| 6 | +@Suite struct DefaultPackagingSystemTests { |
| 7 | + |
| 8 | + @Test func wasmOptFallbackHandlesNewOutputFile() throws { |
| 9 | + try withTemporaryDirectory { tempDir, _ in |
| 10 | + let inputFile = tempDir.appendingPathComponent("input.wasm") |
| 11 | + let outputFile = tempDir.appendingPathComponent("output.wasm") |
| 12 | + let inputContent = Data("input wasm content".utf8) |
| 13 | + try inputContent.write(to: inputFile) |
| 14 | + |
| 15 | + var warnings: [String] = [] |
| 16 | + // Create system with mock which function that always fails to find wasm-opt |
| 17 | + let system = DefaultPackagingSystem( |
| 18 | + printWarning: { warnings.append($0) }, |
| 19 | + which: { _ in throw PackageToJSError("wasm-opt not found") } |
| 20 | + ) |
| 21 | + |
| 22 | + // This should work - fallback should copy file |
| 23 | + try system.wasmOpt(["-Os"], input: inputFile.path, output: outputFile.path) |
| 24 | + |
| 25 | + // Verify the output file was created with input content |
| 26 | + let finalContent = try Data(contentsOf: outputFile) |
| 27 | + #expect(finalContent == inputContent) |
| 28 | + #expect(warnings.contains { $0.contains("wasm-opt is not installed") }) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test func wasmOptFallbackHandlesExistingOutputFile() throws { |
| 33 | + try withTemporaryDirectory { tempDir, _ in |
| 34 | + let inputFile = tempDir.appendingPathComponent("input.wasm") |
| 35 | + let outputFile = tempDir.appendingPathComponent("output.wasm") |
| 36 | + let inputContent = Data("input wasm content".utf8) |
| 37 | + let existingContent = Data("existing output content".utf8) |
| 38 | + |
| 39 | + // Create input file and existing output file |
| 40 | + try inputContent.write(to: inputFile) |
| 41 | + try existingContent.write(to: outputFile) |
| 42 | + |
| 43 | + var warnings: [String] = [] |
| 44 | + // Create system with mock which function that always fails to find wasm-opt |
| 45 | + let system = DefaultPackagingSystem( |
| 46 | + printWarning: { warnings.append($0) }, |
| 47 | + which: { _ in throw PackageToJSError("wasm-opt not found") } |
| 48 | + ) |
| 49 | + |
| 50 | + // This should work - fallback should overwrite existing file |
| 51 | + try system.wasmOpt(["-Os"], input: inputFile.path, output: outputFile.path) |
| 52 | + |
| 53 | + // Verify the output file was overwritten with input content |
| 54 | + let finalContent = try Data(contentsOf: outputFile) |
| 55 | + #expect(finalContent == inputContent) |
| 56 | + #expect(finalContent != existingContent) |
| 57 | + #expect(warnings.contains { $0.contains("wasm-opt is not installed") }) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments