@@ -62,7 +62,7 @@ describe('applyFixes', () => {
6262
6363 it ( 'does not duplicate keys in .env.example if already present' , ( ) => {
6464 fs . writeFileSync ( examplePath , 'A=\nB=\n' ) ;
65- const { changed , result } = applyFixes ( {
65+ const { } = applyFixes ( {
6666 envPath,
6767 missingKeys : [ 'B' ] ,
6868 duplicateKeys : [ ] ,
150150 } ) ;
151151
152152 const env = fs . readFileSync ( envPath , 'utf-8' ) ;
153- expect ( env ) . toBe ( ` A=2\nC=1\nB=2\n` ) ;
153+ expect ( env ) . toBe ( ' A=2\nC=1\nB=2\n' ) ;
154154 } ) ;
155155
156156 it ( 'handles ensureGitignore=true without throwing (best-effort)' , ( ) => {
164164 expect ( changed ) . toBe ( false ) ;
165165 } ) ;
166166
167+ describe ( 'line ending preservation' , ( ) => {
168+ it ( 'preserves CRLF when removing duplicate keys' , ( ) => {
169+ fs . writeFileSync ( envPath , 'A=1\r\nB=2\r\nA=3\r\n' ) ;
170+
171+ applyFixes ( { envPath, missingKeys : [ ] , duplicateKeys : [ 'A' ] } ) ;
172+
173+ const finalContent = fs . readFileSync ( envPath , 'utf-8' ) ;
174+ expect ( finalContent ) . toBe ( 'B=2\r\nA=3\r\n' ) ;
175+ } ) ;
176+
177+ it ( 'preserves LF when removing duplicate keys' , ( ) => {
178+ fs . writeFileSync ( envPath , 'A=1\nB=2\nA=3\n' ) ;
179+
180+ applyFixes ( { envPath, missingKeys : [ ] , duplicateKeys : [ 'A' ] } ) ;
181+
182+ const finalContent = fs . readFileSync ( envPath , 'utf-8' ) ;
183+ expect ( finalContent ) . toBe ( 'B=2\nA=3\n' ) ;
184+ } ) ;
185+
186+ it ( 'preserves CRLF when adding missing keys' , ( ) => {
187+ fs . writeFileSync ( envPath , 'A=1\r\n' ) ;
188+
189+ applyFixes ( { envPath, missingKeys : [ 'B' , 'C' ] , duplicateKeys : [ ] } ) ;
190+
191+ const finalContent = fs . readFileSync ( envPath , 'utf-8' ) ;
192+ expect ( finalContent ) . toBe ( 'A=1\r\nB=\r\nC=\r\n' ) ;
193+ } ) ;
194+
195+ it ( 'preserves LF when adding missing keys' , ( ) => {
196+ fs . writeFileSync ( envPath , 'A=1\n' ) ;
197+
198+ applyFixes ( { envPath, missingKeys : [ 'B' , 'C' ] , duplicateKeys : [ ] } ) ;
199+
200+ const finalContent = fs . readFileSync ( envPath , 'utf-8' ) ;
201+ expect ( finalContent ) . toBe ( 'A=1\nB=\nC=\n' ) ;
202+ } ) ;
203+
204+ it ( 'uses CRLF separator when file has no trailing newline and CRLF style' , ( ) => {
205+ fs . writeFileSync ( envPath , 'A=1\r\nB=2' ) ; // no trailing newline
206+
207+ applyFixes ( { envPath, missingKeys : [ 'C' ] , duplicateKeys : [ ] } ) ;
208+
209+ const finalContent = fs . readFileSync ( envPath , 'utf-8' ) ;
210+ expect ( finalContent ) . toBe ( 'A=1\r\nB=2\r\nC=\r\n' ) ;
211+ } ) ;
212+ } ) ;
213+
167214 describe ( 'ensureGitignore functionality' , ( ) => {
168215 it ( 'creates .gitignore when in git repo but no .gitignore exists' , ( ) => {
169216 const tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'dotenv-diff-' ) ) ;
230277 const gitignorePath = path . join ( tmpDir , '.gitignore' ) ;
231278 fs . writeFileSync ( gitignorePath , '.env\n.env.*\n' ) ;
232279
233- const { changed , result } = applyFixes ( {
280+ const { result } = applyFixes ( {
234281 envPath,
235282 missingKeys : [ ] ,
236283 duplicateKeys : [ ] ,
0 commit comments