161847f8eSopenharmony_ci/*
261847f8eSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
361847f8eSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
461847f8eSopenharmony_ci * you may not use this file except in compliance with the License.
561847f8eSopenharmony_ci * You may obtain a copy of the License at
661847f8eSopenharmony_ci *
761847f8eSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
861847f8eSopenharmony_ci *
961847f8eSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1061847f8eSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1161847f8eSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1261847f8eSopenharmony_ci * See the License for the specific language governing permissions and
1361847f8eSopenharmony_ci * limitations under the License.
1461847f8eSopenharmony_ci */
1561847f8eSopenharmony_ci
1661847f8eSopenharmony_ciconst { expect } = require('chai');
1761847f8eSopenharmony_ciconst { apiCheckInfoArr, removeDuplicateObj } = require('../../src/utils');
1861847f8eSopenharmony_ciconst { scanEntry } = require('../../src/api_check_plugin');
1961847f8eSopenharmony_ciconst path = require('path');
2061847f8eSopenharmony_ciconst fs = require('fs');
2161847f8eSopenharmony_ci
2261847f8eSopenharmony_cidescribe('diffSingleFile', function () {
2361847f8eSopenharmony_ci  const testCasesDir = path.join(__dirname, '..', '/ut');
2461847f8eSopenharmony_ci  const testCasesFileNames = fs.readdirSync(testCasesDir);
2561847f8eSopenharmony_ci  const expectFileDir = path.join(__dirname, '..', '/expect');
2661847f8eSopenharmony_ci  const outputFilePath = path.join(__dirname, '..', 'output');
2761847f8eSopenharmony_ci  if (!fs.existsSync(outputFilePath)) {
2861847f8eSopenharmony_ci    fs.mkdirSync(outputFilePath);
2961847f8eSopenharmony_ci  }
3061847f8eSopenharmony_ci  testCasesFileNames.forEach(fileName => {
3161847f8eSopenharmony_ci    const expectFilePath = path.resolve(expectFileDir, `${fileName.replace(/.d.ts/g, '.txt')}`);
3261847f8eSopenharmony_ci    it(`testDiff# ${fileName}`, function () {
3361847f8eSopenharmony_ci      let result = [];
3461847f8eSopenharmony_ci      const url = path.join(testCasesDir, fileName);
3561847f8eSopenharmony_ci      const prId = 'xxx';
3661847f8eSopenharmony_ci      fs.writeFileSync(path.join(__dirname, 'testMdfile.txt'), url);
3761847f8eSopenharmony_ci      scanEntry(path.join(__dirname, 'testMdfile.txt'), prId, true);
3861847f8eSopenharmony_ci      const apiCheckResultArr = removeDuplicateObj(apiCheckInfoArr);
3961847f8eSopenharmony_ci      result.push(apiCheckResultArr[apiCheckResultArr.length - 1]);
4061847f8eSopenharmony_ci      const resultFilePath = path.join(outputFilePath, `output_${fileName.replace(/.d.ts/g, '.txt')}`);
4161847f8eSopenharmony_ci      fs.writeFileSync(resultFilePath, JSON.stringify(result));
4261847f8eSopenharmony_ci      const resultFileContent = fs.readFileSync(resultFilePath, 'utf-8').replace(/\n|\r|\s/g, '');
4361847f8eSopenharmony_ci      const expectContent = fs.readFileSync(expectFilePath, 'utf-8').replace(/\n|\r|\s/g, '');
4461847f8eSopenharmony_ci      expect(resultFileContent).to.be.equal(expectContent);
4561847f8eSopenharmony_ci    });
4661847f8eSopenharmony_ci  });
4761847f8eSopenharmony_ci});
48