@@ -186,6 +186,46 @@ QUnit.module('codemod-cli', function(hooks) {
186186 } )
187187 ) ;
188188
189+ QUnit . test (
190+ 'transform should receive options from ${name}.options.json' ,
191+ wrap ( function * ( assert ) {
192+ const expectedReplacement = 'AAAAHHHHHH' ;
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+ const { getOptions } = require('codemod-cli');
202+ module.exports = function transformer(file, api) {
203+ const options = getOptions();
204+ const j = getParser(api);
205+ return j(file.source)
206+ .find(j.Literal)
207+ .forEach(path => {
208+ path.replace(
209+ j.stringLiteral(options.replaceAll)
210+ );
211+ })
212+ .toSource();
213+ }
214+ ` ,
215+ __testfixtures__ : {
216+ 'basic.input.js' : 'var foo = "foo";' ,
217+ 'basic.output.js' : `var foo = "${ expectedReplacement } ";` ,
218+ 'basic.options.json' : `{ "replaceAll": "${ expectedReplacement } " }` ,
219+ } ,
220+ } ,
221+ } ,
222+ } ) ;
223+
224+ let result = yield execa ( EXECUTABLE_PATH , [ 'test' ] ) ;
225+ assert . equal ( result . code , 0 , 'exited with zero' ) ;
226+ } )
227+ ) ;
228+
189229 QUnit . test (
190230 'transform should receive a file path in tests' ,
191231 wrap ( function * ( assert ) {
@@ -295,10 +335,7 @@ QUnit.module('codemod-cli', function(hooks) {
295335 'works with globs' ,
296336 wrap ( function * ( assert ) {
297337 userProject . write ( {
298- foo : {
299- 'something.js' : 'let blah = bar' ,
300- 'other.js' : 'let blah = bar' ,
301- } ,
338+ foo : { 'something.js' : 'let blah = bar' , 'other.js' : 'let blah = bar' } ,
302339 } ) ;
303340
304341 yield execa ( codemodProject . path ( 'bin/cli.js' ) , [ 'main' , 'foo/*thing.js' ] ) ;
@@ -356,6 +393,49 @@ QUnit.module('codemod-cli', function(hooks) {
356393 } )
357394 ) ;
358395
396+ QUnit . test (
397+ 'runs transform with options' ,
398+ wrap ( function * ( assert ) {
399+ codemodProject . write ( {
400+ transforms : {
401+ main : {
402+ 'index.js' : `
403+ const { getParser } = require('codemod-cli').jscodeshift;
404+ const { getOptions } = require('codemod-cli');
405+ module.exports = function transformer(file, api) {
406+ const options = getOptions();
407+ const j = getParser(api);
408+ return j(file.source)
409+ .find(j.Literal)
410+ .forEach(path => {
411+ path.replace(
412+ j.stringLiteral(options.biz + options.baz)
413+ );
414+ })
415+ .toSource();
416+ }
417+ ` ,
418+ } ,
419+ } ,
420+ } ) ;
421+ userProject . write ( {
422+ foo : { 'something.js' : `let blah = "bar";` } ,
423+ } ) ;
424+
425+ yield CodemodCLI . runTransform ( codemodProject . path ( 'bin' ) , 'main' , [
426+ '--biz' ,
427+ 'A' ,
428+ '--baz' ,
429+ 'B' ,
430+ 'foo/*ing.[jt]s' ,
431+ ] ) ;
432+
433+ assert . deepEqual ( userProject . read ( ) , {
434+ foo : { 'something.js' : `let blah = "AB";` } ,
435+ } ) ;
436+ } )
437+ ) ;
438+
359439 QUnit . test (
360440 'runs transform against class syntax' ,
361441 wrap ( function * ( assert ) {
0 commit comments