|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import * as os from 'os'; |
| 19 | +import * as fs from 'fs'; |
| 20 | +import * as path from 'path'; |
| 21 | +import { format, resolveConfig } from 'prettier'; |
| 22 | +import { expect } from 'chai'; |
| 23 | +import { describe, it } from 'mocha'; |
| 24 | + |
| 25 | +import { pruneDts } from './prune-dts'; |
| 26 | + |
| 27 | +const testCasesDir = path.resolve(__dirname, 'tests'); |
| 28 | +const tmpDir = os.tmpdir(); |
| 29 | + |
| 30 | +const testDataFilter = /(.*).input.d.ts/; |
| 31 | +const testCaseFilterRe = /.*/; |
| 32 | + |
| 33 | +async function runScript(inputFile: string): Promise<string> { |
| 34 | + const outputFile = path.resolve(tmpDir, 'output.d.ts'); |
| 35 | + pruneDts(inputFile, outputFile); |
| 36 | + return outputFile; |
| 37 | +} |
| 38 | + |
| 39 | +interface TestCase { |
| 40 | + name: string; |
| 41 | + inputFileName: string; |
| 42 | + outputFileName: string; |
| 43 | +} |
| 44 | + |
| 45 | +function getTestCases(): TestCase[] { |
| 46 | + if ( |
| 47 | + !fs.existsSync(testCasesDir) || |
| 48 | + !fs.lstatSync(testCasesDir).isDirectory() |
| 49 | + ) { |
| 50 | + throw new Error(`${testCasesDir} folder does not exist`); |
| 51 | + } |
| 52 | + |
| 53 | + return fs |
| 54 | + .readdirSync(testCasesDir) |
| 55 | + .filter((fileName: string) => testDataFilter.test(fileName)) |
| 56 | + .filter((fileName: string) => testCaseFilterRe.test(fileName)) |
| 57 | + .map((fileName: string) => { |
| 58 | + const testCaseName = fileName.match(testDataFilter)![1]; |
| 59 | + |
| 60 | + const inputFileName = `${testCaseName}.input.d.ts`; |
| 61 | + const outputFileName = `${testCaseName}.output.d.ts`; |
| 62 | + |
| 63 | + const name = testCaseName.replace(/-/g, ' '); |
| 64 | + return { name, inputFileName, outputFileName }; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +describe('Prune DTS', () => { |
| 69 | + for (const testCase of getTestCases()) { |
| 70 | + it(testCase.name, async () => { |
| 71 | + const absoluteInputFile = path.resolve( |
| 72 | + testCasesDir, |
| 73 | + testCase.inputFileName |
| 74 | + ); |
| 75 | + const absoluteOutputFile = path.resolve( |
| 76 | + testCasesDir, |
| 77 | + testCase.outputFileName |
| 78 | + ); |
| 79 | + |
| 80 | + const tmpFile = await runScript(absoluteInputFile); |
| 81 | + const prettierConfig = await resolveConfig(absoluteInputFile); |
| 82 | + |
| 83 | + const expectedDtsUnformatted = fs.readFileSync( |
| 84 | + absoluteOutputFile, |
| 85 | + 'utf-8' |
| 86 | + ); |
| 87 | + const expectedDts = format(expectedDtsUnformatted, { |
| 88 | + filepath: absoluteOutputFile, |
| 89 | + ...prettierConfig |
| 90 | + }); |
| 91 | + const actualDtsUnformatted = fs.readFileSync(tmpFile, 'utf-8'); |
| 92 | + const actualDts = format(actualDtsUnformatted, { |
| 93 | + filepath: tmpFile, |
| 94 | + ...prettierConfig |
| 95 | + }); |
| 96 | + |
| 97 | + expect(actualDts).to.equal(expectedDts); |
| 98 | + }); |
| 99 | + } |
| 100 | +}); |
0 commit comments