@@ -7,7 +7,7 @@ import { beforeEach, describe, expect, test, vi } from 'vitest';
7
7
import { loadFileConfig , mergeConfigs } from '../../src/config/configLoad.js' ;
8
8
import type { RepomixConfigCli , RepomixConfigFile } from '../../src/config/configSchema.js' ;
9
9
import { getGlobalDirectory } from '../../src/config/globalDirectory.js' ;
10
- import { RepomixConfigValidationError } from '../../src/shared/errorHandle.js' ;
10
+ import { RepomixConfigValidationError , RepomixError } from '../../src/shared/errorHandle.js' ;
11
11
import { logger } from '../../src/shared/logger.js' ;
12
12
13
13
vi . mock ( 'node:fs/promises' ) ;
@@ -59,13 +59,15 @@ describe('configLoad', () => {
59
59
} ;
60
60
vi . mocked ( getGlobalDirectory ) . mockReturnValue ( '/global/repomix' ) ;
61
61
vi . mocked ( fs . stat )
62
- . mockRejectedValueOnce ( new Error ( 'File not found' ) ) // Local config
63
- . mockResolvedValueOnce ( { isFile : ( ) => true } as Stats ) ; // Global config
62
+ . mockRejectedValueOnce ( new Error ( 'File not found' ) ) // Local repomix.config.json5
63
+ . mockRejectedValueOnce ( new Error ( 'File not found' ) ) // Local repomix.config.jsonc
64
+ . mockRejectedValueOnce ( new Error ( 'File not found' ) ) // Local repomix.config.json
65
+ . mockResolvedValueOnce ( { isFile : ( ) => true } as Stats ) ; // Global repomix.config.json5
64
66
vi . mocked ( fs . readFile ) . mockResolvedValue ( JSON . stringify ( mockGlobalConfig ) ) ;
65
67
66
68
const result = await loadFileConfig ( process . cwd ( ) , null ) ;
67
69
expect ( result ) . toEqual ( mockGlobalConfig ) ;
68
- expect ( fs . readFile ) . toHaveBeenCalledWith ( path . join ( '/global/repomix' , 'repomix.config.json ' ) , 'utf-8' ) ;
70
+ expect ( fs . readFile ) . toHaveBeenCalledWith ( path . join ( '/global/repomix' , 'repomix.config.json5 ' ) , 'utf-8' ) ;
69
71
} ) ;
70
72
71
73
test ( 'should return an empty object if no config file is found' , async ( ) => {
@@ -77,6 +79,9 @@ describe('configLoad', () => {
77
79
expect ( result ) . toEqual ( { } ) ;
78
80
79
81
expect ( loggerSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'No custom config found' ) ) ;
82
+ expect ( loggerSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'repomix.config.json5' ) ) ;
83
+ expect ( loggerSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'repomix.config.jsonc' ) ) ;
84
+ expect ( loggerSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( 'repomix.config.json' ) ) ;
80
85
} ) ;
81
86
82
87
test ( 'should throw an error for invalid JSON' , async ( ) => {
@@ -138,6 +143,45 @@ describe('configLoad', () => {
138
143
} ,
139
144
} ) ;
140
145
} ) ;
146
+
147
+ test ( 'should load .jsonc config file with priority order' , async ( ) => {
148
+ const mockConfig = {
149
+ output : { filePath : 'jsonc-output.txt' } ,
150
+ ignore : { useDefaultPatterns : true } ,
151
+ } ;
152
+ vi . mocked ( fs . stat )
153
+ . mockRejectedValueOnce ( new Error ( 'File not found' ) ) // repomix.config.json5
154
+ . mockResolvedValueOnce ( { isFile : ( ) => true } as Stats ) ; // repomix.config.jsonc
155
+ vi . mocked ( fs . readFile ) . mockResolvedValue ( JSON . stringify ( mockConfig ) ) ;
156
+
157
+ const result = await loadFileConfig ( process . cwd ( ) , null ) ;
158
+ expect ( result ) . toEqual ( mockConfig ) ;
159
+ expect ( fs . readFile ) . toHaveBeenCalledWith ( path . resolve ( process . cwd ( ) , 'repomix.config.jsonc' ) , 'utf-8' ) ;
160
+ } ) ;
161
+
162
+ test ( 'should prioritize .json5 over .jsonc and .json' , async ( ) => {
163
+ const mockConfig = {
164
+ output : { filePath : 'json5-output.txt' } ,
165
+ ignore : { useDefaultPatterns : true } ,
166
+ } ;
167
+ vi . mocked ( fs . stat ) . mockResolvedValueOnce ( { isFile : ( ) => true } as Stats ) ; // repomix.config.json5 exists
168
+ vi . mocked ( fs . readFile ) . mockResolvedValue ( JSON . stringify ( mockConfig ) ) ;
169
+
170
+ const result = await loadFileConfig ( process . cwd ( ) , null ) ;
171
+ expect ( result ) . toEqual ( mockConfig ) ;
172
+ expect ( fs . readFile ) . toHaveBeenCalledWith ( path . resolve ( process . cwd ( ) , 'repomix.config.json5' ) , 'utf-8' ) ;
173
+ // Should not check for .jsonc or .json since .json5 was found
174
+ expect ( fs . stat ) . toHaveBeenCalledTimes ( 1 ) ;
175
+ } ) ;
176
+
177
+ test ( 'should throw RepomixError when specific config file does not exist' , async ( ) => {
178
+ const nonExistentConfigPath = 'non-existent-config.json' ;
179
+ vi . mocked ( fs . stat ) . mockRejectedValue ( new Error ( 'File not found' ) ) ;
180
+
181
+ await expect ( loadFileConfig ( process . cwd ( ) , nonExistentConfigPath ) ) . rejects . toThrow (
182
+ `Config file not found at ${ nonExistentConfigPath } ` ,
183
+ ) ;
184
+ } ) ;
141
185
} ) ;
142
186
143
187
describe ( 'mergeConfigs' , ( ) => {
0 commit comments