Repository navigation
Expand file tree
/
Copy pathloader.test.js
More file actions
282 lines (232 loc) · 10.8 KB
/
Copy pathloader.test.js
File metadata and controls
282 lines (232 loc) · 10.8 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('path');
const _ = require('lodash');
const configUtils = require('../../../utils/config-utils');
const sinon = require('sinon');
// Captured before any test changes the environment.
const envAtLoad = process.env;
const envValuesAtLoad = { ...process.env };
describe('Config Loader', function () {
beforeAll(async function () {
await configUtils.restore();
});
afterEach(async function () {
await configUtils.restore();
});
describe('hierarchy of config channels', function () {
let originalEnv;
let originalArgv;
let customConfig;
let loader;
let tmpDir;
function writeSecret(contents) {
const filePath = path.join(tmpDir, 'secret');
fs.writeFileSync(filePath, contents);
return filePath;
}
beforeEach(function () {
originalEnv = { ...process.env };
originalArgv = _.clone(process.argv);
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ghost-loader-'));
loader = require('../../../../core/shared/config/loader');
// getNodeEnv() reads process.env.NODE_ENV, so drive that directly
process.env.NODE_ENV = 'testing';
// we manually call `loadConf` in the tests and we need to ensure that the minimum
// required config properties are available
process.env.paths__contentPath = 'content/';
// Remove any nconf-style env vars that could interfere with
// config hierarchy assertions (e.g. logging__level set by CI)
delete process.env.logging__level;
});
afterEach(function () {
// Restore in place: Vitest retains the original env object for
// vi.stubEnv cleanup, so replacing it would break later tests.
for (const key of Object.keys(process.env)) {
if (!(key in originalEnv)) {
delete process.env[key];
}
}
Object.assign(process.env, originalEnv);
process.argv = originalArgv;
fs.rmSync(tmpDir, { recursive: true, force: true });
sinon.restore();
});
// not database:client, which the schema restricts
it('env parameter is stronger than file', function () {
process.env.database__connection__filename = '/from-env.db';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('database:connection:filename'), '/from-env.db');
});
it('argv is stronger than env parameter', function () {
process.env.database__connection__filename = '/from-env.db';
process.argv[2] = '--database:connection:filename=/from-argv.db';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('database:connection:filename'), '/from-argv.db');
});
it('secret file is stronger than file', function () {
process.env.logging__level_FILE = writeSecret('warn\n');
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('logging:level'), 'warn');
});
it('argv is stronger than a secret file', function () {
process.env.logging__level_FILE = writeSecret('warn\n');
process.argv[2] = '--logging:level=stronger';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('logging:level'), 'stronger');
});
it('does not leak the secret file path into config', function () {
process.env.database__connection__password_FILE = writeSecret('hunter2\n');
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('database:connection:password_FILE'), undefined);
});
it('throws if a value and its secret file are both set', function () {
process.env.logging__level = 'warn';
process.env.logging__level_FILE = writeSecret('error\n');
assert.throws(
() =>
loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
}),
/Cannot set both logging__level and logging__level_FILE/,
);
});
it('argv or env is NOT stronger than overrides', function () {
process.env.paths__corePath = 'try-to-override';
process.argv[2] = '--paths:corePath=try-to-override';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert(!customConfig.get('paths:corePath').includes('try-to-override'));
});
it('overrides is stronger than every other config file', function () {
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert(!customConfig.get('paths:corePath').includes('try-to-override'));
assert.equal(customConfig.get('database:client'), 'better-sqlite3');
assert.equal(customConfig.get('database:debug'), true);
// Note: url is now set via process.env in test/utils/vitest-setup-db.ts for dynamic port allocation
assert.equal(customConfig.get('logging:level'), 'error');
assert.deepEqual(customConfig.get('logging:transports'), ['stdout']);
});
it('should load JSONC files', function () {
process.env.NODE_ENV = 'development';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('site_uuid'), 'a58fe20c-0af0-4fc6-9b1a-20873d5b7d03');
assert.equal(customConfig.get('commented'), undefined);
});
it('ignores a non-object that would replace an object from a lower store', function () {
const warn = sinon.stub(console, 'warn');
const customConfigPath = path.join(tmpDir, 'config.testing.json');
fs.writeFileSync(customConfigPath, JSON.stringify({ admin: 'https://example.com' }));
process.env.admin__sessionMaxAgeMs = '1000';
customConfig = loader.loadNconf({ customConfigPath: tmpDir });
assert.deepEqual(customConfig.get('admin'), { redirects: true, sessionMaxAgeMs: 1000 });
sinon.assert.calledOnceWithExactly(
warn,
`Ghost config: ignoring \`admin\` in ${customConfigPath}: it is a string, but Ghost expects an object there.`,
);
});
it('leaves a relative sqlite filename relative to the working directory', function () {
process.env.database__client = 'sqlite3';
process.env.database__connection__filename = 'content/data/ghost.db';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(customConfig.get('database:client'), 'better-sqlite3');
assert.equal(customConfig.get('database:connection:filename'), 'content/data/ghost.db');
});
it('resolves the bundled sqlite filename against the package root', function () {
process.env.NODE_ENV = 'development';
customConfig = loader.loadNconf({ customConfigPath: tmpDir });
assert.equal(
customConfig.get('database:connection:filename'),
path.join(path.resolve(__dirname, '../../../../'), 'content/data/ghost-dev.db'),
);
});
it('resolves relative paths from any source against the package root', function () {
process.env.paths__contentPath = 'content/';
customConfig = loader.loadNconf({
baseConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
customConfigPath: path.join(__dirname, '../../../utils/fixtures/config'),
});
assert.equal(
customConfig.get('paths:contentPath'),
path.join(path.resolve(__dirname, '../../../../'), 'content/'),
);
});
});
// Runs after the hierarchy tests above, which set, change and delete keys.
describe('hierarchy fixture cleanup', function () {
it('keeps the same process.env object and restores its keys and values', function () {
assert.equal(process.env, envAtLoad);
assert.deepEqual({ ...process.env }, envValuesAtLoad);
});
});
describe('Index', function () {
it('should have exactly the right keys', function () {
const pathConfig = configUtils.config.get('paths');
// This will fail if there are any extra keys
// NOTE: using `Object.keys` here instead of `should.have.keys` assertion
// because when `have.keys` fails there's no useful diff
// and it doesn't make sure to check for "extra" keys
// Sorted, because membership is the point: zod emits the keys the schema
// names first, in declaration order, so adding one to ./schema.ts would
// otherwise break this on ordering alone.
assert.deepEqual(
Object.keys(pathConfig).sort(),
[
'contentPath',
'fixtures',
'defaultSettings',
'assetSrc',
'appRoot',
'corePath',
'adminAssets',
'helperTemplates',
'defaultViews',
'defaultRouteSettings',
'internalAppPath',
'internalAdaptersPath',
'migrationPath',
'publicFilePath',
].sort(),
);
});
it('should have the correct values for each key', function () {
const pathConfig = configUtils.config.get('paths');
const appRoot = path.resolve(__dirname, '../../../../');
assert.equal(pathConfig.appRoot, appRoot);
});
it('should allow specific properties to be user defined', function () {
const contentPath = path.join(configUtils.config.get('paths').appRoot, 'otherContent', '/');
configUtils.set('paths:contentPath', contentPath);
assert.equal(configUtils.config.get('paths').contentPath, contentPath);
assert.equal(configUtils.config.getContentPath('images'), contentPath + 'images/');
});
});
});