Skip to content

Commit d1bc291

Browse files
author
Robert Jackson
committed
Refactor cli-tests.
* Use better variable names (codemodProject vs input) * Extract shared `setupProject(hooks)` method * Fix linting issue with dev deps (don't publish `tests/` to npm)
1 parent a5776f7 commit d1bc291

2 files changed

Lines changed: 41 additions & 33 deletions

File tree

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"lint:js": "eslint .",
1111
"test": "qunit tests/**/*-test.js"
1212
},
13+
"files": [
14+
"src",
15+
"bin",
16+
"commands"
17+
],
1318
"dependencies": {
1419
"chalk": "^2.4.1",
1520
"common-tags": "^1.8.0",

tests/cli-test.js

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
11
const fs = require('fs-extra');
22
const path = require('path');
3-
/*eslint-disable node/no-unpublished-require */
43
const createTempDir = require('broccoli-test-helper').createTempDir;
54
const wrap = require('co').wrap;
65
const execa = require('execa');
76
const walkSync = require('walk-sync');
8-
/*eslint-enable*/
97

108
const PROJECT_ROOT = path.join(__dirname, '..');
119
const EXECUTABLE_PATH = path.join(PROJECT_ROOT, 'bin', 'cli.js');
10+
const CodemodCLI = require('../src');
1211
const ROOT = process.cwd();
1312

1413
QUnit.module('codemod-cli', function(hooks) {
15-
let input, output;
14+
let codemodProject;
15+
16+
function setupProject(hooks) {
17+
let sharedProject;
18+
19+
hooks.before(
20+
wrap(function*() {
21+
sharedProject = yield createTempDir();
22+
23+
process.chdir(sharedProject.path());
24+
yield execa(EXECUTABLE_PATH, ['new', 'test-project']);
25+
26+
process.chdir(ROOT);
27+
})
28+
);
29+
30+
hooks.beforeEach(function() {
31+
codemodProject.copy(sharedProject.path('test-project'));
32+
33+
// setup required dependencies in the project
34+
fs.ensureDirSync(`${codemodProject.path()}/node_modules`);
35+
fs.symlinkSync(`${ROOT}/node_modules/jest`, `${codemodProject.path()}/node_modules/jest`);
36+
fs.symlinkSync(PROJECT_ROOT, `${codemodProject.path()}/node_modules/codemod-cli`);
37+
});
38+
}
1639

1740
hooks.beforeEach(
1841
wrap(function*() {
19-
input = yield createTempDir();
20-
output = yield createTempDir();
42+
codemodProject = yield createTempDir();
2143

22-
process.chdir(input.path());
44+
process.chdir(codemodProject.path());
2345
})
2446
);
2547

2648
hooks.afterEach(
2749
wrap(function*() {
28-
yield input.dispose();
29-
yield output.dispose();
50+
yield codemodProject.dispose();
3051

3152
process.chdir(ROOT);
3253
})
@@ -39,10 +60,12 @@ QUnit.module('codemod-cli', function(hooks) {
3960
let result = yield execa(EXECUTABLE_PATH, ['new', 'ember-qunit-codemod']);
4061

4162
assert.equal(result.code, 0, 'exited with zero');
42-
assert.deepEqual(walkSync(input.path()), [
63+
assert.deepEqual(walkSync(codemodProject.path()), [
4364
'ember-qunit-codemod/',
4465
'ember-qunit-codemod/.travis.yml',
4566
'ember-qunit-codemod/README.md',
67+
'ember-qunit-codemod/bin/',
68+
'ember-qunit-codemod/bin/cli.js',
4669
'ember-qunit-codemod/package.json',
4770
'ember-qunit-codemod/transforms/',
4871
'ember-qunit-codemod/transforms/.gitkeep',
@@ -52,27 +75,7 @@ QUnit.module('codemod-cli', function(hooks) {
5275
});
5376

5477
QUnit.module('generate', function(hooks) {
55-
let project;
56-
57-
hooks.before(
58-
wrap(function*() {
59-
project = yield createTempDir();
60-
61-
process.chdir(project.path());
62-
yield execa(EXECUTABLE_PATH, ['new', 'test-project']);
63-
64-
process.chdir(ROOT);
65-
})
66-
);
67-
68-
hooks.beforeEach(function() {
69-
input.copy(project.path('test-project'));
70-
71-
// setup required dependencies in the project
72-
fs.ensureDirSync(`${input.path()}/node_modules`);
73-
fs.symlinkSync(`${ROOT}/node_modules/jest`, `${input.path()}/node_modules/jest`);
74-
fs.symlinkSync(PROJECT_ROOT, `${input.path()}/node_modules/codemod-cli`);
75-
});
78+
setupProject(hooks);
7679

7780
QUnit.module('codemod', function() {
7881
QUnit.test(
@@ -81,7 +84,7 @@ QUnit.module('codemod-cli', function(hooks) {
8184
let result = yield execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main']);
8285

8386
assert.equal(result.code, 0, 'exited with zero');
84-
assert.deepEqual(walkSync(input.path('transforms')), [
87+
assert.deepEqual(walkSync(codemodProject.path('transforms')), [
8588
'.gitkeep',
8689
'main/',
8790
'main/README.md',
@@ -108,7 +111,7 @@ QUnit.module('codemod-cli', function(hooks) {
108111
]);
109112

110113
assert.equal(result.code, 0, 'exited with zero');
111-
assert.deepEqual(walkSync(input.path('transforms')), [
114+
assert.deepEqual(walkSync(codemodProject.path('transforms')), [
112115
'.gitkeep',
113116
'main/',
114117
'main/README.md',
@@ -142,7 +145,7 @@ QUnit.module('codemod-cli', function(hooks) {
142145
yield execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main']);
143146
yield execa(EXECUTABLE_PATH, ['generate', 'fixture', 'main', 'this-dot-owner']);
144147

145-
input.write({
148+
codemodProject.write({
146149
transforms: {
147150
main: {
148151
__testfixtures__: {

0 commit comments

Comments
 (0)