@@ -38,7 +38,9 @@ describe('cli', () => {
3838 expect ( stderr . toString ( ) ) . toBe ( '' ) ;
3939
4040 await expect ( readFile ( path . join ( cwd , 'secrets/.env' ) , 'utf8' ) ) . resolves . toBe ( 'SECRET=1' ) ;
41- await expect ( readFile ( path . join ( cwd , 'config/app.json' ) , 'utf8' ) ) . resolves . toBe ( '{"flag":true}' ) ;
41+ await expect ( readFile ( path . join ( cwd , 'config/app.json' ) , 'utf8' ) ) . resolves . toBe (
42+ '{"flag":true}' ,
43+ ) ;
4244
4345 const gitignore = await readFile ( path . join ( cwd , '.gitignore' ) , 'utf8' ) ;
4446 expect ( gitignore ) . toContain ( 'secrets/.env' ) ;
@@ -48,6 +50,83 @@ describe('cli', () => {
4850 } ) ;
4951 } ) ;
5052
53+ test ( 'loads configuration from a referenced module' , async ( ) => {
54+ await withTempDir ( async ( cwd ) => {
55+ const modulePath = path . join ( cwd , 'cpconfig.config.mjs' ) ;
56+
57+ await writeFile (
58+ modulePath ,
59+ `export default {\n files: {\n 'module-output.txt': { contents: 'from module' }\n }\n};\n` ,
60+ ) ;
61+
62+ await writeFile (
63+ path . join ( cwd , 'package.json' ) ,
64+ JSON . stringify (
65+ {
66+ ...packageTemplate ,
67+ config : {
68+ cpconfig : './cpconfig.config.mjs' ,
69+ } ,
70+ } ,
71+ null ,
72+ 2 ,
73+ ) ,
74+ ) ;
75+
76+ const stdout = createBuffer ( ) ;
77+ const stderr = createBuffer ( ) ;
78+
79+ const exitCode = await runCli ( [ ] , { cwd, stdout, stderr } ) ;
80+
81+ expect ( exitCode ) . toBe ( 0 ) ;
82+ expect ( stderr . toString ( ) ) . toBe ( '' ) ;
83+
84+ await expect ( readFile ( path . join ( cwd , 'module-output.txt' ) , 'utf8' ) ) . resolves . toBe (
85+ 'from module' ,
86+ ) ;
87+ const gitignore = await readFile ( path . join ( cwd , '.gitignore' ) , 'utf8' ) ;
88+ expect ( gitignore ) . toContain ( 'module-output.txt' ) ;
89+ expect ( stdout . toString ( ) ) . toContain ( 'cpconfig apply' ) ;
90+ expect ( stdout . toString ( ) ) . toContain ( 'cpconfig.config.mjs' ) ;
91+ } ) ;
92+ } ) ;
93+
94+ test ( 'supports config modules exporting factories' , async ( ) => {
95+ await withTempDir ( async ( cwd ) => {
96+ const modulePath = path . join ( cwd , 'cpconfig.factory.mjs' ) ;
97+
98+ await writeFile (
99+ modulePath ,
100+ `export default async function buildConfig() {\n await Promise.resolve();\n return {\n files: {\n 'factory.txt': { contents: 'from factory' }\n },\n options: {\n gitignorePath: 'generated.ignore'\n }\n };\n}\n` ,
101+ ) ;
102+
103+ await writeFile (
104+ path . join ( cwd , 'package.json' ) ,
105+ JSON . stringify (
106+ {
107+ ...packageTemplate ,
108+ cpconfig : './cpconfig.factory.mjs' ,
109+ } ,
110+ null ,
111+ 2 ,
112+ ) ,
113+ ) ;
114+
115+ const stdout = createBuffer ( ) ;
116+ const stderr = createBuffer ( ) ;
117+
118+ const exitCode = await runCli ( [ ] , { cwd, stdout, stderr } ) ;
119+
120+ expect ( exitCode ) . toBe ( 0 ) ;
121+ expect ( stderr . toString ( ) ) . toBe ( '' ) ;
122+
123+ await expect ( readFile ( path . join ( cwd , 'factory.txt' ) , 'utf8' ) ) . resolves . toBe ( 'from factory' ) ;
124+ const gitignore = await readFile ( path . join ( cwd , 'generated.ignore' ) , 'utf8' ) ;
125+ expect ( gitignore ) . toContain ( 'factory.txt' ) ;
126+ expect ( stdout . toString ( ) ) . toContain ( 'cpconfig.factory.mjs' ) ;
127+ } ) ;
128+ } ) ;
129+
51130 test ( 'supports dry runs without writing files' , async ( ) => {
52131 await withTempDir ( async ( cwd ) => {
53132 await writeFile (
0 commit comments