@@ -12,9 +12,11 @@ describe('writeEnvLocal', () => {
1212 } ) ;
1313
1414 afterEach ( ( ) => {
15- const envPath = join ( testDir , '.env.local' ) ;
16- if ( existsSync ( envPath ) ) {
17- unlinkSync ( envPath ) ;
15+ for ( const file of [ '.env.local' , '.gitignore' ] ) {
16+ const filePath = join ( testDir , file ) ;
17+ if ( existsSync ( filePath ) ) {
18+ unlinkSync ( filePath ) ;
19+ }
1820 }
1921 } ) ;
2022
@@ -133,4 +135,82 @@ describe('writeEnvLocal', () => {
133135 const content = readFileSync ( join ( testDir , '.env.local' ) , 'utf-8' ) ;
134136 expect ( content ) . toContain ( 'WORKOS_API_KEY=sk_test_123' ) ;
135137 } ) ;
138+
139+ describe ( 'gitignore handling' , ( ) => {
140+ const envVars = {
141+ WORKOS_CLIENT_ID : 'client_123' ,
142+ WORKOS_REDIRECT_URI : 'http://localhost:3000/callback' ,
143+ } ;
144+
145+ it ( 'creates .gitignore with .env.local when no .gitignore exists' , ( ) => {
146+ writeEnvLocal ( testDir , envVars ) ;
147+
148+ const gitignorePath = join ( testDir , '.gitignore' ) ;
149+ expect ( existsSync ( gitignorePath ) ) . toBe ( true ) ;
150+ expect ( readFileSync ( gitignorePath , 'utf-8' ) ) . toBe ( '.env.local\n' ) ;
151+ } ) ;
152+
153+ it ( 'appends .env.local to existing .gitignore that does not include it' , ( ) => {
154+ const gitignorePath = join ( testDir , '.gitignore' ) ;
155+ writeFileSync ( gitignorePath , 'node_modules\ndist\n' ) ;
156+
157+ writeEnvLocal ( testDir , envVars ) ;
158+
159+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
160+ expect ( content ) . toContain ( 'node_modules\n' ) ;
161+ expect ( content ) . toContain ( '.env.local\n' ) ;
162+ } ) ;
163+
164+ it ( 'does not duplicate .env.local if already present' , ( ) => {
165+ const gitignorePath = join ( testDir , '.gitignore' ) ;
166+ writeFileSync ( gitignorePath , 'node_modules\n.env.local\n' ) ;
167+
168+ writeEnvLocal ( testDir , envVars ) ;
169+
170+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
171+ const matches = content . match ( / \. e n v \. l o c a l / g) ;
172+ expect ( matches ) . toHaveLength ( 1 ) ;
173+ } ) ;
174+
175+ it ( 'does not add .env.local if .env*.local pattern exists' , ( ) => {
176+ const gitignorePath = join ( testDir , '.gitignore' ) ;
177+ writeFileSync ( gitignorePath , 'node_modules\n.env*.local\n' ) ;
178+
179+ writeEnvLocal ( testDir , envVars ) ;
180+
181+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
182+ expect ( content ) . not . toContain ( '\n.env.local\n' ) ;
183+ } ) ;
184+
185+ it ( 'does not add .env.local if .env* pattern exists' , ( ) => {
186+ const gitignorePath = join ( testDir , '.gitignore' ) ;
187+ writeFileSync ( gitignorePath , '.env*\n' ) ;
188+
189+ writeEnvLocal ( testDir , envVars ) ;
190+
191+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
192+ expect ( content ) . toBe ( '.env*\n' ) ;
193+ } ) ;
194+
195+ it ( 'preserves existing .gitignore content when appending' , ( ) => {
196+ const gitignorePath = join ( testDir , '.gitignore' ) ;
197+ const original = 'node_modules\ndist\n.DS_Store\n' ;
198+ writeFileSync ( gitignorePath , original ) ;
199+
200+ writeEnvLocal ( testDir , envVars ) ;
201+
202+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
203+ expect ( content ) . toBe ( original + '.env.local\n' ) ;
204+ } ) ;
205+
206+ it ( 'handles .gitignore without trailing newline' , ( ) => {
207+ const gitignorePath = join ( testDir , '.gitignore' ) ;
208+ writeFileSync ( gitignorePath , 'node_modules' ) ;
209+
210+ writeEnvLocal ( testDir , envVars ) ;
211+
212+ const content = readFileSync ( gitignorePath , 'utf-8' ) ;
213+ expect ( content ) . toBe ( 'node_modules\n.env.local\n' ) ;
214+ } ) ;
215+ } ) ;
136216} ) ;
0 commit comments