9
9
10
10
import org .apache .commons .io .FileUtils ;
11
11
import org .junit .Assert ;
12
+ import org .junit .Before ;
12
13
import org .junit .Rule ;
13
14
import org .junit .Test ;
14
15
import org .junit .rules .TemporaryFolder ;
16
+ import org .mockito .Mock ;
15
17
import org .mockito .MockedStatic ;
16
18
import org .mockito .Mockito ;
17
19
20
+ import com .vaadin .flow .di .Lookup ;
21
+ import com .vaadin .flow .server .frontend .scanner .ClassFinder ;
18
22
import com .vaadin .flow .server .frontend .scanner .FrontendDependenciesScanner ;
19
23
24
+ import elemental .json .JsonObject ;
25
+
20
26
public class TaskRunDevBundleBuildTest {
21
27
22
28
@ Rule
23
29
public TemporaryFolder temporaryFolder = new TemporaryFolder ();
24
30
31
+ private Options options ;
32
+
33
+ ClassFinder finder ;
34
+
35
+ @ Before
36
+ public void init () {
37
+ options = new Options (Mockito .mock (Lookup .class ),
38
+ temporaryFolder .getRoot ()).withBuildDirectory ("target" );
39
+ finder = Mockito .mock (ClassFinder .class );
40
+ }
41
+
25
42
@ Test
26
43
public void noDevBundle_bundleCompilationRequires () throws IOException {
27
44
final boolean needsBuild = TaskRunDevBundleBuild .needsBuildInternal (
28
- temporaryFolder . getRoot ( ),
29
- Mockito . mock ( FrontendDependenciesScanner . class ) );
45
+ options , Mockito . mock ( FrontendDependenciesScanner . class ),
46
+ finder );
30
47
Assert .assertTrue ("Bundle should require creation if not available" ,
31
48
needsBuild );
32
49
}
@@ -43,8 +60,8 @@ public void devBundleStatsJsonMissing_bundleCompilationRequires()
43
60
.thenReturn (null );
44
61
45
62
final boolean needsBuild = TaskRunDevBundleBuild .needsBuildInternal (
46
- temporaryFolder . getRoot ( ),
47
- Mockito . mock ( FrontendDependenciesScanner . class ) );
63
+ options , Mockito . mock ( FrontendDependenciesScanner . class ),
64
+ finder );
48
65
Assert .assertTrue ("Missing stats.json should require bundling" ,
49
66
needsBuild );
50
67
}
@@ -82,7 +99,7 @@ public void hashesMatch_noNpmPackages_noCompilationRequired()
82
99
+ "}" );
83
100
84
101
final boolean needsBuild = TaskRunDevBundleBuild
85
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
102
+ .needsBuildInternal (options , depScanner , finder );
86
103
Assert .assertFalse ("Missing stats.json should require bundling" ,
87
104
needsBuild );
88
105
}
@@ -122,7 +139,7 @@ public void hashesMatch_statsMissingNpmPackages_compilationRequired()
122
139
+ "}" );
123
140
124
141
final boolean needsBuild = TaskRunDevBundleBuild
125
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
142
+ .needsBuildInternal (options , depScanner , finder );
126
143
Assert .assertTrue ("Missing npmPackage should require bundling" ,
127
144
needsBuild );
128
145
}
@@ -160,7 +177,7 @@ public void hashesMatch_statsMissingPackageJsonPackage_compilationRequired()
160
177
+ "}" );
161
178
162
179
final boolean needsBuild = TaskRunDevBundleBuild
163
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
180
+ .needsBuildInternal (options , depScanner , finder );
164
181
Assert .assertTrue ("Bundle missing module dependency should rebuild" ,
165
182
needsBuild );
166
183
}
@@ -199,7 +216,7 @@ public void hashesMatch_packageJsonMissingNpmPackages_statsHasJsonPackages_noCom
199
216
+ "}" );
200
217
201
218
final boolean needsBuild = TaskRunDevBundleBuild
202
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
219
+ .needsBuildInternal (options , depScanner , finder );
203
220
Assert .assertFalse (
204
221
"Not missing npmPackage in stats.json should not require compilation" ,
205
222
needsBuild );
@@ -235,7 +252,7 @@ public void hashesMatch_packageJsonHasRange_statsHasFixed_noCompilationRequired(
235
252
+ "}" );
236
253
237
254
final boolean needsBuild = TaskRunDevBundleBuild
238
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
255
+ .needsBuildInternal (options , depScanner , finder );
239
256
Assert .assertFalse (
240
257
"Not missing npmPackage in stats.json should not require compilation" ,
241
258
needsBuild );
@@ -271,7 +288,7 @@ public void hashesMatch_packageJsonHasTildeRange_statsHasNewerFixed_noCompilatio
271
288
+ "}" );
272
289
273
290
boolean needsBuild = TaskRunDevBundleBuild
274
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
291
+ .needsBuildInternal (options , depScanner , finder );
275
292
Assert .assertFalse (
276
293
"No compilation if tilde range only patch update" ,
277
294
needsBuild );
@@ -283,8 +300,8 @@ public void hashesMatch_packageJsonHasTildeRange_statsHasNewerFixed_noCompilatio
283
300
+ " \" packageJsonHash\" : \" af45419b27dcb44b875197df4347b97316cc8fa6055458223a73aedddcfe7cc6\" \n "
284
301
+ "}" );
285
302
286
- needsBuild = TaskRunDevBundleBuild
287
- . needsBuildInternal ( temporaryFolder . getRoot (), depScanner );
303
+ needsBuild = TaskRunDevBundleBuild . needsBuildInternal ( options ,
304
+ depScanner , finder );
288
305
Assert .assertTrue (
289
306
"Compilation required if minor version change for tilde range" ,
290
307
needsBuild );
@@ -320,7 +337,7 @@ public void hashesMatch_packageJsonHasCaretRange_statsHasNewerFixed_noCompilatio
320
337
+ "}" );
321
338
322
339
boolean needsBuild = TaskRunDevBundleBuild
323
- .needsBuildInternal (temporaryFolder . getRoot () , depScanner );
340
+ .needsBuildInternal (options , depScanner , finder );
324
341
Assert .assertFalse (
325
342
"No compilation if caret range only minor version update" ,
326
343
needsBuild );
@@ -332,11 +349,126 @@ public void hashesMatch_packageJsonHasCaretRange_statsHasNewerFixed_noCompilatio
332
349
+ " \" packageJsonHash\" : \" af45419b27dcb44b875197df4347b97316cc8fa6055458223a73aedddcfe7cc6\" \n "
333
350
+ "}" );
334
351
335
- needsBuild = TaskRunDevBundleBuild
336
- . needsBuildInternal ( temporaryFolder . getRoot (), depScanner );
352
+ needsBuild = TaskRunDevBundleBuild . needsBuildInternal ( options ,
353
+ depScanner , finder );
337
354
Assert .assertTrue (
338
355
"Compilation required if major version change for caret range" ,
339
356
needsBuild );
340
357
}
341
358
}
359
+
360
+ @ Test
361
+ public void noPackageJson_defaultPackagesAndModulesInStats_noBuildNeeded ()
362
+ throws IOException {
363
+ final FrontendDependenciesScanner depScanner = Mockito
364
+ .mock (FrontendDependenciesScanner .class );
365
+ Mockito .when (depScanner .getPackages ())
366
+ .thenReturn (Collections .singletonMap ("@vaadin/text" , "1.0.0" ));
367
+
368
+ String defaultHash = TaskRunDevBundleBuild
369
+ .getDefaultPackageJson (options , depScanner , finder )
370
+ .getObject (NodeUpdater .VAADIN_DEP_KEY )
371
+ .getString (NodeUpdater .HASH_KEY );
372
+
373
+ try (MockedStatic <FrontendUtils > utils = Mockito
374
+ .mockStatic (FrontendUtils .class )) {
375
+ utils .when (() -> FrontendUtils .getDevBundleFolder (Mockito .any ()))
376
+ .thenReturn (temporaryFolder .getRoot ());
377
+ utils .when (() -> FrontendUtils
378
+ .findBundleStatsJson (temporaryFolder .getRoot ()))
379
+ .thenReturn ("{\n " + " \" npmModules\" : {\n "
380
+ + " \" @polymer/polymer\" : \" 3.5.1\" ,\n "
381
+ + " \" @vaadin/common-frontend\" : \" 0.0.17\" ,\n "
382
+ + " \" @vaadin/router\" : \" 1.7.4\" ,\n "
383
+ + " \" construct-style-sheets-polyfill\" : \" 3.1.0\" ,\n "
384
+ + " \" lit\" : \" 2.4.1\" ,\n "
385
+ + " \" @vaadin/text\" : \" 1.0.0\" \n },\n "
386
+ + " \" entryScripts\" : [\n "
387
+ + " \" VAADIN/build/indexhtml-aa31f040.js\" \n "
388
+ + " ],\n " + " \" packageJsonHash\" : \" " + defaultHash
389
+ + "\" \n " + "}" );
390
+
391
+ final boolean needsBuild = TaskRunDevBundleBuild
392
+ .needsBuildInternal (options , depScanner , finder );
393
+ Assert .assertFalse (
394
+ "Default package.json should be built and validated" ,
395
+ needsBuild );
396
+ }
397
+ }
398
+
399
+ @ Test
400
+ public void noPackageJson_defaultPackagesInStats_missingNpmModules_buildNeeded ()
401
+ throws IOException {
402
+ final FrontendDependenciesScanner depScanner = Mockito
403
+ .mock (FrontendDependenciesScanner .class );
404
+ Mockito .when (depScanner .getPackages ())
405
+ .thenReturn (Collections .singletonMap ("@vaadin/text" , "1.0.0" ));
406
+
407
+ String defaultHash = TaskRunDevBundleBuild
408
+ .getDefaultPackageJson (options , depScanner , finder )
409
+ .getObject (NodeUpdater .VAADIN_DEP_KEY )
410
+ .getString (NodeUpdater .HASH_KEY );
411
+
412
+ try (MockedStatic <FrontendUtils > utils = Mockito
413
+ .mockStatic (FrontendUtils .class )) {
414
+ utils .when (() -> FrontendUtils .getDevBundleFolder (Mockito .any ()))
415
+ .thenReturn (temporaryFolder .getRoot ());
416
+ utils .when (() -> FrontendUtils
417
+ .findBundleStatsJson (temporaryFolder .getRoot ()))
418
+ .thenReturn ("{\n " + " \" npmModules\" : {\n "
419
+ + " \" @polymer/polymer\" : \" 3.5.1\" ,\n "
420
+ + " \" @vaadin/common-frontend\" : \" 0.0.17\" ,\n "
421
+ + " \" @vaadin/router\" : \" 1.7.4\" ,\n "
422
+ + " \" construct-style-sheets-polyfill\" : \" 3.1.0\" ,\n "
423
+ + " \" lit\" : \" 2.4.1\" \n " + " },\n "
424
+ + " \" entryScripts\" : [\n "
425
+ + " \" VAADIN/build/indexhtml-aa31f040.js\" \n "
426
+ + " ],\n " + " \" packageJsonHash\" : \" " + defaultHash
427
+ + "\" \n " + "}" );
428
+
429
+ final boolean needsBuild = TaskRunDevBundleBuild
430
+ .needsBuildInternal (options , depScanner , finder );
431
+ Assert .assertTrue (
432
+ "Missing NpmPackage with default bundle should require rebuild" ,
433
+ needsBuild );
434
+ }
435
+ }
436
+
437
+ @ Test
438
+ public void noPackageJson_defaultPackagesInStats_noBuildNeeded ()
439
+ throws IOException {
440
+ final FrontendDependenciesScanner depScanner = Mockito
441
+ .mock (FrontendDependenciesScanner .class );
442
+ Mockito .when (depScanner .getPackages ())
443
+ .thenReturn (Collections .emptyMap ());
444
+
445
+ String defaultHash = TaskRunDevBundleBuild
446
+ .getDefaultPackageJson (options , depScanner , finder )
447
+ .getObject (NodeUpdater .VAADIN_DEP_KEY )
448
+ .getString (NodeUpdater .HASH_KEY );
449
+
450
+ try (MockedStatic <FrontendUtils > utils = Mockito
451
+ .mockStatic (FrontendUtils .class )) {
452
+ utils .when (() -> FrontendUtils .getDevBundleFolder (Mockito .any ()))
453
+ .thenReturn (temporaryFolder .getRoot ());
454
+ utils .when (() -> FrontendUtils
455
+ .findBundleStatsJson (temporaryFolder .getRoot ()))
456
+ .thenReturn ("{\n " + " \" npmModules\" : {\n "
457
+ + " \" @polymer/polymer\" : \" 3.5.1\" ,\n "
458
+ + " \" @vaadin/common-frontend\" : \" 0.0.17\" ,\n "
459
+ + " \" @vaadin/router\" : \" 1.7.4\" ,\n "
460
+ + " \" construct-style-sheets-polyfill\" : \" 3.1.0\" ,\n "
461
+ + " \" lit\" : \" 2.4.1\" \n " + " },\n "
462
+ + " \" entryScripts\" : [\n "
463
+ + " \" VAADIN/build/indexhtml-aa31f040.js\" \n "
464
+ + " ],\n " + " \" packageJsonHash\" : \" " + defaultHash
465
+ + "\" \n " + "}" );
466
+
467
+ final boolean needsBuild = TaskRunDevBundleBuild
468
+ .needsBuildInternal (options , depScanner , finder );
469
+ Assert .assertFalse (
470
+ "Default package.json should be built and validated" ,
471
+ needsBuild );
472
+ }
473
+ }
342
474
}
0 commit comments