Skip to content

Commit b293927

Browse files
committed
Add template test support
1 parent 7f61739 commit b293927

5 files changed

Lines changed: 147 additions & 69 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@babel/parser": "^7.6.0",
3939
"chalk": "^2.4.2",
4040
"common-tags": "^1.8.0",
41+
"ember-template-recast": "^4.1.4",
4142
"execa": "^2.0.4",
4243
"fs-extra": "^8.1.0",
4344
"globby": "^10.0.1",

src/test-support.js

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,14 @@
11
'use strict';
22

3-
/* global it, describe, beforeEach, afterEach */
4-
5-
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6-
const fs = require('fs-extra');
7-
const path = require('path');
8-
const globby = require('globby');
9-
10-
function transformDetails(options) {
11-
let root = process.cwd() + `/transforms/${options.name}/`;
12-
13-
return {
14-
name: options.name,
15-
root,
16-
transformPath: root + 'index',
17-
fixtureDir: root + '__testfixtures__/',
18-
};
19-
}
20-
21-
function jscodeshiftTest(options) {
22-
let details = transformDetails(options);
23-
24-
let transform = require(details.transformPath);
25-
26-
describe(details.name, function() {
27-
globby
28-
.sync('**/*.input.*', {
29-
cwd: details.fixtureDir,
30-
absolute: true,
31-
})
32-
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
33-
.forEach(filename => {
34-
let extension = path.extname(filename);
35-
let testName = filename.replace(`.input${extension}`, '');
36-
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
37-
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
38-
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
39-
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
40-
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
41-
42-
describe(testName, function() {
43-
beforeEach(function() {
44-
process.env.CODEMOD_CLI_ARGS = options;
45-
});
46-
47-
afterEach(function() {
48-
process.env.CODEMOD_CLI_ARGS = '';
49-
});
50-
51-
it('transforms correctly', function() {
52-
runInlineTest(
53-
transform,
54-
{},
55-
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
56-
fs.readFileSync(outputPath, 'utf8')
57-
);
58-
});
59-
60-
it('is idempotent', function() {
61-
runInlineTest(
62-
transform,
63-
{},
64-
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
65-
fs.readFileSync(outputPath, 'utf8')
66-
);
67-
});
68-
});
69-
});
70-
});
71-
}
3+
const jscodeshiftTest = require('./test-support/jscodeshift');
4+
const templateTest = require('./test-support/template');
725

736
function runTransformTest(options) {
747
switch (options.type) {
758
case 'jscodeshift':
769
return jscodeshiftTest(options);
10+
case 'template':
11+
return templateTest(options);
7712
}
7813
}
7914

src/test-support/jscodeshift.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict';
2+
3+
/* global it, describe, beforeEach, afterEach */
4+
5+
const { runInlineTest } = require('jscodeshift/dist/testUtils');
6+
const fs = require('fs-extra');
7+
const path = require('path');
8+
const globby = require('globby');
9+
const { transformDetails } = require('./utils');
10+
11+
module.exports = function jscodeshiftTest(options) {
12+
let details = transformDetails(options);
13+
14+
let transform = require(details.transformPath);
15+
16+
describe(details.name, function() {
17+
globby
18+
.sync('**/*.input.*', {
19+
cwd: details.fixtureDir,
20+
absolute: true,
21+
})
22+
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
23+
.forEach(filename => {
24+
let extension = path.extname(filename);
25+
let testName = filename.replace(`.input${extension}`, '');
26+
let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`);
27+
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
28+
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
29+
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
30+
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
31+
32+
describe(testName, function() {
33+
beforeEach(function() {
34+
process.env.CODEMOD_CLI_ARGS = options;
35+
});
36+
37+
afterEach(function() {
38+
process.env.CODEMOD_CLI_ARGS = '';
39+
});
40+
41+
it('transforms correctly', function() {
42+
runInlineTest(
43+
transform,
44+
{},
45+
{ path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') },
46+
fs.readFileSync(outputPath, 'utf8')
47+
);
48+
});
49+
50+
it('is idempotent', function() {
51+
runInlineTest(
52+
transform,
53+
{},
54+
{ path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') },
55+
fs.readFileSync(outputPath, 'utf8')
56+
);
57+
});
58+
});
59+
});
60+
});
61+
};

src/test-support/template.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
/* global expect, it, describe, beforeEach, afterEach */
4+
5+
const fs = require('fs-extra');
6+
const path = require('path');
7+
const globby = require('globby');
8+
const { transformDetails } = require('./utils');
9+
10+
function runTemplateTest(transformFn, input, expectedOutput) {
11+
const { transform } = require('ember-template-recast');
12+
13+
let { code } = transform(input, transformFn);
14+
15+
expect((code || '').trim()).toEqual(expectedOutput.trim());
16+
}
17+
18+
module.exports = function templateTest(options) {
19+
let details = transformDetails(options);
20+
21+
let transform = require(details.transformPath);
22+
23+
describe(details.name, function() {
24+
globby
25+
.sync('**/*.input.*', {
26+
cwd: details.fixtureDir,
27+
absolute: true,
28+
})
29+
.map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1))
30+
.forEach(filename => {
31+
let extension = path.extname(filename);
32+
let testName = filename.replace(`.input${extension}`, '');
33+
let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`);
34+
let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`);
35+
let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`);
36+
let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}';
37+
38+
describe(testName, function() {
39+
beforeEach(function() {
40+
process.env.CODEMOD_CLI_ARGS = options;
41+
});
42+
43+
afterEach(function() {
44+
process.env.CODEMOD_CLI_ARGS = '';
45+
});
46+
47+
it('transforms correctly', function() {
48+
runTemplateTest(
49+
transform,
50+
fs.readFileSync(inputPath, 'utf8'),
51+
fs.readFileSync(outputPath, 'utf8')
52+
);
53+
});
54+
55+
it('is idempotent', function() {
56+
runTemplateTest(
57+
transform,
58+
fs.readFileSync(outputPath, 'utf8'),
59+
fs.readFileSync(outputPath, 'utf8')
60+
);
61+
});
62+
});
63+
});
64+
});
65+
};

src/test-support/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
function transformDetails(options) {
4+
let root = process.cwd() + `/transforms/${options.name}/`;
5+
6+
return {
7+
name: options.name,
8+
root,
9+
transformPath: root + 'index',
10+
fixtureDir: root + '__testfixtures__/',
11+
};
12+
}
13+
14+
module.exports = {
15+
transformDetails,
16+
};

0 commit comments

Comments
 (0)