forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot-env.test.js
More file actions
97 lines (79 loc) · 3.05 KB
/
Copy pathdot-env.test.js
File metadata and controls
97 lines (79 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const process = require('process')
const test = require('ava')
const sinon = require('sinon')
const { withSiteBuilder } = require('../../tests/utils/site-builder')
const { loadDotEnvFiles } = require('./dot-env')
const warn = sinon.stub()
test('should return an object with empty array for a site with no .env file', async (t) => {
await withSiteBuilder('site-without-env-file', async (builder) => {
await builder.buildAsync()
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [])
})
})
test('should read env vars from .env file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env',
env: { TEST: 'FROM_ENV' },
})
await builder.buildAsync()
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [{ file: '.env', env: { TEST: 'FROM_ENV' } }])
})
})
test('should read env vars from .env.development file', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder.withEnvFile({
path: '.env.development',
env: { TEST: 'FROM_DEVELOPMENT_ENV' },
})
await builder.buildAsync()
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [{ file: '.env.development', env: { TEST: 'FROM_DEVELOPMENT_ENV' } }])
})
})
test('should read from both .env.development and .env', async (t) => {
process.env.NODE_ENV = 'development'
await withSiteBuilder('site-with-envs-file', async (builder) => {
builder
.withEnvFile({
path: '.env',
env: { ONE: 'FROM_ENV', TWO: 'FROM_ENV' },
})
.withEnvFile({
path: '.env.development',
env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' },
})
await builder.buildAsync()
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [
{ file: '.env.development', env: { ONE: 'FROM_DEVELOPMENT_ENV', THREE: 'FROM_DEVELOPMENT_ENV' } },
{ file: '.env', env: { ONE: 'FROM_ENV', TWO: 'FROM_ENV' } },
])
})
})
test('should handle empty .env file', async (t) => {
await withSiteBuilder('site-with-empty-env-file', async (builder) => {
builder.withEnvFile({
path: '.env',
})
await builder.buildAsync()
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [{ file: '.env', env: {} }])
})
})
test('should filter process.env vars', async (t) => {
await withSiteBuilder('site-with-empty-env-file', async (builder) => {
builder.withEnvFile({
path: '.env',
env: { SHOULD_FILTER: 'FROM_ENV', OTHER: 'FROM_ENV' },
})
await builder.buildAsync()
process.env.SHOULD_FILTER = 'FROM_PROCESS_ENV'
const results = await loadDotEnvFiles({ projectDir: builder.directory, warn })
t.deepEqual(results, [{ file: '.env', env: { OTHER: 'FROM_ENV' } }])
})
})