|
| 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 { expect } from 'chai'; |
| 22 | +import { describe, it } from 'mocha'; |
| 23 | + |
| 24 | +import { pruneDts } from './prune-dts'; |
| 25 | + |
| 26 | +const testCasesDir = path.resolve(__dirname, 'tests'); |
| 27 | +const tmpDir = os.tmpdir(); |
| 28 | + |
| 29 | +const testDataFilter = /(.*).input.d.ts/; |
| 30 | +const testCaseFilterRe = /.*inherits.*/; |
| 31 | + |
| 32 | +function runScript(filename: string): string { |
| 33 | + const inputFile = path.resolve(testCasesDir, filename); |
| 34 | + const outputFile = path.resolve(tmpDir, 'output.d.ts'); |
| 35 | + pruneDts(inputFile, outputFile); |
| 36 | + return fs.readFileSync(outputFile, 'utf-8'); |
| 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, () => { |
| 71 | + const expectedDts = fs.readFileSync( |
| 72 | + path.resolve(testCasesDir, testCase.outputFileName), |
| 73 | + 'utf-8' |
| 74 | + ); |
| 75 | + const actualDts = runScript(testCase.inputFileName); |
| 76 | + expect(actualDts).to.equal(expectedDts); |
| 77 | + }); |
| 78 | + } |
| 79 | +}); |
0 commit comments