forked from TryGhost/Ghost-CLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-config-spec.js
89 lines (75 loc) · 3.95 KB
/
validate-config-spec.js
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
'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const errors = require('../../../../../lib/errors');
const setupEnv = require('../../../../utils/env');
const advancedOpts = require('../../../../../lib/commands/config/advanced');
const validateConfig = require('../../../../../lib/commands/doctor/checks/validate-config');
describe('Unit: Doctor Checks > validateConfig', function () {
const sandbox = sinon.sandbox.create();
let env;
afterEach(function () {
sandbox.restore();
if (env) {
env.cleanup();
env = null;
}
});
it('skips check, when instance is currently running', function () {
const isRunningStub = sinon.stub().returns(true);
expect(validateConfig.skip({instance: {process: {isRunning: isRunningStub}}}), 'true if current instance is running').to.be.true;
});
it('rejects if environment is passed and no config exists for that environment', function () {
env = setupEnv();
const cwdStub = sandbox.stub(process, 'cwd').returns(env.dir);
return validateConfig.task({system: {environment: 'testing'}}).then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.ConfigError);
expect(error.message).to.match(/Config file is not valid JSON/);
expect(error.options.environment).to.equal('testing');
expect(cwdStub.calledOnce).to.be.true;
});
});
it('rejects if environment is passed and the config file is not valid json', function () {
env = setupEnv({files: [{path: 'config.testing.json', contents: 'not json'}]});
const cwdStub = sandbox.stub(process, 'cwd').returns(env.dir);
return validateConfig.task({system: {environment: 'testing'}}).then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.ConfigError);
expect(error.message).to.match(/Config file is not valid JSON/);
expect(error.options.environment).to.equal('testing');
expect(cwdStub.calledOnce).to.be.true;
});
});
it('rejects with error if config values does not pass', function () {
const config = {server: {port: 2368}};
env = setupEnv({files: [{path: 'config.testing.json', content: config, json: true}]});
const urlStub = sandbox.stub(advancedOpts.url, 'validate').returns('Invalid URL');
const portStub = sandbox.stub(advancedOpts.port, 'validate').returns('Port is in use');
sandbox.stub(process, 'cwd').returns(env.dir);
return validateConfig.task({system: {environment: 'testing'}}).then(() => {
expect(false, 'error should have been thrown').to.be.true;
}).catch((error) => {
expect(error).to.be.an.instanceof(errors.ConfigError);
expect(error.message).to.equal('Port is in use');
expect(error.options.config).to.deep.equal({'server.port': 2368});
expect(urlStub.called).to.be.false;
expect(portStub.calledOnce).to.be.true;
expect(portStub.calledWithExactly(2368)).to.be.true;
});
});
it('passes if all validate functions return true', function () {
const config = {server: {port: 2368}};
const env = setupEnv({files: [{path: 'config.testing.json', content: config, json: true}]});
const urlStub = sandbox.stub(advancedOpts.url, 'validate').returns(true);
const portStub = sandbox.stub(advancedOpts.port, 'validate').returns(true);
sandbox.stub(process, 'cwd').returns(env.dir);
return validateConfig.task({system: {environment: 'testing'}}).then(() => {
expect(urlStub.called).to.be.false;
expect(portStub.calledOnce).to.be.true;
expect(portStub.calledWithExactly(2368)).to.be.true;
});
});
});