Skip to content

Commit 4ccaf8b

Browse files
authored
Merge pull request #36 from ro0gr/pass-path-to-inline-test
Pass a file path to the runInlineTest
2 parents ba03bac + 39949b1 commit 4ccaf8b

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

src/test-support.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function jscodeshiftTest(options) {
3636
runInlineTest(
3737
transform,
3838
{},
39-
{ source: fs.readFileSync(inputPath, 'utf8') },
39+
{ path: inputPath, source: fs.readFileSync(inputPath, 'utf8') },
4040
fs.readFileSync(outputPath, 'utf8')
4141
);
4242
});
@@ -45,7 +45,7 @@ function jscodeshiftTest(options) {
4545
runInlineTest(
4646
transform,
4747
{},
48-
{ source: fs.readFileSync(outputPath, 'utf8') },
48+
{ path: inputPath, source: fs.readFileSync(outputPath, 'utf8') },
4949
fs.readFileSync(outputPath, 'utf8')
5050
);
5151
});

tests/cli-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,45 @@ QUnit.module('codemod-cli', function(hooks) {
185185
}
186186
})
187187
);
188+
189+
QUnit.test(
190+
'transform should receive a file path in tests',
191+
wrap(function*(assert) {
192+
const expectedPath = `${codemodProject.path()}/transforms/main/__testfixtures__/basic.input.js`;
193+
194+
yield execa(EXECUTABLE_PATH, ['generate', 'codemod', 'main']);
195+
196+
codemodProject.write({
197+
transforms: {
198+
main: {
199+
'index.js': `
200+
const { getParser } = require('codemod-cli').jscodeshift;
201+
202+
module.exports = function transformer(file, api) {
203+
const j = getParser(api);
204+
205+
return j(file.source)
206+
.find(j.Literal)
207+
.forEach(path => {
208+
path.replace(
209+
j.stringLiteral(file.path)
210+
);
211+
})
212+
.toSource();
213+
}
214+
`,
215+
__testfixtures__: {
216+
'basic.input.js': 'var foo = "foo";',
217+
'basic.output.js': `var foo = "${expectedPath}";`,
218+
},
219+
},
220+
},
221+
});
222+
223+
let result = yield execa(EXECUTABLE_PATH, ['test']);
224+
assert.equal(result.code, 0, 'exited with zero');
225+
})
226+
);
188227
});
189228
});
190229

0 commit comments

Comments
 (0)