1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use rollupObject 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 16 17import { expect } from 'chai'; 18import mocha from 'mocha'; 19import path from "path"; 20import fs from "fs"; 21import sinon from 'sinon'; 22 23import { 24 ENTRYABILITY_TS_PATH_DEFAULT, 25 ENTRYABILITY_JS_PATH_DEFAULT, 26 INDEX_ETS_PATH_DEFAULT 27} from '../mock/rollup_mock/common'; 28import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 29import { ModuleColdreloadMode } from '../../../lib/fast_build/ark_compiler/module/module_coldreload_mode'; 30import { RELEASE } from '../../../lib/fast_build/ark_compiler/common/ark_define'; 31import { toUnixPath } from '../../../lib/utils'; 32import { 33 SOURCEMAPS, 34 EXTNAME_TS, 35 EXTNAME_ETS 36} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 37import { 38 ES2ABC_PATH, 39 SYMBOLMAP_MAP, 40 DEFAULT_ETS, 41 DEBUG_INFO, 42 SIMBOL_TABLE 43} from '../mock/rollup_mock/path_config'; 44import { 45 ENTRYABILITY_TS_PATH, 46 INDEX_ETS_PATH, 47 FILE, 48 SOURCE 49} from '../mock/rollup_mock/common'; 50import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap'; 51 52mocha.describe('test module_coldreload_mode file api', function () { 53 mocha.before(function () { 54 this.rollup = new RollUpPluginMock(); 55 }); 56 57 mocha.after(() => { 58 delete this.rollup; 59 }); 60 61 mocha.it('1-1: test the error message of the ModuleColdreloadMode constructor', function () { 62 this.rollup.coldReload(RELEASE); 63 this.rollup.share.projectConfig.oldMapFilePath = ''; 64 const stub = sinon.stub(this.rollup.share, 'throwArkTsCompilerError'); 65 try { 66 new ModuleColdreloadMode(this.rollup); 67 } catch (e) { 68 } 69 expect(stub.calledWith('ArkTS:INTERNAL ERROR: Coldreload failed, ' + 70 'symbolMap file is not correctly configured.')).to.be.true; 71 stub.restore(); 72 }); 73 74 mocha.it('1-2: test addColdReloadArgs under cold reload debug', function () { 75 this.rollup.coldReload(); 76 this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS; 77 const moduleMode = new ModuleColdreloadMode(this.rollup); 78 moduleMode.addColdReloadArgs(); 79 expect(moduleMode.cmdArgs[0].indexOf(ES2ABC_PATH) > 0).to.be.true; 80 expect(moduleMode.cmdArgs[1] === DEBUG_INFO).to.be.true; 81 expect(moduleMode.cmdArgs[2] === SIMBOL_TABLE).to.be.true; 82 expect(moduleMode.cmdArgs[3].indexOf(SYMBOLMAP_MAP) > 0).to.be.true; 83 }); 84 85 mocha.it('1-3: test addColdReloadArgs under cold reload release', function () { 86 this.rollup.coldReload(RELEASE); 87 this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS; 88 const moduleMode = new ModuleColdreloadMode(this.rollup); 89 moduleMode.addColdReloadArgs(); 90 expect(moduleMode.cmdArgs[0].indexOf(ES2ABC_PATH) > 0).to.be.true; 91 expect(moduleMode.cmdArgs[1] === DEBUG_INFO).to.be.false; 92 expect(moduleMode.cmdArgs[1] === SIMBOL_TABLE).to.be.true; 93 expect(moduleMode.cmdArgs[2].indexOf(SYMBOLMAP_MAP) > 0).to.be.true; 94 }); 95 96 mocha.it('1-4: test updateSourceMapFromFileList under cold reload debug', function () { 97 this.rollup.coldReload(); 98 this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS; 99 const moduleMode = new ModuleColdreloadMode(this.rollup); 100 const fileList = this.rollup.getModuleIds(); 101 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 102 103 for (const filePath of fileList) { 104 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS)) { 105 const sourceMap: Map<string, string[]> = new Map<string, string[]>(); 106 const relativeSourceFilePath = 107 toUnixPath(filePath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 108 sourceMap[FILE] = path.basename(relativeSourceFilePath); 109 sourceMap[SOURCE] = [relativeSourceFilePath]; 110 sourceMapGenerator.updateSourceMap(filePath, sourceMap); 111 } 112 } 113 const fileListArray: Array<string> = [ 114 path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT), 115 path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT) 116 ]; 117 moduleMode.updateSourceMapFromFileList(fileListArray); 118 const sourceMapFilePath: string = path.join(this.rollup.share.projectConfig.patchAbcPath, SOURCEMAPS); 119 if (sourceMapFilePath && fs.existsSync(sourceMapFilePath)) { 120 const testObject = fs.readFileSync(sourceMapFilePath).toString(); 121 expect(testObject.indexOf(ENTRYABILITY_TS_PATH_DEFAULT) > 0 || 122 testObject.indexOf(ENTRYABILITY_JS_PATH_DEFAULT) > 0 || 123 testObject.indexOf(INDEX_ETS_PATH_DEFAULT) > 0).to.be.true; 124 } 125 126 let newSourceMaps = sourceMapGenerator.getSourceMaps(); 127 for (const key of Object.keys(newSourceMaps)) { 128 delete newSourceMaps[key]; 129 } 130 SourceMapGenerator.cleanSourceMapObject(); 131 }); 132});