-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbin-test.js
193 lines (144 loc) · 5.24 KB
/
bin-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
'use strict';
const path = require('path');
const fs = require('fs-extra');
const cp = require('child_process');
const tmp = require('tmp');
const chalk = require("chalk");
const originalCwd = process.cwd();
let tmpPath;
function run(codemodArgs) {
let stdout = '';
let stderr = '';
return new Promise(resolve => {
let codemodCmd = [path.join(originalCwd, 'bin/ember-modules-codemod')];
if(codemodArgs) {
codemodCmd = codemodCmd.concat(codemodArgs);
}
let ps = cp.spawn('node', codemodCmd, {
cwd: tmpPath
});
ps.stdout.on('data', data => {
stdout += data.toString();
});
ps.stderr.on('data', data => {
stderr += data.toString();
});
ps.on('exit', code => {
resolve({
exitCode: code,
stdout,
stderr
});
});
});
}
describe('bin acceptance', function() {
let tmpPackageJson;
beforeEach(function() {
tmpPath = tmp.dirSync().name;
process.chdir(tmpPath);
tmpPackageJson = path.join(process.cwd(), 'package.json');
});
afterAll(function() {
process.chdir(originalCwd);
});
it('handles non-ember projects', function() {
return run().then(result => {
let exitCode = result.exitCode;
let stderr = result.stderr;
expect(exitCode).not.toEqual(0);
expect(stderr).toContain(`It doesn't look like you're inside an Ember app. I couldn't find a package.json at ${tmpPackageJson}`);
});
});
describe('with valid package.json', function() {
beforeEach(function() {
fs.writeJsonSync(tmpPackageJson, {
devDependencies: {
'ember-cli': ''
}
});
});
it('exits gracefully when no files found', function() {
return run().then(result => {
let exitCode = result.exitCode;
let stderr = result.stderr;
expect(exitCode).toEqual(0);
// jscodeshift can process in any order
expect(stderr).toMatch('Skipping path app which does not exist.');
expect(stderr).toMatch('Skipping path addon which does not exist.');
expect(stderr).toMatch('Skipping path addon-test-support which does not exist.');
expect(stderr).toMatch('Skipping path tests which does not exist.');
expect(stderr).toMatch('Skipping path test-support which does not exist.');
expect(stderr).toMatch('Skipping path lib which does not exist.');
});
});
describe('with valid file', function() {
let tmpFile;
const inputFile = path.join(originalCwd, '__testfixtures__/final-boss.input.js');
const outputFile = path.join(originalCwd, '__testfixtures__/final-boss.output.js');
beforeEach(function() {
fs.ensureDirSync(path.join(tmpPath, 'app'));
tmpFile = path.join(tmpPath, 'app/final-boss.js');
fs.copySync(
inputFile,
tmpFile
);
});
it('works', function() {
return run().then(result => {
let exitCode = result.exitCode;
let stdout = result.stdout;
expect(exitCode).toEqual(0);
expect(stdout).toContain('Done! All uses of the Ember global have been updated.');
expect(fs.readFileSync(tmpFile, 'utf8')).toEqual(fs.readFileSync(outputFile, 'utf8'));
});
});
});
describe('with invalid file', function() {
const inputFile = path.join(originalCwd, '__testfixtures__/leave-unmatched-destructuring.input.js');
beforeEach(function() {
fs.ensureDirSync(path.join(tmpPath, 'app'));
let tmpFile = path.join(tmpPath, 'app/leave-unmatched-destructuring.input.js');
fs.copySync(
inputFile,
tmpFile
);
});
it('reports errors', function() {
return run().then(result => {
let exitCode = result.exitCode;
let stdout = result.stdout;
expect(exitCode).toEqual(0);
expect(stdout).toMatch(chalk.yellow("\nDone! Some files could not be upgraded automatically. See " + chalk.blue("MODULE_REPORT.md") + "."));
let expectedOutput = fs.readFileSync(path.join(__dirname, 'expected', 'MODULE_REPORT.md'), 'utf8')
.replace(/\//, path.sep)
.replace(/\r?\n/g, require('os').EOL);
let actualOutput = fs.readFileSync(path.join(tmpPath, 'MODULE_REPORT.md'), 'utf8');
expect(actualOutput).toEqual(expectedOutput);
});
});
});
describe('with a custom path', function() {
let tmpFile;
const inputFile = path.join(originalCwd, '__testfixtures__/final-boss.input.js');
const outputFile = path.join(originalCwd, '__testfixtures__/final-boss.output.js');
beforeEach(function() {
fs.ensureDirSync(path.join(tmpPath, 'custom-path'));
tmpFile = path.join(tmpPath, 'custom-path/final-boss.js');
fs.copySync(
inputFile,
tmpFile
);
});
it('works', function() {
return run('custom-path').then(result => {
let exitCode = result.exitCode;
let stdout = result.stdout;
expect(exitCode).toEqual(0);
expect(stdout).toContain('Done! All uses of the Ember global have been updated.');
expect(fs.readFileSync(tmpFile, 'utf8')).toEqual(fs.readFileSync(outputFile, 'utf8'));
});
});
});
});
});