/
options.test.js
204 lines (181 loc) · 5.65 KB
/
options.test.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
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
'use strict';
const path = require('path');
const assert = require('assert');
const os = require('os');
const mm = require('egg-mock');
const parseOptions = require('../lib/utils/options');
const utils = require('./utils');
describe('test/options.test.js', () => {
afterEach(mm.restore);
it('should return undefined by port as default', () => {
const options = parseOptions({});
assert(options.port === undefined);
});
it('should start with https and listen 8443 with https.{key|cert}', () => {
const options = parseOptions({
https: {
key: utils.getFilepath('server.key'),
cert: utils.getFilepath('server.crt'),
},
});
assert(options.port === 8443);
assert(options.https.key);
assert(options.https.cert);
});
it('should start with httpsOptions and listen 8443', () => {
const options = parseOptions({
https: {
passphrase: '123456',
key: utils.getFilepath('server.key'),
cert: utils.getFilepath('server.crt'),
},
});
assert(options.port === 8443);
assert(options.https.key);
assert(options.https.cert);
assert(options.https.passphrase);
});
it('should listen custom port 6001', () => {
const options = parseOptions({
port: '6001',
});
assert(options.port === 6001);
});
it('should set NO_DEPRECATION on production env', () => {
mm(process.env, 'NODE_ENV', 'production');
const options = parseOptions({
workers: 1,
});
assert(options.workers === 1);
assert(process.env.NO_DEPRECATION === '*');
});
it('should not extend when port is null/undefined', () => {
let options = parseOptions({
port: null,
});
assert(options.port === undefined);
options = parseOptions({
port: undefined,
});
assert(options.port === undefined);
});
it('should not call os.cpus when specify workers', () => {
mm.syncError(os, 'cpus', 'should not call os.cpus');
const options = parseOptions({
workers: 1,
});
assert(options.workers === 1);
});
describe('debug', () => {
it('empty', () => {
mm(process, 'execArgv', []);
const options = parseOptions({});
assert(options.isDebug === undefined);
});
it('--inspect', () => {
mm(process, 'execArgv', [ '--inspect=9229' ]);
const options = parseOptions({});
assert(options.isDebug === true);
});
it('--debug', () => {
mm(process, 'execArgv', [ '--debug=5858' ]);
const options = parseOptions({});
assert(options.isDebug === true);
});
});
describe('options', () => {
let app;
before(() => {
app = utils.cluster('apps/options', {
foo: true,
framework: path.dirname(require.resolve('egg')),
});
return app.ready();
});
after(() => app.close());
it('should be passed through', () => {
app.expect('stdout', /app options foo: true/);
app.expect('stdout', /agent options foo: true/);
});
});
describe('framework', () => {
it('should get from absolite path', () => {
const frameworkPath = path.dirname(require.resolve('egg'));
const options = parseOptions({
framework: frameworkPath,
});
assert(options.framework === frameworkPath);
});
it('should get from absolite path but not exist', () => {
const frameworkPath = path.join(__dirname, 'noexist');
try {
parseOptions({
framework: frameworkPath,
});
throw new Error('should not run');
} catch (err) {
assert(err.message === `${frameworkPath} should exist`);
}
});
it('should get from npm package', () => {
const frameworkPath = path.join(__dirname, '../node_modules/egg');
const options = parseOptions({
framework: 'egg',
});
assert(options.framework === frameworkPath);
});
it('should get from npm package but not exist', () => {
try {
parseOptions({
framework: 'noexist',
});
throw new Error('should not run');
} catch (err) {
const frameworkPath = path.join(__dirname, '../node_modules');
assert(err.message === `noexist is not found in ${frameworkPath}`);
}
});
it('should get from pkg.egg.framework', () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-pkg-egg');
const options = parseOptions({
baseDir,
});
assert(options.framework === path.join(baseDir, 'node_modules/yadan'));
});
it('should get from pkg.egg.framework but not exist', () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-pkg-egg-noexist');
try {
parseOptions({
baseDir,
});
throw new Error('should not run');
} catch (err) {
const frameworkPaths = [
path.join(baseDir, 'node_modules'),
path.join(process.cwd(), 'node_modules'),
].join(',');
assert(err.message === `noexist is not found in ${frameworkPaths}`);
}
});
it('should get egg by default', () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-egg-default');
const options = parseOptions({
baseDir,
});
assert(options.framework === path.join(baseDir, 'node_modules/egg'));
});
});
it('should exist when specify baseDir', () => {
it('should get egg by default but not exist', () => {
const baseDir = path.join(__dirname, 'noexist');
try {
parseOptions({
baseDir,
});
throw new Error('should not run');
} catch (err) {
assert(err.message === `${path.join(baseDir, 'package.json')} should exist`);
}
});
});
});