Skip to content

Commit 833662c

Browse files
authored
Merge pull request #46 from ssutar/tests_subfolders
Update transform to include subfolders from the testfixtures in the test run
2 parents 6985964 + 4178924 commit 833662c

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

src/test-support.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const { runInlineTest } = require('jscodeshift/dist/testUtils');
66
const fs = require('fs-extra');
77
const path = require('path');
8+
const globby = require('globby');
89

910
function transformDetails(options) {
1011
let root = process.cwd() + `/transforms/${options.name}/`;
@@ -23,8 +24,13 @@ function jscodeshiftTest(options) {
2324
let transform = require(details.transformPath);
2425

2526
describe(details.name, function() {
26-
fs.readdirSync(details.fixtureDir)
27-
.filter(filename => /\.input$/.test(path.basename(filename, path.extname(filename))))
27+
globby
28+
.sync('**/*.input.*', {
29+
cwd: details.fixtureDir,
30+
absolute: true,
31+
transform: entry =>
32+
entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1),
33+
})
2834
.forEach(filename => {
2935
let extension = path.extname(filename);
3036
let testName = filename.replace(`.input${extension}`, '');

tests/cli-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,48 @@ QUnit.module('codemod-cli', function(hooks) {
225225
assert.equal(result.code, 0, 'exited with zero');
226226
})
227227
);
228+
229+
QUnit.test(
230+
'transform should receive a subfolder file path in tests',
231+
wrap(function*(assert) {
232+
const realCodemodProjectPath = fs.realpathSync(codemodProject.path());
233+
const expectedPath = `${realCodemodProjectPath}/transforms/main/__testfixtures__/foo/basic.input.js`;
234+
235+
yield execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main']);
236+
237+
codemodProject.write({
238+
transforms: {
239+
main: {
240+
'index.js': `
241+
const { getParser } = require('codemod-cli').jscodeshift;
242+
243+
module.exports = function transformer(file, api) {
244+
const j = getParser(api);
245+
246+
return j(file.source)
247+
.find(j.Literal)
248+
.forEach(path => {
249+
path.replace(
250+
j.stringLiteral(file.path)
251+
);
252+
})
253+
.toSource();
254+
}
255+
`,
256+
__testfixtures__: {
257+
foo: {
258+
'basic.input.js': 'var foo = "foo";',
259+
'basic.output.js': `var foo = "${expectedPath}";`,
260+
},
261+
},
262+
},
263+
},
264+
});
265+
266+
let result = yield execa(EXECUTABLE_PATH, ['test']);
267+
assert.equal(result.code, 0, 'exited with zero');
268+
})
269+
);
228270
});
229271
});
230272

0 commit comments

Comments
 (0)