Skip to content

Commit 006d56f

Browse files
committed
refactor(gen:test): move runGen to test-helpers, rename getConfig to readJSON
1 parent 15d3656 commit 006d56f

File tree

3 files changed

+82
-84
lines changed

3 files changed

+82
-84
lines changed

Diff for: src/test/endpoint.test.js

+3-33
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
copyAsync,
1515
runCmd,
1616
assertOnlyFiles,
17-
getConfig
17+
readJSON,
18+
runGen
1819
} from './test-helpers';
1920

2021
const TEST_DIR = __dirname;
@@ -35,37 +36,6 @@ const defaultOptions = {
3536
oauth: [],
3637
socketio: true
3738
};
38-
function runGen(prompts) {
39-
return new Promise((resolve, reject) => {
40-
let dir;
41-
helpers
42-
.run(require.resolve('../generators/app'))
43-
.inTmpDir(function(_dir) {
44-
// this will create a new temporary directory for each new generator run
45-
var done = this.async();
46-
if(DEBUG) console.log(`TEMP DIR: ${_dir}`);
47-
dir = _dir;
48-
49-
// symlink our dependency directories
50-
return Promise.all([
51-
fs.mkdirAsync(dir + '/client').then(() => {
52-
return fs.symlinkAsync(__dirname + '/fixtures/bower_components', dir + '/client/bower_components');
53-
}),
54-
fs.symlinkAsync(__dirname + '/fixtures/node_modules', dir + '/node_modules')
55-
]).then(done);
56-
})
57-
.withGenerators([
58-
require.resolve('../generators/endpoint'),
59-
// [helpers.createDummyGenerator(), 'ng-component:app']
60-
])
61-
.withOptions({
62-
skipInstall: true
63-
})
64-
.withPrompts(prompts)
65-
.on('error', reject)
66-
.on('end', () => resolve(dir));
67-
});
68-
}
6939

7040
function runEndpointGen(name, opt={}) {
7141
let prompts = opt.prompts || {};
@@ -162,7 +132,7 @@ before(function() {
162132
jscs.configure(JSON.parse(data));
163133
});
164134
}),
165-
getConfig(path.join(TEST_DIR, 'fixtures/.yo-rc.json')).then(_config => {
135+
readJSON(path.join(TEST_DIR, 'fixtures/.yo-rc.json')).then(_config => {
166136
_config['generator-angular-fullstack'].insertRoutes = false;
167137
_config['generator-angular-fullstack'].pluralizeRoutes = false;
168138
_config['generator-angular-fullstack'].insertSockets = false;

Diff for: src/test/main.test.js

+9-50
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
copyAsync,
1111
runCmd,
1212
assertOnlyFiles,
13-
getConfig
13+
readJSON,
14+
runGen
1415
} from './test-helpers';
1516

1617
const defaultOptions = {
@@ -30,48 +31,6 @@ const defaultOptions = {
3031
};
3132
const TEST_DIR = __dirname;
3233

33-
function runGen(prompts, opts={}) {
34-
let options = opts.options || {skipInstall: true};
35-
36-
return new Promise((resolve, reject) => {
37-
let dir;
38-
let gen = helpers
39-
.run(require.resolve('../generators/app'))
40-
.inTmpDir(function(_dir) {
41-
// this will create a new temporary directory for each new generator run
42-
var done = this.async();
43-
if(DEBUG) console.log(`TEMP DIR: ${_dir}`);
44-
dir = _dir;
45-
46-
let promises = [
47-
fs.mkdirAsync(dir + '/client').then(() => {
48-
return fs.symlinkAsync(__dirname + '/fixtures/bower_components', dir + '/client/bower_components');
49-
}),
50-
fs.symlinkAsync(__dirname + '/fixtures/node_modules', dir + '/node_modules')
51-
];
52-
53-
if(opts.copyConfigFile) {
54-
promises.push(copyAsync(path.join(TEST_DIR, 'fixtures/.yo-rc.json'), path.join(dir, '.yo-rc.json')));
55-
}
56-
57-
// symlink our dependency directories
58-
return Promise.all(promises).then(done);
59-
})
60-
.withGenerators([
61-
require.resolve('../generators/endpoint'),
62-
// [helpers.createDummyGenerator(), 'ng-component:app']
63-
])
64-
// .withArguments(['upperCaseBug'])
65-
.withOptions(options);
66-
67-
if(prompts) gen.withPrompts(prompts);
68-
69-
gen
70-
.on('error', reject)
71-
.on('end', () => resolve(dir));
72-
});
73-
}
74-
7534
function runEndpointGen(name, opt={}) {
7635
let prompts = opt.prompts || {};
7736
let options = opt.options || {};
@@ -129,7 +88,7 @@ describe('angular-fullstack:app', function() {
12988

13089
describe('with a generated endpoint', function() {
13190
beforeEach(function() {
132-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
91+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
13392
return runEndpointGen('foo', {config: config['generator-angular-fullstack']});
13493
});
13594
});
@@ -141,7 +100,7 @@ describe('angular-fullstack:app', function() {
141100

142101
describe('with a generated capitalized endpoint', function() {
143102
beforeEach(function() {
144-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
103+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
145104
return runEndpointGen('Foo', {config: config['generator-angular-fullstack']});
146105
});
147106
});
@@ -153,7 +112,7 @@ describe('angular-fullstack:app', function() {
153112

154113
describe('with a generated path name endpoint', function() {
155114
beforeEach(function() {
156-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
115+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
157116
return runEndpointGen('foo/bar', {config: config['generator-angular-fullstack']});
158117
});
159118
});
@@ -165,7 +124,7 @@ describe('angular-fullstack:app', function() {
165124

166125
describe('with a generated snake-case endpoint', function() {
167126
beforeEach(function() {
168-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
127+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
169128
return runEndpointGen('foo-bar', {config: config['generator-angular-fullstack']});
170129
});
171130
});
@@ -287,7 +246,7 @@ describe('angular-fullstack:app', function() {
287246

288247
describe('with a generated endpoint', function() {
289248
beforeEach(function() {
290-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
249+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
291250
return runEndpointGen('foo', {config: config['generator-angular-fullstack']});
292251
});
293252
});
@@ -363,7 +322,7 @@ describe('angular-fullstack:app', function() {
363322

364323
describe('with a generated endpoint', function() {
365324
beforeEach(function() {
366-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
325+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
367326
return runEndpointGen('foo', {config: config['generator-angular-fullstack']});
368327
});
369328
});
@@ -440,7 +399,7 @@ describe('angular-fullstack:app', function() {
440399

441400
describe('with a generated endpoint', function() {
442401
beforeEach(function() {
443-
return getConfig(path.join(dir, '.yo-rc.json')).then(config => {
402+
return readJSON(path.join(dir, '.yo-rc.json')).then(config => {
444403
return runEndpointGen('foo', {config: config['generator-angular-fullstack']});
445404
});
446405
});

Diff for: src/test/test-helpers.js

+70-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import helpers from 'yeoman-test';
88
import assert from 'yeoman-assert';
99
import recursiveReadDir from 'recursive-readdir';
1010

11+
const TEST_DIR = __dirname;
12+
13+
/**
14+
* Copy file from src to dest
15+
* @param {string} src
16+
* @param {string} dest
17+
* @returns {Promise}
18+
*/
1119
export function copyAsync(src, dest) {
1220
return fs.readFileAsync(src)
1321
.then(data => fs.writeFileAsync(dest, data));
@@ -37,6 +45,13 @@ export function runCmd(cmd) {
3745
});
3846
}
3947

48+
/**
49+
* Assert that only the expected files are present
50+
* @param {string[]} expectedFiles - array of expected files
51+
* @param {string} [topLevelPath='./'] - root dir of expected files to recursively search
52+
* @param {string[]} [skip=['node_modules','bower_components']] - files/folders recursiveReadDir should skip
53+
* @returns {Promise}
54+
*/
4055
export function assertOnlyFiles(expectedFiles, topLevelPath='./', skip=['node_modules', 'bower_components']) {
4156
return new Promise((resolve, reject) => {
4257
recursiveReadDir(topLevelPath, skip, function(err, actualFiles) {
@@ -55,8 +70,62 @@ export function assertOnlyFiles(expectedFiles, topLevelPath='./', skip=['node_mo
5570
});
5671
}
5772

58-
export function getConfig(path) {
73+
/**
74+
* Read JSON from a file
75+
* @param {string} path
76+
* @returns {Promise} - parsed JSON
77+
*/
78+
export function readJSON(path) {
5979
return fs.readFileAsync(path, 'utf8').then(data => {
6080
return JSON.parse(data);
6181
});
6282
}
83+
84+
/**
85+
* Run angular-fullstack:app
86+
* @param {object} [prompts]
87+
* @param {object} [opts={}]
88+
* @param {boolean} [opts.copyConfigFile] - copy default .yo-rc.json
89+
* @returns {Promise}
90+
*/
91+
export function runGen(prompts, opts={}) {
92+
let options = opts.options || {skipInstall: true};
93+
94+
return new Promise((resolve, reject) => {
95+
let dir;
96+
let gen = helpers
97+
.run(require.resolve('../generators/app'))
98+
.inTmpDir(function(_dir) {
99+
// this will create a new temporary directory for each new generator run
100+
var done = this.async();
101+
if(DEBUG) console.log(`TEMP DIR: ${_dir}`);
102+
dir = _dir;
103+
104+
let promises = [
105+
fs.mkdirAsync(dir + '/client').then(() => {
106+
return fs.symlinkAsync(__dirname + '/fixtures/bower_components', dir + '/client/bower_components');
107+
}),
108+
fs.symlinkAsync(__dirname + '/fixtures/node_modules', dir + '/node_modules')
109+
];
110+
111+
if(opts.copyConfigFile) {
112+
promises.push(copyAsync(path.join(TEST_DIR, 'fixtures/.yo-rc.json'), path.join(dir, '.yo-rc.json')));
113+
}
114+
115+
// symlink our dependency directories
116+
return Promise.all(promises).then(done);
117+
})
118+
.withGenerators([
119+
require.resolve('../generators/endpoint'),
120+
// [helpers.createDummyGenerator(), 'ng-component:app']
121+
])
122+
// .withArguments(['upperCaseBug'])
123+
.withOptions(options);
124+
125+
if(prompts) gen.withPrompts(prompts);
126+
127+
gen
128+
.on('error', reject)
129+
.on('end', () => resolve(dir));
130+
});
131+
}

0 commit comments

Comments
 (0)