@@ -112,7 +112,7 @@ QUnit.module('codemod-cli', function(hooks) {
112112 setupProject ( hooks ) ;
113113
114114 QUnit . module ( 'codemod' , function ( ) {
115- QUnit . test ( 'should generate a codemod' , async function ( assert ) {
115+ QUnit . test ( 'should generate a js codemod' , async function ( assert ) {
116116 let result = await execa ( EXECUTABLE_PATH , [ 'generate' , 'codemod' , 'main' ] ) ;
117117
118118 assert . equal ( result . exitCode , 0 , 'exited with zero' ) ;
@@ -127,10 +127,26 @@ QUnit.module('codemod-cli', function(hooks) {
127127 'main/test.js' ,
128128 ] ) ;
129129 } ) ;
130+
131+ QUnit . test ( 'should generate a hbs codemod' , async function ( assert ) {
132+ let result = await execa ( EXECUTABLE_PATH , [ 'generate' , 'codemod' , 'main' , '--type' , 'hbs' ] ) ;
133+
134+ assert . equal ( result . exitCode , 0 , 'exited with zero' ) ;
135+ assert . deepEqual ( walkSync ( codemodProject . path ( 'transforms' ) ) , [
136+ '.gitkeep' ,
137+ 'main/' ,
138+ 'main/README.md' ,
139+ 'main/__testfixtures__/' ,
140+ 'main/__testfixtures__/basic.input.hbs' ,
141+ 'main/__testfixtures__/basic.output.hbs' ,
142+ 'main/index.js' ,
143+ 'main/test.js' ,
144+ ] ) ;
145+ } ) ;
130146 } ) ;
131147
132148 QUnit . module ( 'fixture' , function ( ) {
133- QUnit . test ( 'should generate a fixture for the specified codemod' , async function ( assert ) {
149+ QUnit . test ( 'should generate a fixture for the specified js codemod' , async function ( assert ) {
134150 await execa ( EXECUTABLE_PATH , [ 'generate' , 'codemod' , 'main' ] ) ;
135151 let result = await execa ( EXECUTABLE_PATH , [
136152 'generate' ,
@@ -153,6 +169,32 @@ QUnit.module('codemod-cli', function(hooks) {
153169 'main/test.js' ,
154170 ] ) ;
155171 } ) ;
172+
173+ QUnit . test ( 'should generate a fixture for the specified hbs codemod' , async function ( assert ) {
174+ await execa ( EXECUTABLE_PATH , [ 'generate' , 'codemod' , 'main' , '--type' , 'hbs' ] ) ;
175+ let result = await execa ( EXECUTABLE_PATH , [
176+ 'generate' ,
177+ 'fixture' ,
178+ 'main' ,
179+ 'this-dot-owner' ,
180+ '--type' ,
181+ 'hbs' ,
182+ ] ) ;
183+
184+ assert . equal ( result . exitCode , 0 , 'exited with zero' ) ;
185+ assert . deepEqual ( walkSync ( codemodProject . path ( 'transforms' ) ) , [
186+ '.gitkeep' ,
187+ 'main/' ,
188+ 'main/README.md' ,
189+ 'main/__testfixtures__/' ,
190+ 'main/__testfixtures__/basic.input.hbs' ,
191+ 'main/__testfixtures__/basic.output.hbs' ,
192+ 'main/__testfixtures__/this-dot-owner.input.hbs' ,
193+ 'main/__testfixtures__/this-dot-owner.output.hbs' ,
194+ 'main/index.js' ,
195+ 'main/test.js' ,
196+ ] ) ;
197+ } ) ;
156198 } ) ;
157199
158200 QUnit . module ( 'test' , function ( ) {
@@ -539,5 +581,89 @@ QUnit.module('codemod-cli', function(hooks) {
539581 } ) ;
540582 } ) ;
541583 } ) ;
584+
585+ QUnit . module ( 'runTransform type=template' , function ( hooks ) {
586+ let userProject ;
587+
588+ hooks . beforeEach ( async function ( ) {
589+ // includes simple mustache transform
590+ await execa ( EXECUTABLE_PATH , [ 'generate' , 'codemod' , 'main' , '--type' , 'hbs' ] ) ;
591+
592+ userProject = await createTempDir ( ) ;
593+ process . chdir ( userProject . path ( ) ) ;
594+ } ) ;
595+
596+ hooks . afterEach ( function ( ) {
597+ process . chdir ( ROOT ) ;
598+
599+ return userProject . dispose ( ) ;
600+ } ) ;
601+
602+ QUnit . test ( 'runs transform' , async function ( assert ) {
603+ userProject . write ( {
604+ foo : {
605+ 'something.hbs' : '{{what}}' ,
606+ 'other.hbs' : '{{what}}' ,
607+ } ,
608+ } ) ;
609+
610+ await CodemodCLI . runTransform (
611+ codemodProject . path ( 'bin' ) ,
612+ 'main' ,
613+ 'foo/*thing.hbs' ,
614+ undefined ,
615+ 'template'
616+ ) ;
617+
618+ assert . deepEqual ( userProject . read ( ) , {
619+ foo : {
620+ 'something.hbs' : '{{wat-wat}}' ,
621+ 'other.hbs' : '{{what}}' ,
622+ } ,
623+ } ) ;
624+ } ) ;
625+
626+ QUnit . test ( 'runs transform with options' , async function ( assert ) {
627+ codemodProject . write ( {
628+ transforms : {
629+ main : {
630+ 'index.js' : `
631+ const { getOptions } = require('codemod-cli');
632+ module.exports = function ({ source /*, path*/ }, { parse, visit }) {
633+ const ast = parse(source);
634+ const options = getOptions();
635+
636+ return visit(ast, (env) => {
637+ let { builders: b } = env.syntax;
638+
639+ return {
640+ MustacheStatement() {
641+ return b.mustache(b.path(options.biz + options.baz));
642+ },
643+ };
644+ });
645+ };
646+ ` ,
647+ } ,
648+ } ,
649+ } ) ;
650+
651+ userProject . write ( {
652+ foo : { 'something.hbs' : `{{foo}}` } ,
653+ } ) ;
654+
655+ await CodemodCLI . runTransform (
656+ codemodProject . path ( 'bin' ) ,
657+ 'main' ,
658+ [ '--biz' , 'A' , '--baz' , 'B' , 'foo/*ing.hbs' ] ,
659+ undefined ,
660+ 'template'
661+ ) ;
662+
663+ assert . deepEqual ( userProject . read ( ) , {
664+ foo : { 'something.hbs' : `{{AB}}` } ,
665+ } ) ;
666+ } ) ;
667+ } ) ;
542668 } ) ;
543669} ) ;
0 commit comments