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 SysTestKit from '../kit/SysTestKit'; 17import fs from '@ohos.file.fs'; 18import {TAG} from '../../Constant'; 19 20const jsCoverageFileName = 'js_coverage.json'; 21 22export async function collectCoverageData() { 23 if (globalThis.__coverage__ === undefined) { 24 console.info(`${TAG} globalThis not have coverage`); 25 return; 26 } 27 const strJson = JSON.stringify(globalThis.__coverage__); 28 let testMode = globalThis.__testMode__; 29 console.info(`${TAG} coverage data testMode: ${testMode}`); 30 let savePath = globalThis.__savePath__; 31 console.info(`${TAG} write coverage data to: ${savePath}`); 32 let readPath = globalThis.__readPath__; 33 console.info(`${TAG} read coverage data in: ${readPath}`); 34 35 // run callback mode if local test or (save path and read path ) is not defined 36 if (!testMode || !isCoveragePathValid(savePath)) { 37 console.info(`${TAG} run coverage data in call back mode`); 38 const strLen = strJson.length; 39 const maxLen = 500; 40 const maxCount = Math.floor(strLen / maxLen); 41 const OHOS_REPORT_COVERAGE_DATA = 'OHOS_REPORT_COVERAGE_DATA:'; 42 for (let count = 0; count <= maxCount; count++) { 43 console.info(`${OHOS_REPORT_COVERAGE_DATA} ${strJson.substring(count * maxLen, (count + 1) * maxLen)}`); 44 await SysTestKit.print(`${OHOS_REPORT_COVERAGE_DATA} ${strJson.substring(count * maxLen, (count + 1) * maxLen)}`); 45 } 46 return; 47 } 48 console.info(`${TAG} run coverage data in save file mode`); 49 if (fs.accessSync(savePath)) { 50 fs.unlinkSync(savePath); 51 } 52 53 let inputPathDir = savePath.substring(0, savePath.length - jsCoverageFileName.length); 54 if (!fs.accessSync(inputPathDir)) { 55 console.info(`${TAG} coverage data create dir: ${inputPathDir}`); 56 fs.mkdirSync(inputPathDir); 57 } 58 59 let file = fs.openSync(savePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 60 let writeLen = fs.writeSync(file.fd, strJson, {encoding:'utf-8'}); 61 console.info(`${TAG} write coverage data success: ${writeLen}`); 62 fs.closeSync(file); 63 const OHOS_REPORT_COVERAGE_PATH = 'OHOS_REPORT_COVERAGE_PATH:'; 64 await SysTestKit.print(`${OHOS_REPORT_COVERAGE_PATH} ${readPath}`); 65 console.info(`${OHOS_REPORT_COVERAGE_PATH} ${readPath}`); 66} 67 68function isCoveragePathValid(inputPath) { 69 if (!inputPath) { 70 return false; 71 } 72 if (inputPath.indexOf(jsCoverageFileName) === -1) { 73 return false; 74 } 75 return true; 76}