@@ -10,21 +10,29 @@ const validEnvFilePath = '../fixtures/dotenv/valid.env';
10
10
const nodeOptionsEnvFilePath = '../fixtures/dotenv/node-options.env' ;
11
11
12
12
describe ( '.env supports edge cases' , ( ) => {
13
-
14
- it ( 'supports multiple declarations' , async ( ) => {
15
- // process.env.BASIC is equal to `basic` because the second .env file overrides it.
13
+ it ( 'supports multiple declarations, including optional ones' , async ( ) => {
16
14
const code = `
17
15
const assert = require('assert');
18
16
assert.strictEqual(process.env.BASIC, 'basic');
19
17
assert.strictEqual(process.env.NODE_NO_WARNINGS, '1');
20
18
` . trim ( ) ;
21
- const child = await common . spawnPromisified (
22
- process . execPath ,
23
- [ `--env-file=${ nodeOptionsEnvFilePath } ` , `--env-file=${ validEnvFilePath } ` , '--eval' , code ] ,
24
- { cwd : __dirname } ,
25
- ) ;
26
- assert . strictEqual ( child . stderr , '' ) ;
27
- assert . strictEqual ( child . code , 0 ) ;
19
+ const result = await Promise . all ( Array . from ( { length : 4 } , ( _ , i ) =>
20
+ common . spawnPromisified (
21
+ process . execPath ,
22
+ [
23
+ // Bitwise AND to create all 4 possible combinations:
24
+ // i & 0b01 is truthy when i has value 0bx1 (i.e. 0b01 (1) and 0b11 (3)), falsy otherwise.
25
+ // i & 0b10 is truthy when i has value 0b1x (i.e. 0b10 (2) and 0b11 (3)), falsy otherwise.
26
+ `${ i & 0b01 ? '--env-file' : '--env-file-if-exists' } =${ path . resolve ( __dirname , nodeOptionsEnvFilePath ) } ` ,
27
+ `${ i & 0b10 ? '--env-file' : '--env-file-if-exists' } =${ path . resolve ( __dirname , validEnvFilePath ) } ` ,
28
+ '--eval' , code ,
29
+ ] ) ) ) ;
30
+ assert . deepStrictEqual ( result , Array . from ( { length : 4 } , ( ) => ( {
31
+ code : 0 ,
32
+ signal : null ,
33
+ stdout : '' ,
34
+ stderr : '' ,
35
+ } ) ) ) ;
28
36
} ) ;
29
37
30
38
it ( 'supports absolute paths' , async ( ) => {
@@ -52,14 +60,27 @@ describe('.env supports edge cases', () => {
52
60
assert . strictEqual ( child . code , 9 ) ;
53
61
} ) ;
54
62
63
+ it ( 'should handle non-existent optional .env file' , async ( ) => {
64
+ const code = `
65
+ require('assert').strictEqual(1,1);
66
+ ` . trim ( ) ;
67
+ const child = await common . spawnPromisified (
68
+ process . execPath ,
69
+ [ '--env-file-if-exists=.env' , '--eval' , code ] ,
70
+ { cwd : __dirname } ,
71
+ ) ;
72
+ assert . notStrictEqual ( child . stderr , '' ) ;
73
+ assert . strictEqual ( child . code , 0 ) ;
74
+ } ) ;
75
+
55
76
it ( 'should not override existing environment variables but introduce new vars' , async ( ) => {
56
77
const code = `
57
78
require('assert').strictEqual(process.env.BASIC, 'existing');
58
79
require('assert').strictEqual(process.env.AFTER_LINE, 'after_line');
59
80
` . trim ( ) ;
60
81
const child = await common . spawnPromisified (
61
82
process . execPath ,
62
- [ `--env-file=${ validEnvFilePath } ` , '--eval' , code ] ,
83
+ [ `--env-file=${ path . resolve ( __dirname , validEnvFilePath ) } ` , '--eval' , code ] ,
63
84
{ cwd : __dirname , env : { ...process . env , BASIC : 'existing' } } ,
64
85
) ;
65
86
assert . strictEqual ( child . stderr , '' ) ;
@@ -104,9 +125,9 @@ describe('.env supports edge cases', () => {
104
125
process . execPath ,
105
126
[
106
127
'--eval' , `require('assert').strictEqual(process.env.BASIC, undefined);` ,
107
- '--' , '--env-file' , validEnvFilePath ,
128
+ '--' , '--env-file' , path . resolve ( __dirname , validEnvFilePath ) ,
108
129
] ,
109
- { cwd : fixtures . path ( 'dotenv' ) } ,
130
+ { cwd : __dirname } ,
110
131
) ;
111
132
assert . strictEqual ( child . stdout , '' ) ;
112
133
assert . strictEqual ( child . stderr , '' ) ;
0 commit comments