• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { expect } from 'chai';
2import path from 'path';
3import fs from 'fs';
4import { JSDocModifierImpl } from '../../src/core/entry';
5
6describe('testSingleFile', function () {
7  const testFileDir = path.join(__dirname, '..', '/ut/');
8  const outputFileDir = path.join(__dirname, '..', '/output/testSingleFile/');
9  const expectFileDir = path.join(__dirname, '..', '/expect/');
10  const testFileNames = fs.readdirSync(testFileDir);
11  const argLen = process.argv.length;
12  testFileNames.forEach((testFileName) => {
13    const testFilePath = path.join(testFileDir, testFileName);
14    const outputFilePath = path.join(outputFileDir, testFileName);
15    const expectFilePath = path.join(expectFileDir, testFileName);
16    const baseName: string = path.basename(testFileName, '.d.ts');
17    const expectReportFilePath: string = path.join(expectFileDir, `${baseName}.json`);
18    const outputReportFilePath: string = path.join(outputFileDir, `${baseName}.json`);
19    it('testFile#' + testFilePath, async function () {
20      if (fs.existsSync(outputFilePath)) {
21        fs.rmSync(outputFilePath);
22      }
23      const inputParams = [];
24      inputParams.push('-s');
25      inputParams.push('-i');
26      inputParams.push(testFilePath);
27      inputParams.push('-o');
28      inputParams.push(outputFilePath);
29      inputParams.push('-t');
30      process.argv.splice(argLen, inputParams.length, ...inputParams);
31      const testEntry = new JSDocModifierImpl();
32      await testEntry.start();
33
34      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
35      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
36      expect(outputFileContent).eql(expectFileContent);
37      const outputReportContent: string = fs.readFileSync(outputReportFilePath, 'utf-8').replace(/\r\n/g, '\n');
38      const expectReportContent: string = fs.readFileSync(expectReportFilePath, 'utf-8').replace(/\r\n/g, '\n');
39      expect(outputReportContent).eql(expectReportContent);
40    });
41  });
42});
43
44describe('testMultiFiles', function () {
45  const testFileDir = path.join(__dirname, '..', '/ut/');
46  const outFileDir = path.join(__dirname, '..', '/output/testMultiFiles/');
47  const expectFileDir = path.join(__dirname, '..', '/expect/');
48
49  before(async function () {
50    this.timeout(10000);
51    const inputParams = [];
52    inputParams.push('-s');
53    inputParams.push('-i');
54    inputParams.push(testFileDir);
55    inputParams.push('-o');
56    inputParams.push(outFileDir);
57    inputParams.push('-t');
58    process.argv.splice(process.argv.length, 0, ...inputParams);
59    const testEntry = new JSDocModifierImpl();
60    await testEntry.start();
61  });
62
63  const testFileNames = fs.readdirSync(testFileDir);
64  testFileNames.forEach((testFileName) => {
65    const testFilePath = path.join(testFileDir, testFileName);
66    const outputFilePath = path.join(outFileDir, testFileName);
67    const expectFilePath = path.join(expectFileDir, testFileName);
68    it('testDir#' + testFilePath, function () {
69      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
70      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
71      expect(outputFileContent).eql(expectFileContent);
72    });
73  });
74});
75
76describe('testBundleSingleFile', function () {
77  const testFileDir = path.join(__dirname, '..', '/ut/');
78  const outFileDir = path.join(__dirname, '..', '/output/testBundleSingleFile/');
79  const expectFileDir = path.join(__dirname, '..', '/expect/');
80  const testFileNames = fs.readdirSync(testFileDir);
81  const nodeExecute = process.execPath;
82  const { execFileSync } = require('child_process');
83  testFileNames.forEach((testFileName) => {
84    const testFilePath = path.join(testFileDir, testFileName);
85    const outputFilePath = path.join(outFileDir, testFileName);
86    const expectFilePath = path.join(expectFileDir, testFileName);
87    it('bundle_file#' + testFileName, function () {
88      if (fs.existsSync(outputFilePath)) {
89        fs.rmSync(outputFilePath);
90      }
91      execFileSync(nodeExecute, [
92        'build/bundle.js', '-s',
93        '-i', `${testFilePath}`,
94        '-o', `${outputFilePath}`,
95        '-t'
96      ]);
97      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
98      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
99      expect(outputFileContent).eql(expectFileContent);
100    });
101  });
102});
103
104describe('testBundleMultiFiles', function () {
105  const testFileDir = path.join(__dirname, '..', '/ut/');
106  const outFileDir = path.join(__dirname, '..', '/output/testBundleMultiFiles/');
107  const expectFileDir = path.join(__dirname, '..', '/expect/');
108  const nodeExecute = process.execPath;
109  const { execFileSync } = require('child_process');
110
111  before(function () {
112    this.timeout(10000);
113    execFileSync(nodeExecute, [
114      'build/bundle.js', '-s',
115      '-i', `${testFileDir}`,
116      '-o', `${outFileDir}`,
117      '-t'
118    ]);
119  });
120
121  const testFileNames = fs.readdirSync(testFileDir);
122  testFileNames.forEach((testFileName) => {
123    const outputFilePath = path.join(outFileDir, testFileName);
124    const expectFilePath = path.join(expectFileDir, testFileName);
125    it('bundle_dir#' + testFileName, function () {
126      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
127      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
128      expect(outputFileContent).eql(expectFileContent);
129    });
130  });
131});