1
1
#!/usr/bin/env -S deno run --unstable --no-check --allow-run --allow-read --allow-write --allow-env --allow-net
2
2
3
- import { join , resolve } from "std/path/mod.ts" ;
3
+ import * as path from "std/path/mod.ts" ;
4
+ import * as fs from "std/fs/mod.ts" ;
4
5
import { setCwd } from "chdir-anywhere" ;
5
6
import { dev } from "./dev.js" ;
6
7
import { parseArgs } from "../test/shared/testArgs.js" ;
8
+ import { buildEngine } from "./buildEngine.js" ;
9
+ import { rollup } from "rollup" ;
10
+ import { rollupTerserPlugin } from "./shared/rollupTerserPlugin.js" ;
7
11
8
12
setCwd ( ) ;
9
13
Deno . chdir ( ".." ) ;
@@ -35,6 +39,7 @@ if (Deno.args.length > 0 && !Deno.args[0].startsWith("-")) {
35
39
}
36
40
37
41
const needsUnitTests = ! filteredTests || filteredTests . startsWith ( "test/unit" ) ;
42
+ const needsMinifiedTests = ! filteredTests || filteredTests . startsWith ( "test/minified" ) ;
38
43
const needsE2eTests = ! filteredTests || filteredTests . startsWith ( "test/e2e" ) ;
39
44
40
45
await dev ( {
@@ -60,7 +65,7 @@ if (needsUnitTests) {
60
65
await removeMaybeDirectory ( DENO_COVERAGE_DIR ) ;
61
66
await removeMaybeDirectory ( FAKE_IMPORTS_COVERAGE_DIR ) ;
62
67
denoTestArgs . push ( `--coverage=${ DENO_COVERAGE_DIR } ` ) ;
63
- const coverageMapPath = join ( Deno . cwd ( ) , FAKE_IMPORTS_COVERAGE_DIR ) ;
68
+ const coverageMapPath = path . join ( Deno . cwd ( ) , FAKE_IMPORTS_COVERAGE_DIR ) ;
64
69
applicationCmdArgs . add ( `--fi-coverage-map=${ coverageMapPath } ` ) ;
65
70
}
66
71
denoTestArgs . push ( filteredTests || "test/unit/" ) ;
@@ -69,6 +74,122 @@ if (needsUnitTests) {
69
74
testCommands . push ( cmd ) ;
70
75
}
71
76
77
+ // Minified tests
78
+ if ( needsMinifiedTests ) {
79
+ const testMinifiedDir = path . resolve ( "test/minified" ) ;
80
+ const testsDir = path . resolve ( testMinifiedDir , "tests" ) ;
81
+ const outDir = path . resolve ( testMinifiedDir , "out" ) ;
82
+ const engineDir = path . resolve ( outDir , "engine" ) ;
83
+ const testOutDir = path . resolve ( outDir , "tests" ) ;
84
+ const minifiedRendaPath = path . resolve ( testMinifiedDir , "shared/minifiedRenda.js" ) ;
85
+ const unminifiedRendaPath = path . resolve ( testMinifiedDir , "shared/unminifiedRenda.js" ) ;
86
+
87
+ const noBuildFlag = Deno . args . includes ( "--no-build" ) ;
88
+
89
+ const denoTestArgs = [ Deno . execPath ( ) , "test" , "--no-check" , "--allow-env" , "--allow-read" , "--allow-net" , "--parallel" ] ;
90
+
91
+ if ( noBuildFlag ) {
92
+ denoTestArgs . push ( filteredTests || "test/minified/tests/" ) ;
93
+ } else {
94
+ denoTestArgs . push ( "test/minified/out/tests/" ) ;
95
+
96
+ try {
97
+ await Deno . remove ( outDir , { recursive : true } ) ;
98
+ } catch ( e ) {
99
+ if ( e instanceof Deno . errors . NotFound ) {
100
+ // Already removed
101
+ } else {
102
+ throw e ;
103
+ }
104
+ }
105
+
106
+ await buildEngine ( engineDir ) ;
107
+
108
+ /**
109
+ * @returns {import("rollup").Plugin }
110
+ */
111
+ function rollupRedirectBuildPlugin ( ) {
112
+ return {
113
+ name : "redirectBuild" ,
114
+ async resolveId ( id , importer ) {
115
+ if ( importer ) {
116
+ const dirname = path . dirname ( importer ) ;
117
+ const resolved = path . resolve ( dirname , id ) ;
118
+ if ( resolved == minifiedRendaPath ) {
119
+ return path . resolve ( engineDir , "renda.js" ) ;
120
+ } else if ( resolved == unminifiedRendaPath ) {
121
+ // We want to allow tests to export from both the built and non-built library.
122
+ // This allows us to simulate situations such as minified client code that wants to
123
+ // communicate with non-minified server code.
124
+ // By marking ./src/unminifiedRenda.js as external, we make sure that the build of our tests
125
+ // keep referencing this file directly, as opposed to including its contents in the bundle.
126
+ // But we do have to rewrite the imported path, since bundled tests will live at another location.
127
+ const newResolved = path . relative ( testOutDir , unminifiedRendaPath ) ;
128
+ return {
129
+ id : newResolved ,
130
+ external : true ,
131
+ } ;
132
+ }
133
+ }
134
+ // Treat std as external
135
+ if ( id . startsWith ( "std/" ) ) {
136
+ return false ;
137
+ }
138
+ return null ;
139
+ } ,
140
+ } ;
141
+ }
142
+
143
+ const testFiles = [ ] ;
144
+ for await ( const entry of fs . walk ( testsDir ) ) {
145
+ if ( entry . name . endsWith ( ".test.js" ) ) {
146
+ const relative = path . relative ( testsDir , entry . path ) ;
147
+ if ( relative . startsWith ( "out/" ) ) continue ;
148
+ testFiles . push ( entry . path ) ;
149
+ }
150
+ }
151
+ const bundle = await rollup ( {
152
+ input : testFiles ,
153
+ plugins : [ rollupRedirectBuildPlugin ( ) ] ,
154
+ onwarn : ( message ) => {
155
+ if ( message . code == "CIRCULAR_DEPENDENCY" ) return ;
156
+ console . error ( message . message ) ;
157
+ } ,
158
+ } ) ;
159
+ const debug = Deno . args . includes ( "--debug" ) || Deno . args . includes ( "-d" ) || inspect ;
160
+ await bundle . write ( {
161
+ dir : testOutDir ,
162
+ plugins : [
163
+ rollupTerserPlugin ( {
164
+ /* eslint-disable camelcase */
165
+ module : true ,
166
+ keep_classnames : debug ,
167
+ keep_fnames : debug ,
168
+ compress : {
169
+ drop_debugger : false ,
170
+ } ,
171
+ sourceMap : debug ,
172
+ mangle : {
173
+ module : true ,
174
+ properties : {
175
+ debug,
176
+ keep_quoted : "strict" ,
177
+ reserved : [ "Deno" , "test" , "fn" ] ,
178
+ } ,
179
+ } ,
180
+ output : {
181
+ beautify : debug ,
182
+ } ,
183
+ /* eslint-enable camelcase */
184
+ } ) ,
185
+ ] ,
186
+ } ) ;
187
+ }
188
+
189
+ if ( inspect ) denoTestArgs . push ( "--inspect-brk" ) ;
190
+ testCommands . push ( denoTestArgs ) ;
191
+ }
192
+
72
193
// E2e tests
73
194
if ( needsE2eTests ) {
74
195
const cmd = [ Deno . execPath ( ) , "run" , "--allow-env" , "--allow-read" , "--allow-write" , "--allow-run" , "--allow-net" ] ;
@@ -144,5 +265,5 @@ if (needsCoverage) {
144
265
throw e ;
145
266
}
146
267
}
147
- await Deno . rename ( resolve ( DENO_COVERAGE_DIR , "html" ) , DENO_HTML_COVERAGE_DIR ) ;
268
+ await Deno . rename ( path . resolve ( DENO_COVERAGE_DIR , "html" ) , DENO_HTML_COVERAGE_DIR ) ;
148
269
}
0 commit comments