11'use strict' ;
22
3- /* global expect, it, describe, beforeEach, afterEach */
3+ /* global expect, it, describe */
44
55const fs = require ( 'fs-extra' ) ;
66const path = require ( 'path' ) ;
77const globby = require ( 'globby' ) ;
88const { transformDetails } = require ( './utils' ) ;
9+ const { transform, parse } = require ( 'ember-template-recast' ) ;
910
10- function runTemplateTest ( transformFn , input , expectedOutput ) {
11- const { transform } = require ( 'ember-template-recast' ) ;
12-
13- let { code } = transform ( input , transformFn ) ;
11+ function runTemplateTest ( plugin , { path, source } , expectedOutput ) {
12+ const code = plugin (
13+ {
14+ path,
15+ source,
16+ } ,
17+ {
18+ parse,
19+ visit ( ast , callback ) {
20+ const results = transform ( ast , callback ) ;
21+ return results && results . code ;
22+ } ,
23+ }
24+ ) ;
1425
1526 expect ( ( code || '' ) . trim ( ) ) . toEqual ( expectedOutput . trim ( ) ) ;
1627}
1728
1829module . exports = function templateTest ( options ) {
1930 let details = transformDetails ( options ) ;
2031
21- let transform = require ( details . transformPath ) ;
32+ let plugin = require ( details . transformPath ) ;
2233
2334 describe ( details . name , function ( ) {
2435 globby
@@ -30,32 +41,23 @@ module.exports = function templateTest(options) {
3041 . forEach ( filename => {
3142 let extension = path . extname ( filename ) ;
3243 let testName = filename . replace ( `.input${ extension } ` , '' ) ;
44+ let testInputPath = path . join ( details . fixtureDir , `${ testName } ${ extension } ` ) ;
3345 let inputPath = path . join ( details . fixtureDir , `${ testName } .input${ extension } ` ) ;
3446 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 ) : '{}' ;
3747
3848 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-
4749 it ( 'transforms correctly' , function ( ) {
4850 runTemplateTest (
49- transform ,
50- fs . readFileSync ( inputPath , 'utf8' ) ,
51+ plugin ,
52+ { path : testInputPath , source : fs . readFileSync ( inputPath , 'utf8' ) } ,
5153 fs . readFileSync ( outputPath , 'utf8' )
5254 ) ;
5355 } ) ;
5456
5557 it ( 'is idempotent' , function ( ) {
5658 runTemplateTest (
57- transform ,
58- fs . readFileSync ( outputPath , 'utf8' ) ,
59+ plugin ,
60+ { path : testInputPath , source : fs . readFileSync ( inputPath , 'utf8' ) } ,
5961 fs . readFileSync ( outputPath , 'utf8' )
6062 ) ;
6163 } ) ;
0 commit comments