Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit b5d3972

Browse files
author
Bobby Earl
authored
Exposing the platform flag. Looking for external config. (#297)
* Exposing the platform flag. Looking for external config. * Using outPath() instead of __dirname. Defaulting config-resolver to karma * Adjusted tests + adding some logging * Fixed linting * Added mock.stopAll()
1 parent f3e1573 commit b5d3972

10 files changed

+222
-44
lines changed

cli/e2e.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,14 @@ const protractorLauncher = require('protractor/built/launcher');
1111
const build = require('./build');
1212
const server = require('./utils/server');
1313
const logger = require('../utils/logger');
14+
const configResolver = require('./utils/config-resolver');
1415

1516
// Disable this to quiet the output
1617
const spawnOptions = { stdio: 'inherit' };
1718

1819
let seleniumServer;
1920
let start;
2021

21-
/**
22-
* Function to get the protractorConfigPath
23-
* @name getProtractorConfigPath
24-
* @returns {string} protractorConfigPath
25-
*/
26-
function getProtractorConfigPath() {
27-
return path.resolve(
28-
__dirname,
29-
'..',
30-
'config',
31-
'protractor',
32-
'protractor.conf.js'
33-
);
34-
}
35-
3622
/**
3723
* Handles killing off the selenium and webpack servers.
3824
* @name killServers
@@ -63,9 +49,9 @@ function killServers(exitCode) {
6349
* Perhaps this should be API driven?
6450
* @name spawnProtractor
6551
*/
66-
function spawnProtractor(chunks, port, skyPagesConfig) {
52+
function spawnProtractor(configPath, chunks, port, skyPagesConfig) {
6753
logger.info('Running Protractor');
68-
protractorLauncher.init(getProtractorConfigPath(), {
54+
protractorLauncher.init(configPath, {
6955
params: {
7056
localUrl: `https://localhost:${port}`,
7157
chunks: chunks,
@@ -79,8 +65,8 @@ function spawnProtractor(chunks, port, skyPagesConfig) {
7965
* Spawns the selenium server if directConnect is not enabled.
8066
* @name spawnSelenium
8167
*/
82-
function spawnSelenium() {
83-
const config = require(getProtractorConfigPath()).config;
68+
function spawnSelenium(configPath) {
69+
const config = require(configPath).config;
8470

8571
return new Promise((resolve, reject) => {
8672
logger.info('Spawning selenium...');
@@ -157,12 +143,13 @@ function spawnBuild(argv, skyPagesConfig, webpack) {
157143
* Assumes build was ran.
158144
* @name e2e
159145
*/
160-
function e2e(argv, skyPagesConfig, webpack) {
146+
function e2e(command, argv, skyPagesConfig, webpack) {
161147
start = new Date().getTime();
162148
process.on('SIGINT', killServers);
163149

164150
const specsPath = path.resolve(process.cwd(), 'e2e/**/*.e2e-spec.ts');
165151
const specsGlob = glob.sync(specsPath);
152+
const configPath = configResolver.resolve(command, argv);
166153

167154
if (specsGlob.length === 0) {
168155
logger.info('No spec files located. Stopping command from running.');
@@ -184,11 +171,12 @@ function e2e(argv, skyPagesConfig, webpack) {
184171
.all([
185172
spawnBuild(argv, skyPagesConfig, webpack),
186173
port,
187-
spawnSelenium()
174+
spawnSelenium(configPath)
188175
]);
189176
})
190177
.then(values => {
191178
spawnProtractor(
179+
configPath,
192180
values[0],
193181
values[1],
194182
skyPagesConfig

cli/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ function test(command, argv) {
99
const logger = require('../utils/logger');
1010
const Server = require('karma').Server;
1111
const tsLinter = require('./utils/ts-linter');
12-
const skyPagesConfigUtil = require('../config/sky-pages/sky-pages.config');
12+
const configResolver = require('./utils/config-resolver');
1313

1414
argv = argv || process.argv;
1515
argv.command = command;
1616

1717
const karmaConfigUtil = require('karma').config;
18-
const karmaConfigPath = skyPagesConfigUtil.outPath(`config/karma/${command}.karma.conf.js`);
18+
const karmaConfigPath = configResolver.resolve(command, argv);
1919
const karmaConfig = karmaConfigUtil.parseConfig(karmaConfigPath);
2020

2121
let lintResult;

cli/utils/config-resolver.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*jslint node: true */
2+
'use strict';
3+
4+
const path = require('path');
5+
const glob = require('glob');
6+
const fs = require('fs-extra');
7+
const logger = require('winston');
8+
9+
const skyPagesConfigUtil = require('../../config/sky-pages/sky-pages.config');
10+
11+
function getPath(command, platform, root, dir) {
12+
let filename;
13+
switch (command) {
14+
case 'e2e':
15+
case 'visual':
16+
filename = `config/protractor/protractor.conf.js`;
17+
break;
18+
19+
// Defaulting to karma so dev-runtime and src-app can be passed in via our test suite.
20+
default:
21+
filename = `config/karma/${command}.karma.conf.js`;
22+
break;
23+
}
24+
25+
return path.join(root, dir, platform, filename);
26+
}
27+
28+
function resolve(command, argv) {
29+
30+
const platform = argv.platform || '';
31+
const internal = getPath(
32+
command,
33+
platform,
34+
skyPagesConfigUtil.outPath(),
35+
''
36+
);
37+
38+
// Using glob so we can find skyux-builder-config regardless of npm install location
39+
let external = glob.sync(getPath(
40+
command,
41+
platform,
42+
process.cwd(),
43+
'node_modules/**/skyux-builder-config/'
44+
));
45+
46+
let config;
47+
if (external.length > 1) {
48+
logger.warn(`Found multiple external config files.`);
49+
external = external.slice(0, 1);
50+
}
51+
52+
if (external.length === 1) {
53+
logger.info(`Using external config ${external[0]}`);
54+
config = external[0];
55+
} else if (fs.existsSync(internal)) {
56+
logger.info(`Using internal config ${internal}`);
57+
config = internal;
58+
} else {
59+
logger.error('Error locating a config file.');
60+
process.exit(1);
61+
}
62+
63+
return config;
64+
}
65+
66+
module.exports = {
67+
resolve
68+
};

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
require('./cli/build-public-library')(skyPagesConfig, webpack);
3131
break;
3232
case 'e2e':
33-
require('./cli/e2e')(argv, skyPagesConfig, webpack);
33+
require('./cli/e2e')(command, argv, skyPagesConfig, webpack);
3434
break;
3535
case 'serve':
3636
require('./cli/serve')(argv, skyPagesConfig, webpack, WebpackDevServer);

loader/sky-assets/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*jshint node: true*/
22
'use strict';
33

4-
const fs = require('fs');
4+
const fs = require('fs-extra');
55
const loaderUtils = require('loader-utils');
66

77
const assetsProcessor = require('../../lib/assets-processor');

test/cli-e2e.spec.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ describe('cli e2e', () => {
7777
}
7878
});
7979

80+
mock('../cli/utils/config-resolver', {
81+
resolve: () => configPath
82+
});
83+
8084
spyOn(process, 'on').and.callFake((evt, cb) => {
8185
if (evt === 'exit') {
8286
PROTRACTOR_CB = cb;
@@ -100,7 +104,7 @@ describe('cli e2e', () => {
100104
});
101105

102106
EXIT_CODE = 1;
103-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
107+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
104108
});
105109

106110
it('should catch protractor kitchen sink error', (done) => {
@@ -112,7 +116,7 @@ describe('cli e2e', () => {
112116
});
113117

114118
EXIT_CODE = 199;
115-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
119+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
116120
});
117121

118122
it('should install, start, and kill selenium only if a seleniumAddress is specified', (done) => {
@@ -141,7 +145,7 @@ describe('cli e2e', () => {
141145
done();
142146
});
143147

144-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
148+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
145149
});
146150

147151
it('should catch build failures', (done) => {
@@ -152,10 +156,11 @@ describe('cli e2e', () => {
152156
done();
153157
});
154158

155-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
159+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
156160
});
157161

158162
it('should catch selenium failures', (done) => {
163+
159164
mock(configPath, {
160165
config: {
161166
seleniumAddress: 'asdf'
@@ -176,13 +181,10 @@ describe('cli e2e', () => {
176181
done();
177182
});
178183

179-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
184+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
180185
});
181186

182187
it('should catch protractor\'s selenium failures', (done) => {
183-
mock(configPath, {
184-
config: {}
185-
});
186188

187189
mock('cross-spawn', {
188190
spawn: () => {
@@ -202,7 +204,7 @@ describe('cli e2e', () => {
202204
done();
203205
});
204206

205-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
207+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
206208
});
207209

208210
it('should not continue if no e2e spec files exist', (done) => {
@@ -216,14 +218,14 @@ describe('cli e2e', () => {
216218
done();
217219
});
218220

219-
mock.reRequire('../cli/e2e')(ARGV, SKY_PAGES_CONFIG, WEBPACK);
221+
mock.reRequire('../cli/e2e')('e2e', ARGV, SKY_PAGES_CONFIG, WEBPACK);
220222
});
221223

222224
it('should accept the --no-build flag and handle errors', (done) => {
223225

224226
spyOn(fs, 'existsSync').and.returnValue(false);
225227

226-
mock.reRequire('../cli/e2e')({ build: false }, SKY_PAGES_CONFIG, WEBPACK);
228+
mock.reRequire('../cli/e2e')('e2e', { build: false }, SKY_PAGES_CONFIG, WEBPACK);
227229
spyOn(process, 'exit').and.callFake(() => {
228230
const calls = logger.info.calls.allArgs();
229231
const message = `Unable to skip build step. "dist/metadata.json" not found.`;
@@ -239,7 +241,7 @@ describe('cli e2e', () => {
239241
spyOn(fs, 'existsSync').and.returnValue(true);
240242
const fsSpy = spyOn(fs, 'readJsonSync').and.returnValue(metadata);
241243

242-
mock.reRequire('../cli/e2e')({ build: false }, SKY_PAGES_CONFIG, WEBPACK);
244+
mock.reRequire('../cli/e2e')('e2e', { build: false }, SKY_PAGES_CONFIG, WEBPACK);
243245
spyOn(process, 'exit').and.callFake(exitCode => {
244246
expect(fsSpy).toHaveBeenCalledWith('dist/metadata.json');
245247
expect(PROTRACTOR_CONFIG_ARGS.params.chunks).toEqual({

test/cli-test.spec.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const logger = require('../utils/logger');
77
describe('cli test', () => {
88
let originalArgv = process.argv;
99

10-
function MockServer(config, onExit) {}
10+
function MockServer() { }
11+
1112
MockServer.prototype.on = function () {};
1213
MockServer.prototype.start = function () {};
1314

@@ -16,6 +17,7 @@ describe('cli test', () => {
1617
spyOn(process, 'exit').and.returnValue();
1718
spyOn(logger, 'info').and.returnValue();
1819
spyOn(logger, 'error').and.returnValue();
20+
1921
mock('../cli/utils/ts-linter', {
2022
lintSync: () => {
2123
return {
@@ -24,9 +26,14 @@ describe('cli test', () => {
2426
};
2527
}
2628
});
29+
2730
mock('../config/sky-pages/sky-pages.config', {
2831
outPath: (path) => path
2932
});
33+
34+
mock('../cli/utils/config-resolver', {
35+
resolve: (command) => `${command}-config.js`
36+
});
3037
});
3138

3239
afterEach(() => {
@@ -44,7 +51,7 @@ describe('cli test', () => {
4451
});
4552
const test = mock.reRequire('../cli/test');
4653
test('test', {});
47-
expect(_configPath.indexOf('/test.karma.conf.js') > -1).toEqual(true);
54+
expect(_configPath).toEqual('test-config.js');
4855
});
4956

5057
it('should load the watch config when running watch command', () => {
@@ -57,7 +64,7 @@ describe('cli test', () => {
5764
});
5865
const test = mock.reRequire('../cli/test');
5966
test('watch');
60-
expect(_configPath.indexOf('/watch.karma.conf.js') > -1).toEqual(true);
67+
expect(_configPath).toEqual('watch-config.js');
6168
});
6269

6370
it('should save the current command to argv', () => {

0 commit comments

Comments
 (0)