• 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
16const { expect } = require('chai');
17const { apiCheckInfoArr, removeDuplicateObj } = require('../../src/utils');
18const { scanEntry } = require('../../src/api_check_plugin');
19const path = require('path');
20const fs = require('fs');
21
22describe('diffSingleFile', function () {
23  const testCasesDir = path.join(__dirname, '..', '/ut');
24  const testCasesFileNames = fs.readdirSync(testCasesDir);
25  const expectFileDir = path.join(__dirname, '..', '/expect');
26  const outputFilePath = path.join(__dirname, '..', 'output');
27  if (!fs.existsSync(outputFilePath)) {
28    fs.mkdirSync(outputFilePath);
29  }
30  testCasesFileNames.forEach(fileName => {
31    const expectFilePath = path.resolve(expectFileDir, `${fileName.replace(/.d.ts/g, '.txt')}`);
32    it(`testDiff# ${fileName}`, function () {
33      let result = [];
34      const url = path.join(testCasesDir, fileName);
35      const prId = 'xxx';
36      fs.writeFileSync(path.join(__dirname, 'testMdfile.txt'), url);
37      scanEntry(path.join(__dirname, 'testMdfile.txt'), prId, true);
38      const apiCheckResultArr = removeDuplicateObj(apiCheckInfoArr);
39      result.push(apiCheckResultArr[apiCheckResultArr.length - 1]);
40      const resultFilePath = path.join(outputFilePath, `output_${fileName.replace(/.d.ts/g, '.txt')}`);
41      fs.writeFileSync(resultFilePath, JSON.stringify(result));
42      const resultFileContent = fs.readFileSync(resultFilePath, 'utf-8').replace(/\n|\r|\s/g, '');
43      const expectContent = fs.readFileSync(expectFilePath, 'utf-8').replace(/\n|\r|\s/g, '');
44      expect(resultFileContent).to.be.equal(expectContent);
45    });
46  });
47});
48