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 { FileUtils } from '../../src/utils/FileUtils'; 20import { FilesMap, Parser } from '../../src/coreImpl/parser/parser'; 21import { StringConstant } from '../../src/utils/Constant'; 22import { DiffHelper } from '../../src/coreImpl/diff/diff'; 23import { BasicDiffInfo } from '../../src/typedef/diff/ApiInfoDiff'; 24import { ApiStatisticsHelper } from '../../src/coreImpl/statistics/Statistics'; 25import { ApiStatisticsInfo } from '../../src/typedef/statistics/ApiStatistics' 26 27describe('testParserEachSince', function () { 28 const testFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/ut/parserSince'); 29 const outputFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/output/parserSince'); 30 const expectFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/expect/parserSince'); 31 const testFileNames: string[] = fs.readdirSync(testFileDir); 32 testFileNames.forEach((testFileName: string) => { 33 const baseName: string = path 34 .basename(testFileName) 35 .replace(/.d.ts/g, '') 36 .replace(/.d.ets/g, ''); 37 const testFilePath: string = path.join(testFileDir, testFileName); 38 const outputFilePath: string = path.join(outputFileDir, `${baseName}.json`); 39 const expectFilePath: string = path.join(expectFileDir, `${baseName}.json`); 40 it('\ntestFile#' + testFilePath + '\noutput:' + outputFilePath + '\nexpect:' + expectFilePath, function () { 41 if (fs.existsSync(outputFilePath)) { 42 fs.rmdirSync(outputFilePath, { recursive: true }); 43 } 44 if (!fs.existsSync(outputFileDir)) { 45 fs.mkdirSync(outputFileDir); 46 } 47 const outputContent: string = Parser.getParseEachSince(Parser.parseFile(testFileDir, testFilePath)); 48 fs.writeFileSync(outputFilePath, outputContent, StringConstant.UTF8); 49 const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n'); 50 expect(outputContent).eql(expectFileContent); 51 }); 52 }); 53}); 54 55describe('testParser', function () { 56 const testFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/ut/parser'); 57 const outputFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/output/parser'); 58 const expectFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/expect/parser'); 59 const testFileNames: string[] = fs.readdirSync(testFileDir); 60 testFileNames.forEach((testFileName: string) => { 61 const baseName: string = path 62 .basename(testFileName) 63 .replace(/.d.ts/g, '') 64 .replace(/.d.ets/g, ''); 65 const testFilePath: string = path.join(testFileDir, testFileName); 66 const outputFilePath: string = path.join(outputFileDir, `${baseName}.json`); 67 const expectFilePath: string = path.join(expectFileDir, `${baseName}.json`); 68 it('\ntestFile#' + testFilePath + '\noutput:' + outputFilePath + '\nexpect:' + expectFilePath, function () { 69 if (fs.existsSync(outputFilePath)) { 70 fs.rmdirSync(outputFilePath, { recursive: true }); 71 } 72 if (!fs.existsSync(outputFileDir)) { 73 fs.mkdirSync(outputFileDir); 74 } 75 const outputContent: string = Parser.getParseResults(Parser.parseFile(testFileDir, testFilePath)); 76 fs.writeFileSync(outputFilePath, outputContent, StringConstant.UTF8); 77 const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n'); 78 expect(outputContent).eql(expectFileContent); 79 }); 80 }); 81}); 82 83describe('testApiDiff', function () { 84 const testOldFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/ut/apiDiff/old'); 85 const testNewFileDir: string = testOldFileDir.replace('old', 'new'); 86 const outputFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/output/apiDiff'); 87 const expectFileDir: string = path.join(FileUtils.getBaseDirName(), '/test/expect/apiDiff'); 88 const testOldFileNames: string[] = fs.readdirSync(testOldFileDir); 89 testOldFileNames.forEach((testOldFileName: string) => { 90 const baseName: string = path 91 .basename(testOldFileName) 92 .replace(/.d.ts/g, '') 93 .replace(/.d.ets/g, ''); 94 const testOldFilePath: string = path.join(testOldFileDir, testOldFileName); 95 const testNewFilePath: string = testOldFilePath.replace('old', 'new'); 96 const outputFilePath: string = path.join(outputFileDir, `${baseName}.json`); 97 const expectFilePath: string = path.join(expectFileDir, `${baseName}.json`); 98 it('\ntestFile#' + testOldFilePath + '\noutput:' + outputFilePath + '\nexpect:' + expectFilePath, function () { 99 if (fs.existsSync(outputFilePath)) { 100 fs.rmdirSync(outputFilePath, { recursive: true }); 101 } 102 if (!fs.existsSync(outputFileDir)) { 103 fs.mkdirSync(outputFileDir); 104 } 105 const oldSDKApiMap: FilesMap = Parser.parseFile(testOldFileDir, testOldFilePath); 106 const newSDKApiMap: FilesMap = Parser.parseFile(testNewFileDir, testNewFilePath); 107 const diffInfos: BasicDiffInfo[] = DiffHelper.diffSDK(oldSDKApiMap, newSDKApiMap, false); 108 const outputContent: string = JSON.stringify(diffInfos, null, 2); 109 fs.writeFileSync(outputFilePath, outputContent, StringConstant.UTF8); 110 const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n'); 111 expect(outputContent).eql(expectFileContent); 112 }); 113 }); 114}); 115 116describe('testStatistics', function () { 117 const testFileDir: string = path.join(__dirname, '..', '/ut/apiStatistics'); 118 const outputFileDir: string = path.join(__dirname, '..', '/output/apiStatistics'); 119 const expectFileDir: string = path.join(__dirname, '..', '/expect/apiStatistics'); 120 const testFileNames: string[] = fs.readdirSync(testFileDir); 121 testFileNames.forEach((testFileName: string) => { 122 const baseName: string = path 123 .basename(testFileName) 124 .replace(/.d.ts/g, '') 125 .replace(/.d.ets/g, ''); 126 const testFilePath: string = path.join(testFileDir, testFileName); 127 const outputFilePath: string = path.join(outputFileDir, `${baseName}.json`); 128 const expectFilePath: string = path.join(expectFileDir, `${baseName}.json`); 129 it('\ntestFile#' + testFilePath + '\noutput:' + outputFilePath + '\nexpect:' + expectFilePath, function () { 130 if (!fs.existsSync(outputFileDir)) { 131 fs.mkdirSync(outputFileDir); 132 } 133 const apiInfos: ApiStatisticsInfo[] = ApiStatisticsHelper.getApiStatisticsInfos( 134 Parser.parseFile(testFileDir, testFilePath) 135 ).apiStatisticsInfos; 136 const outputContent: string = JSON.stringify(apiInfos, null, 2); 137 fs.writeFileSync(outputFilePath, outputContent, StringConstant.UTF8); 138 const expectFileContent: string = fs.readFileSync(expectFilePath, 'utf-8').replace(/\r\n/g, '\n'); 139 expect(outputContent).eql(expectFileContent); 140 }); 141 }); 142});