@@ -4,23 +4,23 @@ import Testing
4
4
@testable import PackageToJS
5
5
6
6
@Suite struct WasmOptTests {
7
-
7
+
8
8
/// A mock system that simulates wasm-opt not being available
9
9
class MockPackagingSystemWithoutWasmOpt : PackagingSystem {
10
10
var warnings : [ String ] = [ ]
11
11
var wasmOptCalls : [ ( arguments: [ String ] , input: String , output: String ) ] = [ ]
12
-
12
+
13
13
func npmInstall( packageDir: String ) throws {
14
14
// No-op for testing
15
15
}
16
-
16
+
17
17
func wasmOpt( _ arguments: [ String ] , input: String , output: String ) throws {
18
18
wasmOptCalls. append ( ( arguments, input, output) )
19
-
19
+
20
20
// Simulate wasm-opt not found by using the same logic as DefaultPackagingSystem
21
21
// but without the actual which() call succeeding
22
22
warnings. append ( " Warning: wasm-opt is not installed, optimizations will not be applied " )
23
-
23
+
24
24
// Test the file copy logic that would happen when wasm-opt is not found
25
25
// (This matches the fixed behavior that removes existing files first)
26
26
if FileManager . default. fileExists ( atPath: output) {
@@ -29,132 +29,132 @@ import Testing
29
29
try FileManager . default. copyItem ( atPath: input, toPath: output)
30
30
}
31
31
}
32
-
32
+
33
33
/// A mock system that fails when copying files (to test error handling)
34
34
class MockPackagingSystemWithCopyFailure : PackagingSystem {
35
35
var wasmOptCalls : [ ( arguments: [ String ] , input: String , output: String ) ] = [ ]
36
-
36
+
37
37
func npmInstall( packageDir: String ) throws {
38
38
// No-op for testing
39
39
}
40
-
40
+
41
41
func wasmOpt( _ arguments: [ String ] , input: String , output: String ) throws {
42
42
wasmOptCalls. append ( ( arguments, input, output) )
43
-
43
+
44
44
// Simulate the case where wasm-opt is not found and file copy fails
45
45
throw PackageToJSError ( " Permission denied when copying wasm file " )
46
46
}
47
47
}
48
-
48
+
49
49
@Test func wasmOptNotInstalledFallbackWorksCorrectly( ) throws {
50
50
try withTemporaryDirectory { tempDir, _ in
51
51
// Create a dummy input wasm file
52
52
let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
53
53
let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
54
54
let dummyContent = Data ( " dummy wasm content " . utf8)
55
55
try dummyContent. write ( to: inputFile)
56
-
56
+
57
57
let mockSystem = MockPackagingSystemWithoutWasmOpt ( )
58
-
58
+
59
59
// This should copy the file instead of optimizing
60
60
try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
61
-
61
+
62
62
// Verify the file was copied
63
63
#expect( FileManager . default. fileExists ( atPath: outputFile. path) )
64
64
let copiedContent = try Data ( contentsOf: outputFile)
65
65
#expect( copiedContent == dummyContent)
66
-
66
+
67
67
// Verify warning was recorded
68
68
#expect( mockSystem. warnings. count == 1 )
69
69
#expect( mockSystem. warnings [ 0 ] . contains ( " wasm-opt is not installed " ) )
70
-
70
+
71
71
// Verify the call was made
72
72
#expect( mockSystem. wasmOptCalls. count == 1 )
73
73
#expect( mockSystem. wasmOptCalls [ 0 ] . arguments == [ " -Os " ] )
74
74
}
75
75
}
76
-
76
+
77
77
@Test func wasmOptNotInstalledHandlesExistingOutputFile( ) throws {
78
78
try withTemporaryDirectory { tempDir, _ in
79
79
// Create a dummy input wasm file
80
80
let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
81
81
let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
82
82
let inputContent = Data ( " input wasm content " . utf8)
83
83
let existingContent = Data ( " existing output content " . utf8)
84
-
84
+
85
85
try inputContent. write ( to: inputFile)
86
86
try existingContent. write ( to: outputFile)
87
-
87
+
88
88
let mockSystem = MockPackagingSystemWithoutWasmOpt ( )
89
-
89
+
90
90
// This should overwrite the existing output file
91
91
try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
92
-
92
+
93
93
// Verify the output file has the input content (not the existing content)
94
94
#expect( FileManager . default. fileExists ( atPath: outputFile. path) )
95
95
let finalContent = try Data ( contentsOf: outputFile)
96
96
#expect( finalContent == inputContent)
97
97
#expect( finalContent != existingContent)
98
98
}
99
99
}
100
-
100
+
101
101
@Test func wasmOptCopyFailureThrowsError( ) throws {
102
102
try withTemporaryDirectory { tempDir, _ in
103
103
let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
104
104
let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
105
-
105
+
106
106
let mockSystem = MockPackagingSystemWithCopyFailure ( )
107
-
107
+
108
108
// This should throw an error
109
109
#expect( throws: PackageToJSError . self) {
110
110
try mockSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
111
111
}
112
-
112
+
113
113
// Verify the call was made
114
114
#expect( mockSystem. wasmOptCalls. count == 1 )
115
115
}
116
116
}
117
-
117
+
118
118
@Test func realDefaultPackagingSystemFallbackBehavior( ) throws {
119
119
try withTemporaryDirectory { tempDir, _ in
120
120
// Create a dummy input wasm file
121
121
let inputFile = tempDir. appendingPathComponent ( " input.wasm " )
122
122
let outputFile = tempDir. appendingPathComponent ( " output.wasm " )
123
123
let dummyContent = Data ( " dummy wasm content " . utf8)
124
124
try dummyContent. write ( to: inputFile)
125
-
125
+
126
126
var capturedWarnings : [ String ] = [ ]
127
127
let realSystem = DefaultPackagingSystem { warning in
128
128
capturedWarnings. append ( warning)
129
129
}
130
-
130
+
131
131
// Temporarily move wasm-opt out of the way to simulate it not being installed
132
132
let wasmOptPath = " /opt/homebrew/bin/wasm-opt "
133
133
let tempWasmOptPath = " /tmp/wasm-opt-backup-for-test "
134
-
134
+
135
135
let wasmOptExists = FileManager . default. fileExists ( atPath: wasmOptPath)
136
136
if wasmOptExists {
137
137
try ? FileManager . default. moveItem ( atPath: wasmOptPath, toPath: tempWasmOptPath)
138
138
}
139
-
139
+
140
140
defer {
141
141
// Restore wasm-opt if it existed
142
142
if wasmOptExists {
143
143
try ? FileManager . default. moveItem ( atPath: tempWasmOptPath, toPath: wasmOptPath)
144
144
}
145
145
}
146
-
146
+
147
147
// This should fall back to copying since wasm-opt is not available
148
148
try realSystem. wasmOpt ( [ " -Os " ] , input: inputFile. path, output: outputFile. path)
149
-
149
+
150
150
// Verify the file was copied
151
151
#expect( FileManager . default. fileExists ( atPath: outputFile. path) )
152
152
let copiedContent = try Data ( contentsOf: outputFile)
153
153
#expect( copiedContent == dummyContent)
154
-
154
+
155
155
// Verify warning was issued
156
156
#expect( capturedWarnings. count >= 1 )
157
157
#expect( capturedWarnings [ 0 ] . contains ( " wasm-opt is not installed " ) )
158
158
}
159
159
}
160
- }
160
+ }
0 commit comments