1
1
import * as fs from 'node:fs' ;
2
2
import * as os from 'node:os' ;
3
3
import { describe , beforeEach , afterEach , test , expect } from 'vitest' ;
4
- import { loadConfig } from '../../src/config/index.js' ;
4
+ import { loadClosestConfig , loadConfigFromProject } from '../../src/config/index.js' ;
5
5
import { normalizePath } from '../../src/config/config.js' ;
6
6
7
- describe ( 'Config: loadConfig ' , ( ) => {
7
+ describe ( 'Config' , ( ) => {
8
8
const testDir = `${ os . tmpdir ( ) } /glint-config-test-load-config-${ process . pid } ` ;
9
9
10
10
beforeEach ( ( ) => {
@@ -20,35 +20,111 @@ describe('Config: loadConfig', () => {
20
20
fs . rmSync ( testDir , { recursive : true , force : true } ) ;
21
21
} ) ;
22
22
23
- test ( 'throws an error if no config is found' , ( ) => {
24
- expect ( ( ) => loadConfig ( testDir ) ) . toThrow ( `Unable to find Glint configuration for ${ testDir } ` ) ;
23
+ describe ( 'loadClosestConfig' , ( ) => {
24
+ test ( 'throws an error if no config is found' , ( ) => {
25
+ expect ( ( ) => loadClosestConfig ( testDir ) ) . toThrow (
26
+ `Unable to find Glint configuration for ${ testDir } `
27
+ ) ;
28
+ } ) ;
29
+
30
+ test ( 'loads from a folder' , ( ) => {
31
+ fs . writeFileSync (
32
+ `${ testDir } /tsconfig.json` ,
33
+ JSON . stringify ( {
34
+ glint : {
35
+ environment : './local-env' ,
36
+ } ,
37
+ } )
38
+ ) ;
39
+
40
+ let config = loadClosestConfig ( `${ testDir } /deeply/nested/directory` ) ;
41
+
42
+ expect ( config . rootDir ) . toBe ( normalizePath ( testDir ) ) ;
43
+ expect ( config . environment . getConfiguredTemplateTags ( ) ) . toEqual ( { test : { } } ) ;
44
+ } ) ;
45
+
46
+ test ( 'locates config in a parent directory' , ( ) => {
47
+ fs . mkdirSync ( `${ testDir } /deeply/nested/directory` , { recursive : true } ) ;
48
+ fs . writeFileSync (
49
+ `${ testDir } /tsconfig.json` ,
50
+ JSON . stringify ( {
51
+ glint : {
52
+ environment : 'kaboom' ,
53
+ checkStandaloneTemplates : false ,
54
+ } ,
55
+ } )
56
+ ) ;
57
+ fs . writeFileSync (
58
+ `${ testDir } /deeply/tsconfig.json` ,
59
+ JSON . stringify ( {
60
+ extends : '../tsconfig.json' ,
61
+ glint : {
62
+ environment : '../local-env' ,
63
+ } ,
64
+ } )
65
+ ) ;
66
+
67
+ let config = loadClosestConfig ( `${ testDir } /deeply/nested/directory` ) ;
68
+
69
+ expect ( config . rootDir ) . toBe ( normalizePath ( `${ testDir } /deeply` ) ) ;
70
+ expect ( config . environment . getConfiguredTemplateTags ( ) ) . toEqual ( { test : { } } ) ;
71
+ expect ( config . checkStandaloneTemplates ) . toBe ( false ) ;
72
+ } ) ;
25
73
} ) ;
26
74
27
- test ( 'locates config in a parent directory' , ( ) => {
28
- fs . mkdirSync ( `${ testDir } /deeply/nested/directory` , { recursive : true } ) ;
29
- fs . writeFileSync (
30
- `${ testDir } /tsconfig.json` ,
31
- JSON . stringify ( {
32
- glint : {
33
- environment : 'kaboom' ,
34
- checkStandaloneTemplates : false ,
35
- } ,
36
- } )
37
- ) ;
38
- fs . writeFileSync (
39
- `${ testDir } /deeply/tsconfig.json` ,
40
- JSON . stringify ( {
41
- extends : '../tsconfig.json' ,
42
- glint : {
43
- environment : '../local-env' ,
44
- } ,
45
- } )
46
- ) ;
75
+ describe ( 'loadConfigFromProject' , ( ) => {
76
+ test ( 'throws an error if no config is found' , ( ) => {
77
+ expect ( ( ) => loadConfigFromProject ( testDir ) ) . toThrow (
78
+ `Unable to find Glint configuration for project ${ testDir } `
79
+ ) ;
80
+ expect ( ( ) => loadConfigFromProject ( `${ testDir } /tsconfig.json` ) ) . toThrow (
81
+ `Unable to find Glint configuration for project ${ testDir } `
82
+ ) ;
83
+ } ) ;
84
+
85
+ test ( 'loads from a folder' , ( ) => {
86
+ fs . writeFileSync (
87
+ `${ testDir } /tsconfig.json` ,
88
+ JSON . stringify ( {
89
+ glint : {
90
+ environment : './local-env' ,
91
+ } ,
92
+ } )
93
+ ) ;
94
+
95
+ expect ( loadConfigFromProject ( testDir ) . rootDir ) . toBe ( normalizePath ( testDir ) ) ;
96
+ } ) ;
97
+
98
+ test ( 'loads from a file' , ( ) => {
99
+ fs . writeFileSync (
100
+ `${ testDir } /tsconfig.custom.json` ,
101
+ JSON . stringify ( {
102
+ glint : {
103
+ environment : './local-env' ,
104
+ } ,
105
+ } )
106
+ ) ;
107
+
108
+ expect ( loadConfigFromProject ( `${ testDir } /tsconfig.custom.json` ) . rootDir ) . toBe (
109
+ normalizePath ( testDir )
110
+ ) ;
111
+ } ) ;
47
112
48
- let config = loadConfig ( `${ testDir } /deeply/nested/directory` ) ;
113
+ test ( 'does not search parent directories' , ( ) => {
114
+ fs . mkdirSync ( `${ testDir } /sub` , { recursive : true } ) ;
115
+ fs . writeFileSync (
116
+ `${ testDir } /tsconfig.json` ,
117
+ JSON . stringify ( {
118
+ glint : {
119
+ environment : 'kaboom' ,
120
+ checkStandaloneTemplates : false ,
121
+ } ,
122
+ } )
123
+ ) ;
49
124
50
- expect ( config . rootDir ) . toBe ( normalizePath ( `${ testDir } /deeply` ) ) ;
51
- expect ( config . environment . getConfiguredTemplateTags ( ) ) . toEqual ( { test : { } } ) ;
52
- expect ( config . checkStandaloneTemplates ) . toBe ( false ) ;
125
+ expect ( ( ) => loadConfigFromProject ( `${ testDir } /sub` ) ) . toThrow (
126
+ `Unable to find Glint configuration for project ${ testDir } `
127
+ ) ;
128
+ } ) ;
53
129
} ) ;
54
130
} ) ;
0 commit comments