Skip to content

Commit fdd5b68

Browse files
committed
refactor(gen:test): pull 4 helper functions out into a separate module
1 parent 1c91686 commit fdd5b68

File tree

2 files changed

+69
-54
lines changed

2 files changed

+69
-54
lines changed

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

+6-54
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ import fs from 'fs';
44
import _ from 'lodash';
55
import Promise from 'bluebird';
66
Promise.promisifyAll(fs);
7-
import {exec} from 'child_process';
87
import helpers from 'yeoman-test';
98
import assert from 'yeoman-assert';
109
import * as getExpectedFiles from './get-expected-files';
11-
import recursiveReadDir from 'recursive-readdir';
10+
import {
11+
copyAsync,
12+
runCmd,
13+
assertOnlyFiles,
14+
getConfig
15+
} from './test-helpers';
1216

1317
const defaultOptions = {
1418
buildtool: 'grunt',
@@ -28,52 +32,6 @@ const defaultOptions = {
2832
};
2933
const TEST_DIR = __dirname;
3034

31-
function copyAsync(src, dest) {
32-
return fs.readFileAsync(src)
33-
.then(data => fs.writeFileAsync(dest, data));
34-
}
35-
36-
/**
37-
* @callback doneCallback
38-
* @param {null|Error} err
39-
*/
40-
41-
/**
42-
* Run the given command in a child process
43-
* @param {string} cmd - command to run
44-
* @param {doneCallback} done
45-
*/
46-
function runCmd(cmd, done) {
47-
exec(cmd, {}, function(err, stdout, stderr) {
48-
if(err) {
49-
console.error(stdout);
50-
throw new Error(`Error running command: ${cmd}`);
51-
done(err);
52-
} else {
53-
if(DEBUG) console.log(stdout);
54-
done();
55-
}
56-
});
57-
}
58-
59-
function assertOnlyFiles(expectedFiles, topLevelPath='./', skip=['node_modules', 'bower_components']) {
60-
return new Promise((resolve, reject) => {
61-
recursiveReadDir(topLevelPath, skip, function(err, actualFiles) {
62-
if(err) return reject(err);
63-
64-
actualFiles = _.map(actualFiles.concat(), file => path.normalize(file.replace(path.normalize(`${topLevelPath}/`), '')));
65-
expectedFiles = _.map(expectedFiles, file => path.normalize(file));
66-
67-
let extras = _.pullAll(actualFiles, expectedFiles);
68-
69-
if(extras.length !== 0) {
70-
return reject(extras);
71-
}
72-
resolve();
73-
});
74-
});
75-
}
76-
7735
function runGen(prompts) {
7836
return new Promise((resolve, reject) => {
7937
let dir;
@@ -130,12 +88,6 @@ function runEndpointGen(name, opt={}) {
13088
});
13189
}
13290

133-
function getConfig(dir) {
134-
return fs.readFileAsync(path.join(dir, '.yo-rc.json'), 'utf8').then(data => {
135-
return JSON.parse(data);
136-
});
137-
}
138-
13991
describe('angular-fullstack:app', function() {
14092
beforeEach(function() {
14193
this.gen = runGen(defaultOptions);

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

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import _ from 'lodash';
5+
import Promise from 'bluebird';
6+
Promise.promisifyAll(fs);
7+
import {exec} from 'child_process';
8+
import helpers from 'yeoman-test';
9+
import assert from 'yeoman-assert';
10+
import * as getExpectedFiles from './get-expected-files';
11+
import recursiveReadDir from 'recursive-readdir';
12+
13+
export function copyAsync(src, dest) {
14+
return fs.readFileAsync(src)
15+
.then(data => fs.writeFileAsync(dest, data));
16+
}
17+
18+
/**
19+
* @callback doneCallback
20+
* @param {null|Error} err
21+
*/
22+
23+
/**
24+
* Run the given command in a child process
25+
* @param {string} cmd - command to run
26+
* @param {doneCallback} done
27+
*/
28+
export function runCmd(cmd, done) {
29+
exec(cmd, {}, function(err, stdout, stderr) {
30+
if(err) {
31+
console.error(stdout);
32+
throw new Error(`Error running command: ${cmd}`);
33+
done(err);
34+
} else {
35+
if(DEBUG) console.log(stdout);
36+
done();
37+
}
38+
});
39+
}
40+
41+
export function assertOnlyFiles(expectedFiles, topLevelPath='./', skip=['node_modules', 'bower_components']) {
42+
return new Promise((resolve, reject) => {
43+
recursiveReadDir(topLevelPath, skip, function(err, actualFiles) {
44+
if(err) return reject(err);
45+
46+
actualFiles = _.map(actualFiles.concat(), file => path.normalize(file.replace(path.normalize(`${topLevelPath}/`), '')));
47+
expectedFiles = _.map(expectedFiles, file => path.normalize(file));
48+
49+
let extras = _.pullAll(actualFiles, expectedFiles);
50+
51+
if(extras.length !== 0) {
52+
return reject(extras);
53+
}
54+
resolve();
55+
});
56+
});
57+
}
58+
59+
export function getConfig(dir) {
60+
return fs.readFileAsync(path.join(dir, '.yo-rc.json'), 'utf8').then(data => {
61+
return JSON.parse(data);
62+
});
63+
}

0 commit comments

Comments
 (0)