• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { expect } from 'chai';
17import path from 'path';
18import fs from 'fs';
19import { JSDocModifierImpl } from '../../src/core/entry';
20
21describe('testSingleFile', function () {
22  const testFileDir = path.join(__dirname, '..', '/ut/');
23  const outputFileDir = path.join(__dirname, '..', '/output/testSingleFile/');
24  const expectFileDir = path.join(__dirname, '..', '/expect/');
25  const testFileNames = fs.readdirSync(testFileDir);
26  const argLen = process.argv.length;
27  testFileNames.forEach((testFileName) => {
28    const testFilePath = path.join(testFileDir, testFileName);
29    const outputFilePath = path.join(outputFileDir, testFileName);
30    const expectFilePath = path.join(expectFileDir, testFileName);
31    const baseName: string = path.basename(testFileName, '.d.ts');
32    const expectReportFilePath: string = path.join(expectFileDir, `${baseName}.json`);
33    const outputReportFilePath: string = path.join(outputFileDir, `${baseName}.json`);
34    it('testFile#' + testFilePath, async function () {
35      if (fs.existsSync(outputFilePath)) {
36        fs.rmSync(outputFilePath);
37      }
38      const inputParams = [];
39      inputParams.push('-s');
40      inputParams.push('-i');
41      inputParams.push(testFilePath);
42      inputParams.push('-o');
43      inputParams.push(outputFilePath);
44      inputParams.push('-t');
45      process.argv.splice(argLen, inputParams.length, ...inputParams);
46      const testEntry = new JSDocModifierImpl();
47      await testEntry.start();
48
49      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
50      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
51      expect(outputFileContent).eql(expectFileContent);
52      const outputReportContent: string = fs.readFileSync(outputReportFilePath, 'utf-8').replace(/\r\n/g, '\n');
53      const expectReportContent: string = fs.readFileSync(expectReportFilePath, 'utf-8').replace(/\r\n/g, '\n');
54      expect(outputReportContent).eql(expectReportContent);
55    });
56  });
57});
58
59describe('testMultiFiles', function () {
60  const testFileDir = path.join(__dirname, '..', '/ut/');
61  const outFileDir = path.join(__dirname, '..', '/output/testMultiFiles/');
62  const expectFileDir = path.join(__dirname, '..', '/expect/');
63
64  before(async function () {
65    this.timeout(10000);
66    const inputParams = [];
67    inputParams.push('-s');
68    inputParams.push('-i');
69    inputParams.push(testFileDir);
70    inputParams.push('-o');
71    inputParams.push(outFileDir);
72    inputParams.push('-t');
73    process.argv.splice(process.argv.length, 0, ...inputParams);
74    const testEntry = new JSDocModifierImpl();
75    await testEntry.start();
76  });
77
78  const testFileNames = fs.readdirSync(testFileDir);
79  testFileNames.forEach((testFileName) => {
80    const testFilePath = path.join(testFileDir, testFileName);
81    const outputFilePath = path.join(outFileDir, testFileName);
82    const expectFilePath = path.join(expectFileDir, testFileName);
83    it('testDir#' + testFilePath, function () {
84      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
85      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
86      expect(outputFileContent).eql(expectFileContent);
87    });
88  });
89});
90
91describe('testBundleSingleFile', function () {
92  const testFileDir = path.join(__dirname, '..', '/ut/');
93  const outFileDir = path.join(__dirname, '..', '/output/testBundleSingleFile/');
94  const expectFileDir = path.join(__dirname, '..', '/expect/');
95  const testFileNames = fs.readdirSync(testFileDir);
96  const nodeExecute = process.execPath;
97  const { execFileSync } = require('child_process');
98  testFileNames.forEach((testFileName) => {
99    const testFilePath = path.join(testFileDir, testFileName);
100    const outputFilePath = path.join(outFileDir, testFileName);
101    const expectFilePath = path.join(expectFileDir, testFileName);
102    it('bundle_file#' + testFileName, function () {
103      if (fs.existsSync(outputFilePath)) {
104        fs.rmSync(outputFilePath);
105      }
106      execFileSync(nodeExecute, [
107        'build/bundle.js', '-s',
108        '-i', `${testFilePath}`,
109        '-o', `${outputFilePath}`,
110        '-t'
111      ]);
112      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
113      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
114      expect(outputFileContent).eql(expectFileContent);
115    });
116  });
117});
118
119describe('testBundleMultiFiles', function () {
120  const testFileDir = path.join(__dirname, '..', '/ut/');
121  const outFileDir = path.join(__dirname, '..', '/output/testBundleMultiFiles/');
122  const expectFileDir = path.join(__dirname, '..', '/expect/');
123  const nodeExecute = process.execPath;
124  const { execFileSync } = require('child_process');
125
126  before(function () {
127    this.timeout(10000);
128    execFileSync(nodeExecute, [
129      'build/bundle.js', '-s',
130      '-i', `${testFileDir}`,
131      '-o', `${outFileDir}`,
132      '-t'
133    ]);
134  });
135
136  const testFileNames = fs.readdirSync(testFileDir);
137  testFileNames.forEach((testFileName) => {
138    const outputFilePath = path.join(outFileDir, testFileName);
139    const expectFilePath = path.join(expectFileDir, testFileName);
140    it('bundle_dir#' + testFileName, function () {
141      const outputFileContent: string = fs.readFileSync(outputFilePath, 'utf-8').replace(/\r\n/g, '\n');
142      const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n');
143      expect(outputFileContent).eql(expectFileContent);
144    });
145  });
146});