@@ -3,6 +3,7 @@ import fs from 'fs-extra';
33import path from 'path' ;
44
55import { DEFAULT_ENV_PATH , getEnv } from '../../src/shared/index.js' ;
6+ import { ux } from '@oclif/core/ux' ;
67
78jest . mock ( '@oclif/core/ux' , ( ) => ( {
89 ux : {
@@ -14,6 +15,9 @@ jest.mock('fs-extra', () => ({
1415 default : {
1516 pathExists : jest . fn ( ) ,
1617 readFile : jest . fn ( ) ,
18+ stat : jest . fn < ( ) => Promise < { isFile : ( ) => boolean } > > ( ) . mockResolvedValue ( {
19+ isFile : ( ) => true ,
20+ } ) ,
1721 } ,
1822} ) ) ;
1923
@@ -54,6 +58,7 @@ jest.mock('dotenv', () => ({
5458
5559const mockedPathExists = fs . pathExists as unknown as jest . Mock < ( ) => Promise < boolean > > ;
5660const mockedReadFile = fs . readFile as unknown as jest . Mock < ( ) => Promise < string > > ;
61+ const mockedStat = fs . stat as unknown as jest . Mock < ( ) => Promise < { isFile : ( ) => boolean } > > ;
5762const mockedRelative = path . relative as jest . Mock < ( from : string , to : string ) => string > ;
5863
5964describe ( 'getEnv (shared/environment)' , ( ) => {
@@ -159,6 +164,32 @@ describe('getEnv (shared/environment)', () => {
159164 } ) ;
160165 } ) ;
161166
167+ describe ( 'path is a directory' , ( ) => {
168+ it ( 'returns empty env when path is a directory (not a file)' , async ( ) => {
169+ mockedPathExists . mockResolvedValue ( true ) ;
170+ mockedStat . mockResolvedValueOnce ( { isFile : ( ) => false } ) ;
171+ const shouldLog = false ;
172+
173+ const result = await getEnv ( [ ] , shouldLog , 'some-dir' ) ;
174+
175+ expect ( result . env ) . toEqual ( { } ) ;
176+ expect ( result . envFilePath ) . toBe ( 'some-dir' ) ;
177+ expect ( mockedReadFile ) . not . toHaveBeenCalled ( ) ;
178+ } ) ;
179+
180+ it ( 'warns when path is a directory and shouldLog is true' , async ( ) => {
181+ mockedPathExists . mockResolvedValue ( true ) ;
182+ mockedStat . mockResolvedValueOnce ( { isFile : ( ) => false } ) ;
183+ const shouldLog = true ;
184+
185+ await getEnv ( [ ] , shouldLog , '/path/to/dir' ) ;
186+
187+ expect ( ux . warn ) . toHaveBeenCalledWith (
188+ '/path/to/dir is a directory, not a file. Proceeding without loading environment variables.'
189+ ) ;
190+ } ) ;
191+ } ) ;
192+
162193 describe ( 'successful load' , ( ) => {
163194 it ( 'returns parsed env and relative path' , async ( ) => {
164195 mockedPathExists . mockResolvedValue ( true ) ;
0 commit comments