1
1
import test from "ava" ;
2
+ import esmock from "esmock" ;
2
3
import path from "node:path" ;
3
4
import { fileURLToPath } from "node:url" ;
4
5
import sinon from "sinon" ;
@@ -19,6 +20,24 @@ const themeLibraryEPath = path.join(__dirname, "..", "..", "fixtures", "theme.li
19
20
const genericExtensionPath = path . join ( __dirname , ".." , ".." , "fixtures" , "extension.a" ) ;
20
21
const moduleAPath = path . join ( __dirname , ".." , ".." , "fixtures" , "module.a" ) ;
21
22
23
+ function createSubclass ( Specification ) {
24
+ class DummySpecification extends Specification {
25
+ getPath ( ) {
26
+ return "path" ;
27
+ }
28
+ getType ( ) {
29
+ return "type" ;
30
+ }
31
+ getKind ( ) {
32
+ return "kind" ;
33
+ }
34
+ getName ( ) {
35
+ return "name" ;
36
+ }
37
+ }
38
+ return DummySpecification ;
39
+ }
40
+
22
41
test . beforeEach ( ( t ) => {
23
42
t . context . basicProjectInput = {
24
43
id : "application.a.id" ,
@@ -37,6 +56,14 @@ test.afterEach.always((t) => {
37
56
sinon . restore ( ) ;
38
57
} ) ;
39
58
59
+ test ( "Specification can't be instantiated" , ( t ) => {
60
+ t . throws ( ( ) => {
61
+ new Specification ( ) ;
62
+ } , {
63
+ message : "Class 'Specification' is abstract. Please use one of the 'types' subclasses"
64
+ } ) ;
65
+ } ) ;
66
+
40
67
test ( "Instantiate a basic project" , async ( t ) => {
41
68
const project = await Specification . create ( t . context . basicProjectInput ) ;
42
69
t . is ( project . getName ( ) , "application.a" , "Returned correct name" ) ;
@@ -299,3 +326,60 @@ test("Invalid specVersion", async (t) => {
299
326
"For details, see https://sap.github.io/ui5-tooling/pages/Configuration/#specification-versions"
300
327
} , "Threw with expected error message" ) ;
301
328
} ) ;
329
+
330
+ test ( "getRootReader: Default parameters" , async ( t ) => {
331
+ // Since Specification#create instantiates a far-away subclass, it would be a mess to mock
332
+ // every class up to "Specification.js" just to stub the resourceFactory's createReader method
333
+ // Therefore we just come up with our own subclass that can be instantiated right away:
334
+
335
+ const createReaderStub = sinon . stub ( ) ;
336
+ const Specification = await esmock ( "../../../lib/specifications/Specification.js" , {
337
+ "@ui5/fs/resourceFactory" : {
338
+ createReader : createReaderStub
339
+ }
340
+ } ) ;
341
+
342
+ const DummySpecification = createSubclass ( Specification ) ;
343
+ const spec = new DummySpecification ( ) ;
344
+ await spec . getRootReader ( ) ;
345
+
346
+ t . is ( createReaderStub . callCount , 1 , "createReader got called once" ) ;
347
+ t . deepEqual ( createReaderStub . getCall ( 0 ) . args [ 0 ] , {
348
+ fsBasePath : "path" ,
349
+ name : "Root reader for type kind name" ,
350
+ useGitignore : true ,
351
+ virBasePath : "/" ,
352
+ } , "createReader got called with expected arguments" ) ;
353
+ } ) ;
354
+
355
+ test ( "getRootReader: Custom parameters" , async ( t ) => {
356
+ const createReaderStub = sinon . stub ( ) ;
357
+ const Specification = await esmock ( "../../../lib/specifications/Specification.js" , {
358
+ "@ui5/fs/resourceFactory" : {
359
+ createReader : createReaderStub
360
+ }
361
+ } ) ;
362
+
363
+ const DummySpecification = createSubclass ( Specification ) ;
364
+ const spec = new DummySpecification ( ) ;
365
+ await spec . getRootReader ( { } ) ;
366
+ await spec . getRootReader ( {
367
+ useGitignore : false
368
+ } ) ;
369
+
370
+
371
+ t . is ( createReaderStub . callCount , 2 , "createReader got called twice" ) ;
372
+ t . deepEqual ( createReaderStub . getCall ( 0 ) . args [ 0 ] , {
373
+ fsBasePath : "path" ,
374
+ name : "Root reader for type kind name" ,
375
+ useGitignore : true ,
376
+ virBasePath : "/" ,
377
+ } , "createReader got called with expected arguments on first call" ) ;
378
+
379
+ t . deepEqual ( createReaderStub . getCall ( 1 ) . args [ 0 ] , {
380
+ fsBasePath : "path" ,
381
+ name : "Root reader for type kind name" ,
382
+ useGitignore : false ,
383
+ virBasePath : "/" ,
384
+ } , "createReader got called with expected arguments on second call" ) ;
385
+ } ) ;
0 commit comments