@@ -22,9 +22,12 @@ import {
22
22
waitForLoaderToBeRemoved ,
23
23
} from "testHelpers/renderHelpers" ;
24
24
import { server } from "testHelpers/server" ;
25
+ import type { FileTree } from "utils/filetree" ;
25
26
import type { MonacoEditorProps } from "./MonacoEditor" ;
26
27
import { Language } from "./PublishTemplateVersionDialog" ;
27
- import TemplateVersionEditorPage from "./TemplateVersionEditorPage" ;
28
+ import TemplateVersionEditorPage , {
29
+ findEntrypointFile ,
30
+ } from "./TemplateVersionEditorPage" ;
28
31
29
32
const { API } = apiModule ;
30
33
@@ -409,3 +412,105 @@ function renderEditorPage(queryClient: QueryClient) {
409
412
</ AppProviders > ,
410
413
) ;
411
414
}
415
+
416
+ describe ( "Find entrypoint" , ( ) => {
417
+ it ( "empty tree" , ( ) => {
418
+ const ft : FileTree = { } ;
419
+ const mainFile = findEntrypointFile ( ft ) ;
420
+ expect ( mainFile ) . toBeUndefined ( ) ;
421
+ } ) ;
422
+ it ( "flat structure, main.tf in root" , ( ) => {
423
+ const ft : FileTree = {
424
+ "aaa.tf" : "hello" ,
425
+ "bbb.tf" : "world" ,
426
+ "main.tf" : "foobar" ,
427
+ "nnn.tf" : "foobaz" ,
428
+ } ;
429
+
430
+ const mainFile = findEntrypointFile ( ft ) ;
431
+ expect ( mainFile ) . toBe ( "main.tf" ) ;
432
+ } ) ;
433
+ it ( "flat structure, no main.tf" , ( ) => {
434
+ const ft : FileTree = {
435
+ "aaa.tf" : "hello" ,
436
+ "bbb.tf" : "world" ,
437
+ "ccc.tf" : "foobaz" ,
438
+ "nnn.tf" : "foobaz" ,
439
+ } ;
440
+
441
+ const mainFile = findEntrypointFile ( ft ) ;
442
+ expect ( mainFile ) . toBe ( "nnn.tf" ) ;
443
+ } ) ;
444
+ it ( "with dirs, single main.tf" , ( ) => {
445
+ const ft : FileTree = {
446
+ "aaa-dir" : {
447
+ "aaa.tf" : "hello" ,
448
+ "bbb.tf" : "world" ,
449
+ } ,
450
+ "bbb-dir" : {
451
+ "aaa.tf" : "hello" ,
452
+ "bbb.tf" : "world" ,
453
+ } ,
454
+ "main.tf" : "foobar" ,
455
+ "nnn.tf" : "foobaz" ,
456
+ } ;
457
+
458
+ const mainFile = findEntrypointFile ( ft ) ;
459
+ expect ( mainFile ) . toBe ( "main.tf" ) ;
460
+ } ) ;
461
+ it ( "with dirs, multiple main.tf's" , ( ) => {
462
+ const ft : FileTree = {
463
+ "aaa-dir" : {
464
+ "aaa.tf" : "hello" ,
465
+ "bbb.tf" : "world" ,
466
+ "main.tf" : "foobar" ,
467
+ } ,
468
+ "bbb-dir" : {
469
+ "aaa.tf" : "hello" ,
470
+ "bbb.tf" : "world" ,
471
+ "main.tf" : "foobar" ,
472
+ } ,
473
+ "ccc-dir" : {
474
+ "aaa.tf" : "hello" ,
475
+ "bbb.tf" : "world" ,
476
+ } ,
477
+ "main.tf" : "foobar" ,
478
+ "nnn.tf" : "foobaz" ,
479
+ "zzz-dir" : {
480
+ "aaa.tf" : "hello" ,
481
+ "bbb.tf" : "world" ,
482
+ "main.tf" : "foobar" ,
483
+ } ,
484
+ } ;
485
+
486
+ const mainFile = findEntrypointFile ( ft ) ;
487
+ expect ( mainFile ) . toBe ( "main.tf" ) ;
488
+ } ) ;
489
+ it ( "with dirs, multiple main.tf, no main.tf in root" , ( ) => {
490
+ const ft : FileTree = {
491
+ "aaa-dir" : {
492
+ "aaa.tf" : "hello" ,
493
+ "bbb.tf" : "world" ,
494
+ "main.tf" : "foobar" ,
495
+ } ,
496
+ "bbb-dir" : {
497
+ "aaa.tf" : "hello" ,
498
+ "bbb.tf" : "world" ,
499
+ "main.tf" : "foobar" ,
500
+ } ,
501
+ "ccc-dir" : {
502
+ "aaa.tf" : "hello" ,
503
+ "bbb.tf" : "world" ,
504
+ } ,
505
+ "nnn.tf" : "foobaz" ,
506
+ "zzz-dir" : {
507
+ "aaa.tf" : "hello" ,
508
+ "bbb.tf" : "world" ,
509
+ "main.tf" : "foobar" ,
510
+ } ,
511
+ } ;
512
+
513
+ const mainFile = findEntrypointFile ( ft ) ;
514
+ expect ( mainFile ) . toBe ( "aaa-dir/main.tf" ) ;
515
+ } ) ;
516
+ } ) ;
0 commit comments