|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -/* global it, describe, beforeEach, afterEach */ |
4 | | - |
5 | | -const { runInlineTest } = require('jscodeshift/dist/testUtils'); |
6 | | -const fs = require('fs-extra'); |
7 | | -const path = require('path'); |
8 | | -const globby = require('globby'); |
9 | | - |
10 | | -function transformDetails(options) { |
11 | | - let root = process.cwd() + `/transforms/${options.name}/`; |
12 | | - |
13 | | - return { |
14 | | - name: options.name, |
15 | | - root, |
16 | | - transformPath: root + 'index', |
17 | | - fixtureDir: root + '__testfixtures__/', |
18 | | - }; |
19 | | -} |
20 | | - |
21 | | -function jscodeshiftTest(options) { |
22 | | - let details = transformDetails(options); |
23 | | - |
24 | | - let transform = require(details.transformPath); |
25 | | - |
26 | | - describe(details.name, function() { |
27 | | - globby |
28 | | - .sync('**/*.input.*', { |
29 | | - cwd: details.fixtureDir, |
30 | | - absolute: true, |
31 | | - }) |
32 | | - .map(entry => entry.slice(entry.indexOf('__testfixtures__') + '__testfixtures__'.length + 1)) |
33 | | - .forEach(filename => { |
34 | | - let extension = path.extname(filename); |
35 | | - let testName = filename.replace(`.input${extension}`, ''); |
36 | | - let testInputPath = path.join(details.fixtureDir, `${testName}${extension}`); |
37 | | - let inputPath = path.join(details.fixtureDir, `${testName}.input${extension}`); |
38 | | - let outputPath = path.join(details.fixtureDir, `${testName}.output${extension}`); |
39 | | - let optionsPath = path.join(details.fixtureDir, `${testName}.options.json`); |
40 | | - let options = fs.pathExistsSync(optionsPath) ? fs.readFileSync(optionsPath) : '{}'; |
41 | | - |
42 | | - describe(testName, function() { |
43 | | - beforeEach(function() { |
44 | | - process.env.CODEMOD_CLI_ARGS = options; |
45 | | - }); |
46 | | - |
47 | | - afterEach(function() { |
48 | | - process.env.CODEMOD_CLI_ARGS = ''; |
49 | | - }); |
50 | | - |
51 | | - it('transforms correctly', function() { |
52 | | - runInlineTest( |
53 | | - transform, |
54 | | - {}, |
55 | | - { path: testInputPath, source: fs.readFileSync(inputPath, 'utf8') }, |
56 | | - fs.readFileSync(outputPath, 'utf8') |
57 | | - ); |
58 | | - }); |
59 | | - |
60 | | - it('is idempotent', function() { |
61 | | - runInlineTest( |
62 | | - transform, |
63 | | - {}, |
64 | | - { path: testInputPath, source: fs.readFileSync(outputPath, 'utf8') }, |
65 | | - fs.readFileSync(outputPath, 'utf8') |
66 | | - ); |
67 | | - }); |
68 | | - }); |
69 | | - }); |
70 | | - }); |
71 | | -} |
| 3 | +const jscodeshiftTest = require('./test-support/jscodeshift'); |
| 4 | +const templateTest = require('./test-support/template'); |
72 | 5 |
|
73 | 6 | function runTransformTest(options) { |
74 | 7 | switch (options.type) { |
75 | 8 | case 'jscodeshift': |
76 | 9 | return jscodeshiftTest(options); |
| 10 | + case 'template': |
| 11 | + return templateTest(options); |
77 | 12 | } |
78 | 13 | } |
79 | 14 |
|
|
0 commit comments