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 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 { ModuleHotreloadMode } from '../../../lib/fast_build/ark_compiler/module/module_hotreload_mode'; 30import { toUnixPath } from '../../../lib/utils'; 31import { 32 SOURCEMAPS, 33 EXTNAME_TS, 34 EXTNAME_ETS 35} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 36import { 37 ES2ABC_PATH, 38 SYMBOLMAP_MAP, 39 DEFAULT_ETS, 40 DEBUG_INFO, 41 SIMBOL_TABLE 42} from '../mock/rollup_mock/path_config'; 43import { 44 ENTRYABILITY_TS_PATH, 45 INDEX_ETS_PATH, 46 FILE, 47 SOURCE 48} from '../mock/rollup_mock/common'; 49import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap'; 50 51mocha.describe('test module_hotreload_mode file api', function () { 52 mocha.before(function () { 53 this.rollup = new RollUpPluginMock(); 54 }); 55 56 mocha.after(() => { 57 delete this.rollup; 58 }); 59 60 mocha.it('1-1: test updateSourceMapFromFileList under hot reload debug', function () { 61 this.rollup.hotReload(); 62 this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS; 63 const moduleMode = new ModuleHotreloadMode(this.rollup); 64 const fileList = this.rollup.getModuleIds(); 65 const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup); 66 67 for (const filePath of fileList) { 68 if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS)) { 69 const sourceMap: Map<string, string[]> = new Map<string, string[]>(); 70 const relativeSourceFilePath = 71 toUnixPath(filePath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, '')); 72 sourceMap[FILE] = path.basename(relativeSourceFilePath); 73 sourceMap[SOURCE] = [relativeSourceFilePath]; 74 sourceMapGenerator.updateSourceMap(filePath, sourceMap); 75 } 76 } 77 const fileListArray: Array<string> = [ 78 path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT), 79 path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT) 80 ]; 81 moduleMode.updateSourceMapFromFileList(fileListArray); 82 const sourceMapFilePath: string = path.join(this.rollup.share.projectConfig.patchAbcPath, SOURCEMAPS); 83 if (sourceMapFilePath && fs.existsSync(sourceMapFilePath)) { 84 const testObject = fs.readFileSync(sourceMapFilePath).toString(); 85 expect(testObject.indexOf(ENTRYABILITY_TS_PATH_DEFAULT) > 0 || 86 testObject.indexOf(ENTRYABILITY_JS_PATH_DEFAULT) > 0 || 87 testObject.indexOf(INDEX_ETS_PATH_DEFAULT) > 0).to.be.true; 88 } 89 90 let newSourceMaps = sourceMapGenerator.getSourceMaps(); 91 for (const key of Object.keys(newSourceMaps)) { 92 delete newSourceMaps[key]; 93 } 94 SourceMapGenerator.cleanSourceMapObject(); 95 }); 96 97 mocha.it('2-1: test addHotReloadArgs under hot reload debug', function () { 98 this.rollup.hotReload(); 99 this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS; 100 const moduleMode = new ModuleHotreloadMode(this.rollup); 101 moduleMode.addHotReloadArgs(); 102 expect(moduleMode.cmdArgs[0].indexOf(ES2ABC_PATH) > 0).to.be.true; 103 expect(moduleMode.cmdArgs[1] === DEBUG_INFO).to.be.true; 104 expect(moduleMode.cmdArgs[2] === SIMBOL_TABLE).to.be.true; 105 expect(moduleMode.cmdArgs[3].indexOf(SYMBOLMAP_MAP) > 0).to.be.true; 106 }); 107 108 mocha.it('3-1: test the error message of the ModuleHotreloadMode constructor', function () { 109 this.rollup.hotReload(); 110 this.rollup.share.projectConfig.oldMapFilePath = ''; 111 const stub = sinon.stub(this.rollup.share, 'throwArkTsCompilerError'); 112 try { 113 new ModuleHotreloadMode(this.rollup); 114 } catch (e) { 115 } 116 expect(stub.calledWith('ArkTS:INTERNAL ERROR: Hotreload failed, ' + 117 'symbolMap file is not correctly configured.')).to.be.true; 118 stub.restore(); 119 }); 120});