@@ -4,23 +4,23 @@ import Testing
44@testable import PackageToJS
55
66@Suite struct WasmOptTests {
7-
7+
88 /// A mock system that simulates wasm-opt not being available
99 class MockPackagingSystemWithoutWasmOpt : PackagingSystem {
1010 var warnings : [ String ] = [ ]
1111 var wasmOptCalls : [ ( arguments: [ String ] , input: String , output: String ) ] = [ ]
12-
12+
1313 func npmInstall( packageDir: String ) throws {
1414 // No-op for testing
1515 }
16-
16+
1717 func wasmOpt( _ arguments: [ String ] , input: String , output: String ) throws {
1818 wasmOptCalls. append ( ( arguments, input, output) )
19-
19+
2020 // Simulate wasm-opt not found by using the same logic as DefaultPackagingSystem
2121 // but without the actual which() call succeeding
2222 warnings. append ( " Warning: wasm-opt is not installed, optimizations will not be applied " )
23-
23+
2424 // Test the file copy logic that would happen when wasm-opt is not found
2525 // (This matches the fixed behavior that removes existing files first)
2626 if FileManager . default. fileExists ( atPath: output) {
@@ -29,132 +29,132 @@ import Testing
2929 try FileManager . default. copyItem ( atPath: input, toPath: output)
3030 }
3131 }
32-
32+
3333 /// A mock system that fails when copying files (to test error handling)
3434 class MockPackagingSystemWithCopyFailure : PackagingSystem {
3535 var wasmOptCalls : [ ( arguments: [ String ] , input: String , output: String ) ] = [ ]
36-
36+
3737 func npmInstall( packageDir: String ) throws {
3838 // No-op for testing
3939 }
40-
40+
4141 func wasmOpt( _ arguments: [ String ] , input: String , output: String ) throws {
4242 wasmOptCalls. append ( ( arguments, input, output) )
43-
43+
4444 // Simulate the case where wasm-opt is not found and file copy fails
4545 throw PackageToJSError ( " Permission denied when copying wasm file " )
4646 }
4747 }
48-
48+
4949 @Test func wasmOptNotInstalledFallbackWorksCorrectly( ) throws {
5050 try withTemporaryDirectory { tempDir, _ in
5151 // Create a dummy input wasm file
5252 let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
5353 let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
5454 let dummyContent = Data ( " dummy wasm content " . utf8)
5555 try dummyContent. write ( to: inputFile)
56-
56+
5757 let mockSystem = MockPackagingSystemWithoutWasmOpt ( )
58-
58+
5959 // This should copy the file instead of optimizing
6060 try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
61-
61+
6262 // Verify the file was copied
6363 #expect( FileManager . default. fileExists ( atPath: outputFile. path) )
6464 let copiedContent = try Data ( contentsOf: outputFile)
6565 #expect( copiedContent == dummyContent)
66-
66+
6767 // Verify warning was recorded
6868 #expect( mockSystem. warnings. count == 1 )
6969 #expect( mockSystem. warnings [ 0 ] . contains ( " wasm-opt is not installed " ) )
70-
70+
7171 // Verify the call was made
7272 #expect( mockSystem. wasmOptCalls. count == 1 )
7373 #expect( mockSystem. wasmOptCalls [ 0 ] . arguments == [ " -Os " ] )
7474 }
7575 }
76-
76+
7777 @Test func wasmOptNotInstalledHandlesExistingOutputFile( ) throws {
7878 try withTemporaryDirectory { tempDir, _ in
7979 // Create a dummy input wasm file
8080 let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
8181 let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
8282 let inputContent = Data ( " input wasm content " . utf8)
8383 let existingContent = Data ( " existing output content " . utf8)
84-
84+
8585 try inputContent. write ( to: inputFile)
8686 try existingContent. write ( to: outputFile)
87-
87+
8888 let mockSystem = MockPackagingSystemWithoutWasmOpt ( )
89-
89+
9090 // This should overwrite the existing output file
9191 try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
92-
92+
9393 // Verify the output file has the input content (not the existing content)
9494 #expect( FileManager . default. fileExists ( atPath: outputFile. path) )
9595 let finalContent = try Data ( contentsOf: outputFile)
9696 #expect( finalContent == inputContent)
9797 #expect( finalContent != existingContent)
9898 }
9999 }
100-
100+
101101 @Test func wasmOptCopyFailureThrowsError( ) throws {
102102 try withTemporaryDirectory { tempDir, _ in
103103 let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
104104 let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
105-
105+
106106 let mockSystem = MockPackagingSystemWithCopyFailure ( )
107-
107+
108108 // This should throw an error
109109 #expect( throws: PackageToJSError . self) {
110110 try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
111111 }
112-
112+
113113 // Verify the call was made
114114 #expect( mockSystem. wasmOptCalls. count == 1 )
115115 }
116116 }
117-
117+
118118 @Test func realDefaultPackagingSystemFallbackBehavior( ) throws {
119119 try withTemporaryDirectory { tempDir, _ in
120120 // Create a dummy input wasm file
121121 let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
122122 let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
123123 let dummyContent = Data ( " dummy wasm content " . utf8)
124124 try dummyContent. write ( to: inputFile)
125-
125+
126126 var capturedWarnings : [ String ] = [ ]
127127 let realSystem = DefaultPackagingSystem { warning in
128128 capturedWarnings. append ( warning)
129129 }
130-
130+
131131 // Temporarily move wasm-opt out of the way to simulate it not being installed
132132 let wasmOptPath = " /opt/homebrew/bin/wasm-opt "
133133 let tempWasmOptPath = " /tmp/wasm-opt-backup-for-test "
134-
134+
135135 let wasmOptExists = FileManager . default. fileExists ( atPath: wasmOptPath)
136136 if wasmOptExists {
137137 try ? FileManager . default. moveItem ( atPath: wasmOptPath, toPath: tempWasmOptPath)
138138 }
139-
139+
140140 defer {
141141 // Restore wasm-opt if it existed
142142 if wasmOptExists {
143143 try ? FileManager . default. moveItem ( atPath: tempWasmOptPath, toPath: wasmOptPath)
144144 }
145145 }
146-
146+
147147 // This should fall back to copying since wasm-opt is not available
148148 try realSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
149-
149+
150150 // Verify the file was copied
151151 #expect( FileManager . default. fileExists ( atPath: outputFile. path) )
152152 let copiedContent = try Data ( contentsOf: outputFile)
153153 #expect( copiedContent == dummyContent)
154-
154+
155155 // Verify warning was issued
156156 #expect( capturedWarnings. count >= 1 )
157157 #expect( capturedWarnings [ 0 ] . contains ( " wasm-opt is not installed " ) )
158158 }
159159 }
160- }
160+ }
0 commit comments