1/* 2 * Copyright (c) 2023-2025 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 16import { expect } from 'chai'; 17import mocha from 'mocha'; 18import sinon from 'sinon'; 19import fs from 'fs'; 20 21import { 22 getOhmUrlByFilepath, 23 getOhmUrlByExternalPackage, 24 getOhmUrlBySystemApiOrLibRequest, 25 getNormalizedOhmUrlByFilepath, 26 getNormalizedOhmUrlByAliasName, 27 getNormalizedOhmUrlByModuleRequest, 28 pkgDeclFilesConfig, 29 OhmUrlParams 30} from '../../../../lib/ark_utils'; 31import { PACKAGES } from '../../../../lib/pre_define'; 32import projectConfig from '../../utils/processProjectConfig'; 33import { projectConfig as mainProjectConfig } from '../../../../main'; 34import RollUpPluginMock from '../../mock/rollup_mock/rollup_plugin_mock'; 35import { GEN_ABC_PLUGIN_NAME } from '../../../../lib/fast_build/ark_compiler/common/ark_define'; 36import { ModuleSourceFile } from '../../../../lib/fast_build/ark_compiler/module/module_source_file'; 37import { 38 ArkTSErrorDescription, 39 ArkTSInternalErrorDescription, 40 ErrorCode 41} from '../../../../lib/fast_build/ark_compiler/error_code'; 42import { 43 CommonLogger, 44 LogData, 45 LogDataFactory 46} from '../../../../lib/fast_build/ark_compiler/logger'; 47import { PreloadFileModules } from '../../../../lib/fast_build/ark_compiler/module/module_preload_file_utils'; 48 49const PRVIEW_MOCK_CONFIG : Object = { 50 // system api mock 51 "@ohos.bluetooth": { 52 "source": "src/main/mock/ohos/bluetooth.mock.ts" 53 }, 54 // local function mock 55 "./src/main/ets/calc": { 56 "source": "src/main/mock/module/calc.mock.ts" 57 }, 58 // ohpm dependency mock 59 "lib": { 60 "source": "src/main/mock/module/bigInt.mock.ts" 61 }, 62 // native mock 63 "libentry.so": { 64 "source": "src/main/mock/native/libentry.mock.ts" 65 } 66} 67 68const MOCK_CONFIG_FILEPATH = { 69 'lib': `${projectConfig.projectRootPath}/oh_modules/lib/dist/index.js`, 70 './src/main/ets/calc': `${projectConfig.projectRootPath}/entry/src/main/ets/calc.ets`, 71} 72 73mocha.describe('generate ohmUrl', function () { 74 mocha.before(function () { 75 this.rollup = new RollUpPluginMock(); 76 }); 77 78 mocha.after(() => { 79 delete this.rollup; 80 }); 81 82 mocha.it('nested src main ets|js in filePath', function () { 83 const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` 84 + `subfeature/src/main/ets/pages/test.ts`; 85 const moduleName: string = 'entry'; 86 const moduleNamespace: string = 'library'; 87 let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 88 let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); 89 let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 90 let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 91 expect(ohmUrl_1 == expected_1).to.be.true; 92 expect(ohmUrl_2 == expected_2).to.be.true; 93 }); 94 95 mocha.it('nested src ohosTest ets|js in filePath', function () { 96 const filePath: string = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` 97 + `subfeature/src/main/ets/pages/test.ts`; 98 const moduleName: string = 'entry'; 99 const moduleNamespace: string = 'library'; 100 let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 101 let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); 102 let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 103 let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 104 expect(ohmUrl_1 == expected_1).to.be.true; 105 expect(ohmUrl_2 == expected_2).to.be.true; 106 }); 107 108 mocha.it('system builtins & app builtins', function () { 109 mainProjectConfig.bundleName = 'UtTestApplication'; 110 mainProjectConfig.moduleName = 'entry'; 111 const systemModuleRequest: string = '@system.app'; 112 const ohosModuleRequest: string = '@ohos.hilog'; 113 const appSoModuleRequest: string = 'libapplication.so'; 114 const systemParams: OhmUrlParams = { 115 moduleRequest:systemModuleRequest, 116 moduleId: '', 117 config: ModuleSourceFile.projectConfig, 118 logger: ModuleSourceFile.logger, 119 importerFile: undefined, 120 }; 121 const systemOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(systemParams); 122 const ohosParams: OhmUrlParams = { 123 moduleRequest: ohosModuleRequest, 124 moduleId: '', 125 config: ModuleSourceFile.projectConfig, 126 logger: ModuleSourceFile.logger, 127 importerFile: undefined, 128 }; 129 const ohosOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(ohosParams); 130 const appSoParams: OhmUrlParams = { 131 moduleRequest: appSoModuleRequest, 132 moduleId: '', 133 config: ModuleSourceFile.projectConfig, 134 logger: ModuleSourceFile.logger, 135 importerFile: undefined, 136 }; 137 const appOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(appSoParams); 138 const expectedSystemOhmUrl: string = '@native:system.app'; 139 const expectedOhosOhmUrl: string = '@ohos:hilog'; 140 const expectedappOhmUrl: string = '@app:UtTestApplication/entry/application'; 141 expect(systemOhmUrl == expectedSystemOhmUrl).to.be.true; 142 expect(ohosOhmUrl == expectedOhosOhmUrl).to.be.true; 143 expect(appOhmUrl == expectedappOhmUrl).to.be.true; 144 }); 145 146 mocha.it('shared library', function () { 147 this.rollup.build(); 148 const sharedLibraryPackageName: string = "@ohos/sharedLibrary"; 149 const sharedLibraryPackageNameSlashes: string = "@ohos/library///"; 150 const sharedLibraryPage: string = "@ohos/sharedLibrary/src/main/ets/pages/page1"; 151 const errorSharedLibrary: string = "@ohos/staticLibrary"; 152 const sharedLibraryModuleRequest = "@ohos/library////\\\\/"; 153 const sharedLibraryModuleRequestByPath = "@ohos/library/src/main/ets////"; 154 const sharedLibraryPageIndex = "@ohos/library/src/main/ets/pages"; 155 const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPackageName, projectConfig, this.rollup); 156 const sharedLibraryPackageNameSlashesOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPackageNameSlashes, projectConfig, this.rollup, ModuleSourceFile.logger, true); 157 const sharedLibraryPageOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPage, projectConfig, this.rollup); 158 const errorSharedLibraryOhmUrl = getOhmUrlByExternalPackage(errorSharedLibrary, projectConfig, this.rollup); 159 const sharedLibraryModuleRequestOhmUrl = getOhmUrlByExternalPackage(sharedLibraryModuleRequest, projectConfig, this.rollup, ModuleSourceFile.logger, true); 160 const sharedLibraryModuleRequestByPathOhmUrl = getOhmUrlByExternalPackage(sharedLibraryModuleRequestByPath, projectConfig, this.rollup, ModuleSourceFile.logger, true); 161 const sharedLibraryPageIndexOhmUrl = getOhmUrlByExternalPackage(sharedLibraryPageIndex, projectConfig, this.rollup, ModuleSourceFile.logger); 162 const expectedSharedLibraryOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/index"; 163 const expectedSharedLibrarySlashesOhmUrl: string = "@normalized:N&&&@ohos/library/Index&1.0.0"; 164 const expectedSharedLibraryPageOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/pages/page1"; 165 const expectedErrorSharedLibraryOhmUrl = undefined; 166 const expectsharedLibraryModuleRequestOhmUrl = "@normalized:N&&&@ohos/library/Index&1.0.0"; 167 const expectsharedLibraryModuleRequestByPathOhmUrl = "@normalized:N&&&@ohos/library/src/main/ets/Index&1.0.0"; 168 const expectsharedLibraryPageIndexOhmUrl = "@bundle:UtTestApplication/library/ets/pages/index"; 169 expect(sharedLibraryPackageNameOhmUrl == expectedSharedLibraryOhmUrl).to.be.true; 170 expect(sharedLibraryPackageNameSlashesOhmUrl == expectedSharedLibrarySlashesOhmUrl).to.be.true; 171 expect(sharedLibraryPageOhmUrl == expectedSharedLibraryPageOhmUrl).to.be.true; 172 expect(errorSharedLibraryOhmUrl == expectedErrorSharedLibraryOhmUrl).to.be.true; 173 expect(sharedLibraryModuleRequestOhmUrl == expectsharedLibraryModuleRequestOhmUrl).to.be.true; 174 expect(sharedLibraryModuleRequestByPathOhmUrl == expectsharedLibraryModuleRequestByPathOhmUrl).to.be.true; 175 expect(sharedLibraryPageIndexOhmUrl == expectsharedLibraryPageIndexOhmUrl).to.be.true; 176 177 }); 178 179 mocha.it('project module', function () { 180 const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; 181 const harFilePath = `${projectConfig.projectRootPath}/library/src/main/ets/pages/test.ts`; 182 const moduleName: string = 'entry'; 183 const moduleNamespace: string = 'library'; 184 const ohmUrl = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 185 const harOhmUrl = getOhmUrlByFilepath(harFilePath, projectConfig, undefined, moduleNamespace); 186 const expected = 'UtTestApplication/entry/ets/pages/test'; 187 const harOhmUrlExpected = 'UtTestApplication/entry@library/ets/pages/test'; 188 expect(ohmUrl == expected).to.be.true; 189 expect(harOhmUrl == harOhmUrlExpected).to.be.true; 190 }); 191 192 mocha.it('thirdParty module', function () { 193 const moduleLevelPkgPath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 194 const projectLevelPkgPath = `${projectConfig.projectRootPath}/oh_modules/json5/dist/index.js`; 195 const moduleName: string = 'entry'; 196 const moduleLevelPkgOhmUrl = getOhmUrlByFilepath(moduleLevelPkgPath, projectConfig, undefined, undefined); 197 const projectLevelPkgOhmUrl = getOhmUrlByFilepath(projectLevelPkgPath, projectConfig, undefined, undefined); 198 const moduleLevelPkgOhmUrlExpected = `${PACKAGES}@${moduleName}/json5/dist/index`; 199 const projectLevelPkgOhmUrlExpected = `${PACKAGES}/json5/dist/index`; 200 expect(moduleLevelPkgOhmUrl == moduleLevelPkgOhmUrlExpected).to.be.true; 201 expect(projectLevelPkgOhmUrl == projectLevelPkgOhmUrlExpected).to.be.true; 202 }); 203 204 mocha.it('static library entry', function () { 205 const staticLibraryEntry = `${projectConfig.projectRootPath}/library/index.ets`; 206 const moduleNamespace: string = 'library'; 207 const staticLibraryEntryOhmUrl = 208 getOhmUrlByFilepath(staticLibraryEntry, projectConfig, undefined, moduleNamespace); 209 const staticLibraryEntryOhmUrlExpected = 'UtTestApplication/entry@library/index'; 210 expect(staticLibraryEntryOhmUrl == staticLibraryEntryOhmUrlExpected).to.be.true; 211 }); 212 213 mocha.it('ohosTest module', function () { 214 const ohosTestfilePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/pages/test.ts`; 215 const moduleName: string = 'entry'; 216 const ohmUrl = getOhmUrlByFilepath(ohosTestfilePath, projectConfig, undefined, moduleName); 217 const expected = 'UtTestApplication/entry/ets/pages/test'; 218 expect(ohmUrl == expected).to.be.true; 219 }); 220 221 mocha.it('the error message of processPackageDir', function () { 222 this.rollup.build(); 223 projectConfig.modulePathMap = {}; 224 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 225 const moduleName: string = 'entry'; 226 const importerFile: string = 'importTest.ts'; 227 const errInfo: LogData = LogDataFactory.newInstance( 228 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 229 ArkTSErrorDescription, 230 'Failed to resolve OhmUrl. ' + 231 'Failed to get a resolved OhmUrl for "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 232 '', 233 ['Check whether the module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 234 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 235 ); 236 const logger = CommonLogger.getInstance(this.rollup); 237 const stub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 238 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 239 expect(stub.calledWith(errInfo)).to.be.true; 240 stub.restore(); 241 }); 242 243 mocha.it('the error message of processPackageDir without getHvigorConsoleLogger', function () { 244 this.rollup.build(); 245 projectConfig.modulePathMap = {}; 246 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 247 const moduleName: string = 'entry'; 248 const importerFile: string = 'importTest.ts'; 249 const errInfo: LogData = LogDataFactory.newInstance( 250 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 251 ArkTSErrorDescription, 252 'Failed to resolve OhmUrl. ' + 253 'Failed to get a resolved OhmUrl for "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 254 '', 255 ['Check whether the module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 256 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 257 ); 258 CommonLogger.destroyInstance(); 259 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 260 this.rollup.share.getHvigorConsoleLogger = undefined; 261 const logger = CommonLogger.getInstance(this.rollup); 262 const stub = sinon.stub(logger.logger, 'error'); 263 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 264 expect(stub.calledWith(errInfo.toString())).to.be.true; 265 CommonLogger.destroyInstance(); 266 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 267 stub.restore(); 268 }); 269 270 mocha.it('the error message of processPackageDir(packageDir is invalid value)', function () { 271 this.rollup.build(); 272 projectConfig.packageDir = undefined; 273 projectConfig.modulePathMap = {}; 274 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 275 const moduleName: string = 'entry'; 276 const importerFile: string = 'importTest.ts'; 277 const errInfo: LogData = LogDataFactory.newInstance( 278 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 279 ArkTSErrorDescription, 280 'Failed to resolve OhmUrl. Failed to get a resolved OhmUrl for ' + 281 '"/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 282 '', 283 ['Check whether the module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 284 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 285 ); 286 const logger = CommonLogger.getInstance(this.rollup); 287 const stub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 288 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 289 expect(stub.calledWith(errInfo)).to.be.true; 290 stub.restore(); 291 }); 292 293 mocha.it('the error message of processPackageDir(packageDir is invalid value) ' + 294 'without getHvigorConsoleLogger', function () { 295 this.rollup.build(); 296 projectConfig.packageDir = undefined; 297 projectConfig.modulePathMap = {}; 298 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 299 const moduleName: string = 'entry'; 300 const importerFile: string = 'importTest.ts'; 301 const errInfo: LogData = LogDataFactory.newInstance( 302 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 303 ArkTSErrorDescription, 304 'Failed to resolve OhmUrl. Failed to get a resolved OhmUrl for ' + 305 '"/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 306 '', 307 ['Check whether the module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 308 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 309 ); 310 CommonLogger.destroyInstance(); 311 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 312 this.rollup.share.getHvigorConsoleLogger = undefined; 313 const logger = CommonLogger.getInstance(this.rollup); 314 const stub = sinon.stub(logger.logger, 'error'); 315 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 316 expect(stub.calledWith(errInfo.toString())).to.be.true; 317 CommonLogger.destroyInstance(); 318 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 319 stub.restore(); 320 }); 321 322 mocha.it('NormalizedOHMUrl inter-app hsp self import', function () { 323 this.rollup.build(); 324 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 325 this.rollup.share.projectConfig.pkgContextInfo = { 326 'pkghsp': { 327 'packageName': 'pkghsp', 328 'bundleName': 'com.test.testHsp', 329 'moduleName': '', 330 'version': '', 331 'entryPath': 'Index.ets', 332 'isSO': false 333 } 334 } 335 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 336 const moduleInfo = { 337 id: filePath, 338 meta: { 339 pkgName: 'pkghsp', 340 pkgPath: '/testHsp/hsp' 341 } 342 } 343 this.rollup.moduleInfos.push(moduleInfo); 344 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 345 const relativePath: string = '../utils/Calc'; 346 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 347 const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; 348 const moduleSourceFile: string = new ModuleSourceFile(); 349 ModuleSourceFile.initPluginEnv(this.rollup); 350 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 351 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 352 importerFile); 353 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 354 importerFile); 355 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; 356 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 357 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 358 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 359 }); 360 361 mocha.it('NormalizedOHMUrl inter-app hsp others import', function () { 362 this.rollup.build(); 363 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 364 this.rollup.share.projectConfig.pkgContextInfo = { 365 'pkghsp': { 366 'packageName': 'pkghsp', 367 'bundleName': 'com.test.testHsp', 368 'moduleName': 'hsp', 369 'version': '', 370 'entryPath': 'Index.ets', 371 'isSO': false 372 } 373 } 374 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 375 ['pkghsp_alias', 'pkghsp'] 376 ]); 377 this.rollup.share.projectConfig.harNameOhmMap = { 378 'pkghsp_alias': '@bundle:com.test.testHsp/src/main/ets/utils/Calc' 379 } 380 const filePath: string = 'pkghsp/src/main/ets/utils/Calc'; 381 const indexFilePath: string = 'pkghsp_alias'; 382 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 383 const importByPkgName = 'pkghsp_alias'; 384 const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 385 const moduleSourceFile: string = new ModuleSourceFile(); 386 ModuleSourceFile.initPluginEnv(this.rollup); 387 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 388 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 389 importerFile); 390 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&com.test.testHsp&pkghsp/Index&'; 391 const standardImportPathNormalizedOhmUrl: string = 392 '@normalized:N&hsp&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; 393 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 394 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 395 }); 396 397 mocha.it('NormalizedOHMUrl in-app hsp self import', function () { 398 this.rollup.build(); 399 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 400 this.rollup.share.projectConfig.pkgContextInfo = { 401 'pkghsp': { 402 'packageName': 'pkghsp', 403 'bundleName': '', 404 'moduleName': '', 405 'version': '', 406 'entryPath': 'Index.ets', 407 'isSO': false 408 } 409 } 410 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 411 const moduleInfo = { 412 id: filePath, 413 meta: { 414 pkgName: 'pkghsp', 415 pkgPath: '/testHsp/hsp' 416 } 417 } 418 this.rollup.moduleInfos.push(moduleInfo); 419 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 420 const relativePath: string = '../utils/Calc'; 421 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 422 const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; 423 const moduleSourceFile: string = new ModuleSourceFile(); 424 ModuleSourceFile.initPluginEnv(this.rollup); 425 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 426 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 427 importerFile); 428 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 429 importerFile); 430 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghsp/src/main/ets/utils/Calc&'; 431 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 432 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 433 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 434 }); 435 436 mocha.it('NormalizedOHMUrl in-app hsp others import', function () { 437 this.rollup.build(); 438 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 439 this.rollup.share.projectConfig.pkgContextInfo = { 440 'pkghsp': { 441 'packageName': 'pkghsp', 442 'bundleName': '', 443 'moduleName': 'hsp', 444 'version': '', 445 'entryPath': 'Index.ets', 446 'isSO': false 447 } 448 } 449 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 450 ['pkghsp_alias', 'pkghsp'] 451 ]); 452 this.rollup.share.projectConfig.harNameOhmMap = { 453 'pkghsp_alias': '@bundle:com.test.testHap/src/main/ets/utils/Calc' 454 } 455 const filePath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 456 const indexFilePath: string = 'pkghsp_alias'; 457 458 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 459 const importByPkgName = 'pkghsp_alias'; 460 const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 461 const moduleSourceFile: string = new ModuleSourceFile(); 462 ModuleSourceFile.initPluginEnv(this.rollup); 463 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 464 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 465 importerFile); 466 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/Index&'; 467 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/src/main/ets/utils/Calc&'; 468 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 469 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 470 }); 471 472 mocha.it('NormalizedOHMUrl hap self import', function () { 473 this.rollup.build(); 474 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 475 this.rollup.share.projectConfig.pkgContextInfo = { 476 'entry': { 477 'packageName': 'entry', 478 'bundleName': '', 479 'moduleName': '', 480 'version': '', 481 'entryPath': '', 482 'isSO': false 483 } 484 } 485 const filePath: string = '/testHap/entry/src/main/ets/utils/Calc.ets'; 486 const moduleInfo = { 487 id: filePath, 488 meta: { 489 pkgName: 'entry', 490 pkgPath: '/testHap/entry' 491 } 492 } 493 this.rollup.moduleInfos.push(moduleInfo); 494 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 495 const relativePath: string = '../utils/Calc'; 496 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 497 const standardImportPath: string = 'entry/src/main/ets/utils/Calc'; 498 const moduleSourceFile: string = new ModuleSourceFile(); 499 ModuleSourceFile.initPluginEnv(this.rollup); 500 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 501 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 502 importerFile); 503 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 504 importerFile); 505 const expectedNormalizedOhmUrl: string = '@normalized:N&&&entry/src/main/ets/utils/Calc&'; 506 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 507 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 508 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 509 }); 510 511 mocha.it('NormalizedOHMUrl source code har self import (hap/in-app hsp)', function () { 512 this.rollup.build(); 513 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 514 this.rollup.share.projectConfig.pkgContextInfo = { 515 'pkghar': { 516 'packageName': 'pkghar', 517 'bundleName': '', 518 'moduleName': '', 519 'version': '1.0.1', 520 'entryPath': 'Index.ets', 521 'isSO': false 522 } 523 } 524 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 525 const moduleInfo = { 526 id: filePath, 527 meta: { 528 pkgName: 'pkghar', 529 pkgPath: '/testHar/har' 530 } 531 } 532 this.rollup.moduleInfos.push(moduleInfo); 533 const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' 534 const relativePath: string = '../utils/Calc'; 535 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 536 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 537 const moduleSourceFile: string = new ModuleSourceFile(); 538 ModuleSourceFile.initPluginEnv(this.rollup); 539 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 540 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 541 importerFile); 542 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 543 importerFile); 544 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 545 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 546 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 547 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 548 }); 549 550 mocha.it('NormalizedOHMUrl source code har others import (hap/in-app hsp)', function () { 551 this.rollup.build(); 552 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 553 this.rollup.share.projectConfig.pkgContextInfo = { 554 'pkghar': { 555 'packageName': 'pkghar', 556 'bundleName': '', 557 'moduleName': '', 558 'version': '1.0.1', 559 'entryPath': 'Index.ets', 560 'isSO': false 561 } 562 } 563 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 564 const indexFilePath: string = '/testHar/har/Index.ets'; 565 for (let file of [filePath, indexFilePath]) { 566 const moduleInfo = { 567 id: file, 568 meta: { 569 pkgName: 'pkghar', 570 pkgPath: '/testHar/har' 571 } 572 } 573 this.rollup.moduleInfos.push(moduleInfo); 574 } 575 const importerFile: string = '/testHar/entry/src/main/ets/pages/Index.ets' 576 const importByPkgName = 'pkghar'; 577 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 578 const moduleSourceFile: string = new ModuleSourceFile(); 579 ModuleSourceFile.initPluginEnv(this.rollup); 580 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 581 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 582 importerFile); 583 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; 584 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 585 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 586 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 587 }); 588 589 mocha.it('NormalizedOHMUrl source code har self import (inter-app hsp)', function () { 590 this.rollup.build(); 591 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 592 this.rollup.share.projectConfig.pkgContextInfo = { 593 'pkghar': { 594 'packageName': 'pkghar', 595 'bundleName': 'com.test.testHsp', 596 'moduleName': '', 597 'version': '1.0.1', 598 'entryPath': 'Index.ets', 599 'isSO': false 600 } 601 } 602 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 603 const moduleInfo = { 604 id: filePath, 605 meta: { 606 pkgName: 'pkghar', 607 pkgPath: '/testHsp/har' 608 } 609 } 610 this.rollup.moduleInfos.push(moduleInfo); 611 const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' 612 const relativePath: string = '../utils/Calc'; 613 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 614 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 615 const moduleSourceFile: string = new ModuleSourceFile(); 616 ModuleSourceFile.initPluginEnv(this.rollup); 617 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 618 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 619 importerFile); 620 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 621 importerFile); 622 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 623 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 624 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 625 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 626 }); 627 628 mocha.it('NormalizedOHMUrl source code har others import (inter-app hsp)', function () { 629 this.rollup.build(); 630 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 631 this.rollup.share.projectConfig.pkgContextInfo = { 632 'pkghar': { 633 'packageName': 'pkghar', 634 'bundleName': 'com.test.testHsp', 635 'moduleName': '', 636 'version': '1.0.1', 637 'entryPath': 'Index.ets', 638 'isSO': false 639 } 640 } 641 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 642 const indexFilePath: string = '/testHsp/har/Index.ets'; 643 for (let file of [filePath, indexFilePath]) { 644 const moduleInfo = { 645 id: file, 646 meta: { 647 pkgName: 'pkghar', 648 pkgPath: '/testHsp/har' 649 } 650 } 651 this.rollup.moduleInfos.push(moduleInfo); 652 } 653 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 654 const importByPkgName = 'pkghar'; 655 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 656 const moduleSourceFile: string = new ModuleSourceFile(); 657 ModuleSourceFile.initPluginEnv(this.rollup); 658 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 659 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 660 importerFile); 661 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; 662 const standardImportPathNormalizedOhmUrl: string = 663 '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 664 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 665 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 666 }); 667 668 mocha.it('NormalizedOHMUrl product har self import (hap/in-app hsp)', function () { 669 this.rollup.build(); 670 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 671 this.rollup.share.projectConfig.pkgContextInfo = { 672 'pkghar': { 673 'packageName': 'pkghar', 674 'bundleName': '', 675 'moduleName': '', 676 'version': '1.0.1', 677 'entryPath': 'Index.ets', 678 'isSO': false 679 } 680 } 681 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 682 const moduleInfo = { 683 id: filePath, 684 meta: { 685 pkgName: 'pkghar', 686 pkgPath: '/testHar/har' 687 } 688 } 689 this.rollup.moduleInfos.push(moduleInfo); 690 const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' 691 const relativePath: string = '../utils/Calc'; 692 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 693 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 694 const moduleSourceFile: string = new ModuleSourceFile(); 695 ModuleSourceFile.initPluginEnv(this.rollup); 696 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 697 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 698 importerFile); 699 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 700 importerFile); 701 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 702 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 703 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 704 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 705 }); 706 707 mocha.it('NormalizedOHMUrl product har others import (hap/in-app hsp)', function () { 708 this.rollup.build(); 709 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 710 this.rollup.share.projectConfig.pkgContextInfo = { 711 'pkghar': { 712 'packageName': 'pkghar', 713 'bundleName': '', 714 'moduleName': '', 715 'version': '1.0.1', 716 'entryPath': 'Index.ets', 717 'isSO': false 718 } 719 } 720 const filePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/src/main/ets/utils/Calc.ets'; 721 const indexFilePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/Index.ets'; 722 for (let file of [filePath, indexFilePath]) { 723 const moduleInfo = { 724 id: file, 725 meta: { 726 pkgName: 'pkghar', 727 pkgPath: '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar' 728 } 729 } 730 this.rollup.moduleInfos.push(moduleInfo); 731 } 732 const importerFile: string = '/testHar/entry/src/main/ets/pages/index.ets' 733 const importByPkgName = 'pkghar'; 734 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 735 const moduleSourceFile: string = new ModuleSourceFile(); 736 ModuleSourceFile.initPluginEnv(this.rollup); 737 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 738 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 739 importerFile); 740 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; 741 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 742 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 743 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 744 }); 745 746 mocha.it('NormalizedOHMUrl remote source code har self import (inter-app hsp)', function () { 747 this.rollup.build(); 748 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 749 this.rollup.share.projectConfig.pkgContextInfo = { 750 'pkghar': { 751 'packageName': 'pkghar', 752 'bundleName': 'com.test.testHsp', 753 'moduleName': '', 754 'version': '1.0.1', 755 'entryPath': 'Index.ets', 756 'isSO': false 757 } 758 } 759 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 760 const moduleInfo = { 761 id: filePath, 762 meta: { 763 pkgName: 'pkghar', 764 pkgPath: '/testHsp/har' 765 } 766 } 767 this.rollup.moduleInfos.push(moduleInfo); 768 const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' 769 const relativePath: string = '../utils/Calc'; 770 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 771 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 772 const moduleSourceFile: string = new ModuleSourceFile(); 773 ModuleSourceFile.initPluginEnv(this.rollup); 774 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 775 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 776 importerFile); 777 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 778 importerFile); 779 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 780 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 781 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 782 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 783 }); 784 785 mocha.it('NormalizedOHMUrl remote source code har others import (inter-app hsp)', function () { 786 this.rollup.build(); 787 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 788 this.rollup.share.projectConfig.pkgContextInfo = { 789 'pkghar': { 790 'packageName': 'pkghar', 791 'bundleName': 'com.test.testHsp', 792 'moduleName': '', 793 'version': '1.0.1', 794 'entryPath': 'Index.ets', 795 'isSO': false 796 } 797 } 798 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 799 const indexFilePath: string = '/testHsp/har/Index.ets'; 800 for (let file of [filePath, indexFilePath]) { 801 const moduleInfo = { 802 id: file, 803 meta: { 804 pkgName: 'pkghar', 805 pkgPath: '/testHsp/har' 806 } 807 } 808 this.rollup.moduleInfos.push(moduleInfo); 809 } 810 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 811 const importByPkgName = 'pkghar'; 812 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 813 const moduleSourceFile: string = new ModuleSourceFile(); 814 ModuleSourceFile.initPluginEnv(this.rollup); 815 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 816 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 817 importerFile); 818 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; 819 const standardImportPathNormalizedOhmUrl: string = 820 '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 821 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 822 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 823 }); 824 825 mocha.it('NormalizedOHMUrl native so others import (hap/in-app hsp)', function () { 826 this.rollup.build(); 827 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 828 this.rollup.share.projectConfig.pkgContextInfo = { 829 'libproduct.so': { 830 'packageName': 'libproduct.so', 831 'bundleName': '', 832 'moduleName': '', 833 'version': '', 834 'entryPath': '', 835 'isSO': true 836 } 837 } 838 const importerFile: string = '/testHap/hsp/src/main/ets/pages/Index.ets' 839 const moduleRequest = 'libproduct.so'; 840 const moduleSourceFile: string = new ModuleSourceFile(); 841 ModuleSourceFile.initPluginEnv(this.rollup); 842 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 843 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libproduct.so&'; 844 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 845 }); 846 847 mocha.it('NormalizedOHMUrl native so others import (inter-app hsp)', function () { 848 this.rollup.build(); 849 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 850 this.rollup.share.projectConfig.pkgContextInfo = { 851 'libproduct.so': { 852 'packageName': 'libproduct.so', 853 'bundleName': 'com.test.testHsp', 854 'moduleName': '', 855 'version': '', 856 'entryPath': '', 857 'isSO': true 858 } 859 } 860 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 861 const moduleRequest = 'libproduct.so'; 862 const moduleSourceFile: string = new ModuleSourceFile(); 863 ModuleSourceFile.initPluginEnv(this.rollup); 864 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 865 const expectedNormalizedOhmUrl: string = '@normalized:Y&&com.test.testHsp&libproduct.so&'; 866 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 867 }); 868 869 mocha.it('NormalizedOHMUrl native so others import (source code har)', function () { 870 this.rollup.build(); 871 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 872 this.rollup.share.projectConfig.pkgContextInfo = { 873 'libhar.so': { 874 'packageName': 'libhar.so', 875 'bundleName': '', 876 'moduleName': '', 877 'version': '', 878 'entryPath': '', 879 'isSO': true 880 } 881 } 882 const importerFile: string = '/testHap/har/src/main/ets/pages/Index.ets' 883 const moduleRequest = 'libhar.so'; 884 const moduleSourceFile: string = new ModuleSourceFile(); 885 ModuleSourceFile.initPluginEnv(this.rollup); 886 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 887 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; 888 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 889 }); 890 891 mocha.it('NormalizedOHMUrl native so others import (product har)', function () { 892 this.rollup.build(); 893 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 894 this.rollup.share.projectConfig.pkgContextInfo = { 895 'libhar.so': { 896 'packageName': 'libhar.so', 897 'bundleName': '', 898 'moduleName': '', 899 'version': '', 900 'entryPath': '', 901 'isSO': true 902 } 903 } 904 const importerFile: string = 905 '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; 906 const moduleRequest = 'libhar.so'; 907 const moduleSourceFile: string = new ModuleSourceFile(); 908 ModuleSourceFile.initPluginEnv(this.rollup); 909 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 910 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; 911 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 912 }); 913 914 mocha.it('NormalizedOHMUrl ohpm package others import (hap/in-app hsp)', function () { 915 this.rollup.build(); 916 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 917 this.rollup.share.projectConfig.pkgContextInfo = { 918 '@ohos/Test': { 919 'packageName': '@ohos/Test', 920 'bundleName': '', 921 'moduleName': '', 922 'version': '2.3.1', 923 'entryPath': 'index.ets', 924 'isSO': false 925 } 926 } 927 const filePath: string = 928 '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' 929 const indexFilePath: string = '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; 930 for (let file of [filePath, indexFilePath]) { 931 const moduleInfo = { 932 id: file, 933 meta: { 934 pkgName: '@ohos/Test', 935 pkgPath: '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' 936 } 937 } 938 this.rollup.moduleInfos.push(moduleInfo); 939 } 940 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 941 const importByPkgName = '@ohos/Test'; 942 const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; 943 const moduleSourceFile: string = new ModuleSourceFile(); 944 ModuleSourceFile.initPluginEnv(this.rollup); 945 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 946 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 947 importerFile); 948 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/index&2.3.1'; 949 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; 950 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 951 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 952 }); 953 954 mocha.it('NormalizedOHMUrl ohpm package others import (inter-app hsp)', function () { 955 this.rollup.build(); 956 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 957 this.rollup.share.projectConfig.pkgContextInfo = { 958 '@ohos/Test': { 959 'packageName': '@ohos/Test', 960 'bundleName': 'com.test.testHsp', 961 'moduleName': '', 962 'version': '2.3.1', 963 'entryPath': 'index.ets', 964 'isSO': false 965 } 966 } 967 const filePath: string = 968 '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' 969 const indexFilePath: string = '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; 970 for (let file of [filePath, indexFilePath]) { 971 const moduleInfo = { 972 id: file, 973 meta: { 974 pkgName: '@ohos/Test', 975 pkgPath: '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' 976 } 977 } 978 this.rollup.moduleInfos.push(moduleInfo); 979 } 980 const importerFile: string = '/testHsp/entry/src/main/ets/pages/index.ets' 981 const importByPkgName = '@ohos/Test'; 982 const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; 983 const moduleSourceFile: string = new ModuleSourceFile(); 984 ModuleSourceFile.initPluginEnv(this.rollup); 985 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 986 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 987 importerFile); 988 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&@ohos/Test/index&2.3.1'; 989 const standardImportPathNormalizedOhmUrl: string = 990 '@normalized:N&&com.test.testHsp&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; 991 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 992 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 993 }); 994 995 mocha.it('the error message of getNormalizedOhmUrlByFilepath', function () { 996 this.rollup.build(); 997 const pkgParams = { 998 pkgName: 'json5', 999 pkgPath: `${projectConfig.projectRootPath}/entry/oh_modules/json5`, 1000 isRecordName: false 1001 }; 1002 projectConfig.pkgContextInfo = { 1003 'json5': undefined 1004 }; 1005 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 1006 const importerFile: string = 'importTest.ts'; 1007 const errInfo: LogData = LogDataFactory.newInstance( 1008 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 1009 ArkTSErrorDescription, 1010 'Failed to resolve OhmUrl. ' + 1011 'Failed to get a resolved OhmUrl for "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 1012 '', 1013 ['Check whether the "json5" module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 1014 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 1015 ); 1016 const logger = CommonLogger.getInstance(this.rollup); 1017 const stub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 1018 try { 1019 getNormalizedOhmUrlByFilepath(filePath, projectConfig, logger, pkgParams, importerFile); 1020 } catch (e) { 1021 } 1022 expect(stub.calledWith(errInfo)).to.be.true; 1023 stub.restore(); 1024 }); 1025 1026 mocha.it('the error message of getNormalizedOhmUrlByFilepath without getHvigorConsoleLogger', function () { 1027 this.rollup.build(); 1028 const pkgParams = { 1029 pkgName: 'json5', 1030 pkgPath: `${projectConfig.projectRootPath}/entry/oh_modules/json5`, 1031 isRecordName: false 1032 }; 1033 projectConfig.pkgContextInfo = { 1034 'json5': undefined 1035 }; 1036 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 1037 const moduleName: string = 'entry'; 1038 const importerFile: string = 'importTest.ts'; 1039 const errInfo: LogData = LogDataFactory.newInstance( 1040 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 1041 ArkTSErrorDescription, 1042 'Failed to resolve OhmUrl. ' + 1043 'Failed to get a resolved OhmUrl for "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" imported by "importTest.ts".', 1044 '', 1045 ['Check whether the "json5" module which /testProjectRootPath/entry/oh_modules/json5/dist/index.js belongs to is correctly configured.', 1046 'Check if the corresponding file name "/testProjectRootPath/entry/oh_modules/json5/dist/index.js" is correct(including case-sensitivity).'] 1047 ); 1048 CommonLogger.destroyInstance(); 1049 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 1050 this.rollup.share.getHvigorConsoleLogger = undefined; 1051 const logger = CommonLogger.getInstance(this.rollup); 1052 const stub = sinon.stub(logger.logger, 'error'); 1053 try { 1054 getNormalizedOhmUrlByFilepath(filePath, projectConfig, logger, pkgParams, importerFile); 1055 } catch (e) { 1056 } 1057 expect(stub.calledWith(errInfo.toString())).to.be.true; 1058 CommonLogger.destroyInstance(); 1059 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 1060 stub.restore(); 1061 }); 1062 1063 mocha.it('transform mockConfigInfo', function () { 1064 this.rollup.preview(); 1065 ModuleSourceFile.mockConfigInfo = PRVIEW_MOCK_CONFIG; 1066 this.rollup.share.projectConfig.modulePath = `${projectConfig.projectRootPath}/entry`; 1067 this.rollup.share.projectConfig.mockParams = { 1068 etsSourceRootPath: 'src/main/ets', 1069 mockConfigPath: `${projectConfig.projectRootPath}/entry/src/mock/mock-config.json5` 1070 } 1071 this.rollup.share.projectConfig.entryModuleName = 'entry'; 1072 const importerFile: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/index.ets`; 1073 const moduleInfo = { 1074 id: importerFile, 1075 meta: { 1076 moduleName: 'entry', 1077 } 1078 }; 1079 this.rollup.moduleInfos.push(moduleInfo); 1080 for (let moduleRequest in PRVIEW_MOCK_CONFIG) { 1081 let mockPath = PRVIEW_MOCK_CONFIG[moduleRequest] 1082 let filePath: string; 1083 if (Object.prototype.hasOwnProperty.call(MOCK_CONFIG_FILEPATH, moduleRequest)) { 1084 filePath = MOCK_CONFIG_FILEPATH[moduleRequest]; 1085 const moduleInfo = { 1086 id: filePath, 1087 meta: { 1088 moduleName: moduleRequest === 'lib' ? 'lib' : 'entry', 1089 pkgName: moduleRequest === 'lib' ? 'lib' : 'entry', 1090 pkgPath: moduleRequest === 'lib' ? `${projectConfig.projectRootPath}/oh_modules/lib` : 1091 `${projectConfig.projectRootPath}/entry` 1092 } 1093 }; 1094 this.rollup.moduleInfos.push(moduleInfo); 1095 } 1096 const moduleSourceFile: string = new ModuleSourceFile(); 1097 ModuleSourceFile.initPluginEnv(this.rollup); 1098 ModuleSourceFile.setProcessMock(this.rollup); 1099 moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, filePath, importerFile); 1100 } 1101 const expectMockConfig: Object = { 1102 '@ohos:bluetooth': { 1103 source: '@bundle:com.example.app/entry/mock/ohos/bluetooth.mock' 1104 }, 1105 '@bundle:com.example.app/entry/ets/calc': { 1106 source: '@bundle:com.example.app/entry/mock/module/calc.mock' 1107 }, 1108 '@bundle:/testProjectRootPath/oh_modules/lib/dist/index.js': { 1109 source: '@bundle:com.example.app/entry/mock/module/bigInt.mock' 1110 }, 1111 '@app:UtTestApplication/entry/entry': { 1112 source: '@bundle:com.example.app/entry/mock/native/libentry.mock' 1113 } 1114 }; 1115 expect(ModuleSourceFile.newMockConfigInfo.toString() === expectMockConfig.toString()).to.be.true; 1116 ModuleSourceFile.cleanUpObjects(); 1117 }); 1118 1119 mocha.it('NormalizedOHMUrl transform mockConfigInfo', function () { 1120 this.rollup.preview(); 1121 this.rollup.useNormalizedOHMUrl(); 1122 this.rollup.share.projectConfig.pkgContextInfo = { 1123 'entry': { 1124 'packageName': 'entry', 1125 'bundleName': '', 1126 'moduleName': '', 1127 'version': '', 1128 'entryPath': 'index.ets', 1129 'isSO': false 1130 }, 1131 'lib': { 1132 'packageName': 'lib', 1133 'bundleName': '', 1134 'moduleName': 'lib', 1135 'version': '', 1136 'entryPath': 'index.ets', 1137 'isSO': false 1138 }, 1139 'libentry.so': { 1140 'packageName': 'libentry.so', 1141 'bundleName': '', 1142 'moduleName': '', 1143 'version': '', 1144 'entryPath': '', 1145 'isSO': true 1146 } 1147 }; 1148 ModuleSourceFile.mockConfigInfo = PRVIEW_MOCK_CONFIG; 1149 this.rollup.share.projectConfig.modulePath = `${projectConfig.projectRootPath}/entry`; 1150 this.rollup.share.projectConfig.mockParams = { 1151 etsSourceRootPath: 'src/main/ets', 1152 mockConfigPath: `${projectConfig.projectRootPath}/entry/src/mock/mock-config.json5` 1153 } 1154 this.rollup.share.projectConfig.entryModuleName = 'entry'; 1155 1156 const importerFile: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/index.ets`; 1157 const moduleInfo = { 1158 id: importerFile, 1159 meta: { 1160 moduleName: 'entry', 1161 pkgName: 'entry', 1162 pkgPath: `${projectConfig.projectRootPath}/entry` 1163 } 1164 } 1165 this.rollup.moduleInfos.push(moduleInfo); 1166 1167 for (let moduleRequest in PRVIEW_MOCK_CONFIG) { 1168 let mockPath = PRVIEW_MOCK_CONFIG[moduleRequest] 1169 let filePath: string; 1170 if (Object.prototype.hasOwnProperty.call(MOCK_CONFIG_FILEPATH, moduleRequest)) { 1171 filePath = MOCK_CONFIG_FILEPATH[moduleRequest]; 1172 const moduleInfo = { 1173 id: filePath, 1174 meta: { 1175 moduleName: moduleRequest === 'lib' ? 'lib' : 'entry', 1176 pkgName: moduleRequest === 'lib' ? 'lib' : 'entry', 1177 pkgPath: moduleRequest === 'lib' ? `${projectConfig.projectRootPath}/oh_modules/lib` : 1178 `${projectConfig.projectRootPath}/entry` 1179 } 1180 } 1181 this.rollup.moduleInfos.push(moduleInfo); 1182 } 1183 const moduleSourceFile: string = new ModuleSourceFile(); 1184 ModuleSourceFile.initPluginEnv(this.rollup); 1185 ModuleSourceFile.setProcessMock(this.rollup); 1186 moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, filePath, importerFile); 1187 } 1188 const expectMockConfig = { 1189 '@ohos:bluetooth': { 1190 source: '@normalized:N&&&entry/src/main/mock/ohos/bluetooth.mock&' 1191 }, 1192 '@normalized:N&&&entry/src/main/ets/calc&': { 1193 source: '@normalized:N&&&entry/src/main/mock/module/calc.mock&' 1194 }, 1195 '@normalized:N&lib&&lib/dist/index&': { 1196 source: '@normalized:N&&&entry/src/main/mock/module/bigInt.mock&' 1197 }, 1198 '@normalized:Y&&&libentry.so&': { 1199 source: '@normalized:N&&&entry/src/main/mock/native/libentry.mock&' 1200 } 1201 }; 1202 expect(ModuleSourceFile.newMockConfigInfo.toString() === expectMockConfig.toString()).to.be.true; 1203 ModuleSourceFile.cleanUpObjects(); 1204 }); 1205 1206 mocha.it('NormalizedOHMUrl bytecode har import', function () { 1207 this.rollup.build(); 1208 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1209 this.rollup.share.projectConfig.pkgContextInfo = { 1210 'bytecode_har': { 1211 'packageName': 'bytecode_har', 1212 'bundleName': '', 1213 'moduleName': '', 1214 'version': '1.0.0', 1215 'entryPath': 'Index.ets', 1216 'isSO': false 1217 }, 1218 'bytecode_alias_oh': { 1219 'packageName': 'bytecode_alias_oh', 1220 'bundleName': '', 1221 'moduleName': '', 1222 'version': '1.0.0', 1223 'entryPath': 'Index.ets', 1224 'isSO': false 1225 } 1226 } 1227 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 1228 ['bytecode_alias', 'bytecode_har', 'bytecode_alias_oh'] 1229 ]); 1230 this.rollup.share.projectConfig.byteCodeHarInfo = { 1231 'bytecode_alias': { 1232 'abcPath':'D:\\projectPath\\bytecode_har\\modules.abc' 1233 }, 1234 'bytecode_alias_oh': { 1235 'abcPath':'D:\\projectPath\\bytecode_alias_oh\\modules.abc' 1236 } 1237 } 1238 const filePath: string = 'bytecode_alias/src/main/ets/utils/Calc'; 1239 const indexFilePath: string = 'bytecode_alias'; 1240 1241 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 1242 const importByPkgName = 'bytecode_alias'; 1243 const standardImportPath: string = 'bytecode_alias/src/main/ets/utils/Calc'; 1244 const importByPkgNameSlashes = 'bytecode_alias///'; 1245 const importByPkgNameSlashesOh = 'bytecode_alias_oh///'; 1246 const importModuleRequets = 'bytecode_alias_oh///\\\/' 1247 const moduleSourceFile: string = new ModuleSourceFile(); 1248 ModuleSourceFile.initPluginEnv(this.rollup); 1249 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 1250 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 1251 importerFile); 1252 const importByPkgNameOhmUrlSlashes = moduleSourceFile.getOhmUrl(this.rollup, importByPkgNameSlashes, indexFilePath, importerFile); 1253 const importByPkgNameOhmUrlSlashesOh = moduleSourceFile.getOhmUrl(this.rollup, importByPkgNameSlashesOh, indexFilePath, importerFile); 1254 const importModuleRequetsOhmUrlSlashesOh = moduleSourceFile.getOhmUrl(this.rollup, importModuleRequets, indexFilePath, importerFile); 1255 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&bytecode_har/Index&1.0.0'; 1256 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&bytecode_har/src/main/ets/utils/Calc&1.0.0'; 1257 const importByPkgNameNormalizedOhmUrlSlashes: string = '@normalized:N&&&bytecode_har/Index&1.0.0'; 1258 const importByPkgNameNormalizedOhmUrlSlashesOh: string = '@normalized:N&&&bytecode_alias_oh/Index&1.0.0'; 1259 const importModuleRequetsNormalizedOhmUrlSlashesOh: string = '@normalized:N&&&bytecode_alias_oh/Index&1.0.0'; 1260 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 1261 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 1262 expect(importByPkgNameOhmUrlSlashes == importByPkgNameNormalizedOhmUrlSlashes).to.be.true; 1263 expect(importByPkgNameOhmUrlSlashesOh == importByPkgNameNormalizedOhmUrlSlashesOh).to.be.true; 1264 expect(importModuleRequetsOhmUrlSlashesOh == importModuleRequetsNormalizedOhmUrlSlashesOh).to.be.true; 1265 }); 1266 1267 mocha.it('useNormalizedOHMUrl app builtins error message', function () { 1268 this.rollup.build(); 1269 this.rollup.useNormalizedOHMUrl(); 1270 this.rollup.share.projectConfig.pkgContextInfo = {}; 1271 const errInfo: LogData = LogDataFactory.newInstance( 1272 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GET_PKG_CONTENT_INFO, 1273 ArkTSInternalErrorDescription, 1274 "Can not get pkgContextInfo of package 'libapplication.so' which being imported by " + 1275 "'/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'" 1276 ); 1277 const logger = CommonLogger.getInstance(this.rollup); 1278 const loggerStub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 1279 const importerFile: string = 1280 '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; 1281 const appSoModuleRequest: string = 'libapplication.so'; 1282 try { 1283 const appSoParams: OhmUrlParams = { 1284 moduleRequest: appSoModuleRequest, 1285 moduleId: '', 1286 config: this.rollup.share.projectConfig, 1287 logger: logger, 1288 importerFile: importerFile, 1289 }; 1290 getOhmUrlBySystemApiOrLibRequest(appSoParams, true); 1291 } catch (e) { 1292 } 1293 expect(loggerStub.calledWith(errInfo)).to.be.true; 1294 loggerStub.restore(); 1295 }); 1296 1297 mocha.it('useNormalizedOHMUrl app builtins error message without getHvigorConsoleLogger', function () { 1298 this.rollup.build(); 1299 this.rollup.useNormalizedOHMUrl(); 1300 this.rollup.share.projectConfig.pkgContextInfo = {}; 1301 const errInfo: LogData = LogDataFactory.newInstance( 1302 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GET_PKG_CONTENT_INFO, 1303 ArkTSInternalErrorDescription, 1304 "Can not get pkgContextInfo of package 'libapplication.so' which being imported by " + 1305 "'/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'" 1306 ); 1307 CommonLogger.destroyInstance(); 1308 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 1309 this.rollup.share.getHvigorConsoleLogger = undefined; 1310 const logger = CommonLogger.getInstance(this.rollup); 1311 const loggerStub = sinon.stub(logger.logger, 'error'); 1312 const importerFile: string = 1313 '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; 1314 const appSoModuleRequest: string = 'libapplication.so'; 1315 try { 1316 const appSoParams: OhmUrlParams = { 1317 moduleRequest: appSoModuleRequest, 1318 moduleId: '', 1319 config: this.rollup.share.projectConfig, 1320 logger: logger, 1321 importerFile: importerFile, 1322 }; 1323 getOhmUrlBySystemApiOrLibRequest(appSoParams, true); 1324 } catch (e) { 1325 } 1326 expect(loggerStub.calledWith(errInfo.toString())).to.be.true; 1327 CommonLogger.destroyInstance(); 1328 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 1329 loggerStub.restore(); 1330 }); 1331 1332 mocha.it('the error message of getNormalizedOhmUrlByModuleRequest', function () { 1333 const moduleInfoByModuleRequest = { 1334 normalizedPath: "bytecode_module/Index", 1335 packageName: "bytecode_module" 1336 }; 1337 this.rollup.build(); 1338 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1339 this.rollup.share.projectConfig.pkgContextInfo = { 1340 'bytecode_module1': { 1341 'packageName': 'bytecode_module1', 1342 'bundleName': '', 1343 'moduleName': '', 1344 'version': '1.0.0', 1345 'entryPath': 'Index.ets', 1346 'isSO': false 1347 } 1348 } 1349 const errInfo: LogData = LogDataFactory.newInstance( 1350 ErrorCode.ETS2BUNDLE_INTERNAL_PACKAGE_NOT_FOUND_IN_CONTEXT_INFO, 1351 ArkTSInternalErrorDescription, 1352 "Failed to find package 'bytecode_module'. Failed to obtain package 'bytecode_module' " + 1353 "from the package context information." 1354 ); 1355 const logger = CommonLogger.getInstance(this.rollup); 1356 const loggerStub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 1357 try { 1358 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_module']; 1359 getNormalizedOhmUrlByModuleRequest(moduleInfoByModuleRequest, this.rollup.share.projectConfig, logger); 1360 } catch (e) { 1361 } 1362 expect(loggerStub.getCall(0).calledWithMatch(errInfo)).to.be.true; 1363 loggerStub.restore(); 1364 }); 1365 1366 mocha.it('the error message of processPackageDir when useNormalizedOHMUrl is false', function () { 1367 this.rollup.build(); 1368 this.rollup.share.projectConfig.useNormalizedOHMUrl = false; 1369 this.rollup.share.projectConfig.byteCodeHarInfo = {}; 1370 const indexFilePath: string = 'Hsp/////'; 1371 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets'; 1372 const importByPkgName = 'Hsp/////'; 1373 for (let file of [indexFilePath]) { 1374 const moduleInfo = { 1375 id: file, 1376 meta: { 1377 hostDependencyName: 'Hsp/////', 1378 hostModuleName: 'entry' 1379 } 1380 } 1381 this.rollup.moduleInfos.push(moduleInfo); 1382 } 1383 1384 const errInfo: LogData = LogDataFactory.newInstance( 1385 ErrorCode.ETS2BUNDLE_EXTERNAL_FAILED_TO_RESOLVE_OHM_URL, 1386 ArkTSErrorDescription, 1387 'Failed to resolve OhmUrl. ' + 1388 `Failed to get a resolved OhmUrl for "${indexFilePath}" imported by "${importerFile}".`, 1389 '', 1390 [`Check whether the module which ${indexFilePath} belongs to is correctly configured.`, 1391 `Check if the corresponding file name "${indexFilePath}" is correct(including case-sensitivity).`] 1392 ); 1393 const logger = CommonLogger.getInstance(this.rollup); 1394 const loggerStub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 1395 1396 const moduleSourceFile: string = new ModuleSourceFile(); 1397 ModuleSourceFile.initPluginEnv(this.rollup); 1398 moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 1399 1400 expect(loggerStub.getCall(0).calledWithMatch(errInfo)).to.be.true; 1401 loggerStub.restore(); 1402 }); 1403 1404 mocha.it('the error message of getNormalizedOhmUrlByAliasName', function () { 1405 this.rollup.build(); 1406 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1407 this.rollup.share.projectConfig.pkgContextInfo = { 1408 'bytecode_har': { 1409 'packageName': 'bytecode_har', 1410 'bundleName': '', 1411 'moduleName': '', 1412 'version': '1.0.0', 1413 'entryPath': 'Index.ets', 1414 'isSO': false 1415 } 1416 } 1417 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 1418 ['bytecode_alias', 'bytecode_har'] 1419 ]); 1420 this.rollup.share.projectConfig.byteCodeHarInfo = { 1421 'bytecode_alias': { 1422 'abcPath': 'D:\\projectPath\\bytecode_har\\modules.abc' 1423 } 1424 } 1425 const errInfo: LogData = LogDataFactory.newInstance( 1426 ErrorCode.ETS2BUNDLE_INTERNAL_PACKAGE_ENTRY_FILE_NOT_FOUND, 1427 ArkTSInternalErrorDescription, 1428 "Failed to find entry file of 'bytecode_har'. Failed to obtain the entry file " + 1429 "information of 'bytecode_har' from the package context information." 1430 ); 1431 const aliasPkgName = 'bytecode_alias'; 1432 const pkgName = 'bytecode_har'; 1433 const logger = CommonLogger.getInstance(this.rollup); 1434 const loggerStub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printError'); 1435 1436 try { 1437 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']['entryPath']; 1438 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1439 } catch (e) { 1440 } 1441 expect(loggerStub.getCall(0).calledWith(errInfo)).to.be.true; 1442 1443 try { 1444 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']; 1445 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1446 } catch (e) { 1447 } 1448 const errInfo1: LogData = LogDataFactory.newInstance( 1449 ErrorCode.ETS2BUNDLE_INTERNAL_PACKAGE_NOT_FOUND_IN_CONTEXT_INFO, 1450 ArkTSInternalErrorDescription, 1451 "Failed to find package 'bytecode_har'. Failed to obtain package 'bytecode_har' " + 1452 "from the package context information." 1453 ); 1454 expect(loggerStub.getCall(1).calledWithMatch(errInfo1)).to.be.true; 1455 1456 loggerStub.restore(); 1457 }); 1458 1459 mocha.it('the error message of getNormalizedOhmUrlByAliasName without getHvigorConsoleLogger', function () { 1460 this.rollup.build(); 1461 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1462 this.rollup.share.projectConfig.pkgContextInfo = { 1463 'bytecode_har': { 1464 'packageName': 'bytecode_har', 1465 'bundleName': '', 1466 'moduleName': '', 1467 'version': '1.0.0', 1468 'entryPath': 'Index.ets', 1469 'isSO': false 1470 } 1471 } 1472 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 1473 ['bytecode_alias', 'bytecode_har'] 1474 ]); 1475 this.rollup.share.projectConfig.byteCodeHarInfo = { 1476 'bytecode_alias': { 1477 'abcPath': 'D:\\projectPath\\bytecode_har\\modules.abc' 1478 } 1479 } 1480 const errInfo: LogData = LogDataFactory.newInstance( 1481 ErrorCode.ETS2BUNDLE_INTERNAL_PACKAGE_ENTRY_FILE_NOT_FOUND, 1482 ArkTSInternalErrorDescription, 1483 "Failed to find entry file of 'bytecode_har'. Failed to obtain the entry file " + 1484 "information of 'bytecode_har' from the package context information." 1485 ); 1486 CommonLogger.destroyInstance(); 1487 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 1488 this.rollup.share.getHvigorConsoleLogger = undefined; 1489 const aliasPkgName = 'bytecode_alias'; 1490 const pkgName = 'bytecode_har'; 1491 const logger = CommonLogger.getInstance(this.rollup); 1492 const loggerStub = sinon.stub(logger.logger, 'error'); 1493 1494 try { 1495 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']['entryPath']; 1496 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1497 } catch (e) { 1498 } 1499 expect(loggerStub.getCall(0).calledWith(errInfo.toString())).to.be.true; 1500 1501 try { 1502 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']; 1503 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1504 } catch (e) { 1505 } 1506 const errInfo1: LogData = LogDataFactory.newInstance( 1507 ErrorCode.ETS2BUNDLE_INTERNAL_PACKAGE_NOT_FOUND_IN_CONTEXT_INFO, 1508 ArkTSInternalErrorDescription, 1509 "Failed to find package 'bytecode_har'. Failed to obtain package 'bytecode_har' " + 1510 "from the package context information." 1511 ); 1512 expect(loggerStub.getCall(1).calledWithMatch(errInfo1.toString())).to.be.true; 1513 CommonLogger.destroyInstance(); 1514 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 1515 loggerStub.restore(); 1516 }); 1517 1518 mocha.it('Collect. so libraries referenced in the project and generate JSON files', function () { 1519 this.rollup.build(); 1520 this.rollup.share.projectConfig.useNormalizedOHMUrl = false; 1521 this.rollup.share.projectConfig.isPreloadSoEnabled = true; 1522 this.rollup.share.projectConfig.preloadSoFilePath = 'build/default/intermediates/preload/default/preload.json'; 1523 this.rollup.share.projectConfig.cachePath = 'build/default/intermediates/preload/default/cache'; 1524 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 1525 const outFilePath: string = 'build/default/intermediates/preload/default/preload.json'; 1526 const moduleRequest: string = '@ohos.router'; 1527 const moduleInfo = { 1528 id: filePath, 1529 meta: { 1530 needPreloadSo: true 1531 }, 1532 importedIdMaps: [filePath] 1533 } 1534 this.rollup.moduleInfos.push(moduleInfo); 1535 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 1536 const moduleSourceFile: string = new ModuleSourceFile(); 1537 moduleSourceFile.moduleId = filePath; 1538 ModuleSourceFile.initPluginEnv(this.rollup); 1539 PreloadFileModules.initialize(this.rollup); 1540 ModuleSourceFile.PreloadEntryNames = []; 1541 ModuleSourceFile.preloadEntries = []; 1542 1543 const resultOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, filePath, importerFile); 1544 1545 PreloadFileModules.finalizeWritePreloadSoList(); 1546 1547 const fileContent = fs.readFileSync(outFilePath, 'utf8'); 1548 const expectedJson = { 1549 "systemPreloadHintStartupTasks": [ 1550 { 1551 "name": "@ohos.router", 1552 "ohmurl": "@ohos:router", 1553 "srcEntry": "@ohos.router" 1554 } 1555 ] 1556 }; 1557 1558 // Remove trailing ',' if exists 1559 const parsedFileContent = JSON.parse(fileContent.trim().replace(/,$/, '')); 1560 expect(resultOhmUrl === '@ohos:router').to.be.true; 1561 expect(fs.existsSync(outFilePath)).to.be.true; 1562 expect(parsedFileContent).to.deep.equal(expectedJson); 1563 }); 1564 1565 mocha.it('Collect. so libraries referenced in the project and generate JSON files,remove one file', function () { 1566 this.rollup.build(); 1567 this.rollup.share.projectConfig.useNormalizedOHMUrl = false; 1568 this.rollup.share.projectConfig.isPreloadSoEnabled = true; 1569 this.rollup.share.projectConfig.preloadSoFilePath = 'build/default/intermediates/preload/default/preload.json'; 1570 this.rollup.share.projectConfig.cachePath = 'build/default/intermediates/preload/default/cache'; 1571 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 1572 const indexFilePath: string = '/testHsp/hsp/src/main/ets/utils/Index.ets'; 1573 const outFilePath: string = 'build/default/intermediates/preload/default/preload.json'; 1574 1575 for (let file of [filePath, indexFilePath]) { 1576 const moduleInfo = { 1577 id: file, 1578 meta: { 1579 needPreloadSo: true 1580 } 1581 } 1582 this.rollup.moduleInfos.push(moduleInfo); 1583 } 1584 PreloadFileModules.initialize(this.rollup); 1585 PreloadFileModules.preloadEntriesBack = []; 1586 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 1587 const moduleSourceFile: string = new ModuleSourceFile(); 1588 moduleSourceFile.moduleId = filePath; 1589 moduleSourceFile.getOhmUrl(this.rollup, '@ohos.router', filePath, importerFile); 1590 PreloadFileModules.finalizeWritePreloadSoList(); 1591 1592 PreloadFileModules.preloadEntriesBack = []; 1593 const moduleSourceFileIndex: string = new ModuleSourceFile(); 1594 moduleSourceFileIndex.moduleId = indexFilePath; 1595 moduleSourceFileIndex.getOhmUrl(this.rollup, '@kit.LocalizationKit', indexFilePath, importerFile); 1596 PreloadFileModules.finalizeWritePreloadSoList(); 1597 1598 const fileContent = fs.readFileSync(outFilePath, 'utf8'); 1599 const expectedJson = { 1600 "systemPreloadHintStartupTasks": [ 1601 { 1602 "name": "@kit.LocalizationKit", 1603 "ohmurl": "@ohos:LocalizationKit", 1604 "srcEntry": "@kit.LocalizationKit" 1605 } 1606 ] 1607 }; 1608 // Remove trailing ',' if exists 1609 const parsedFileContent = JSON.parse(fileContent.trim().replace(/,$/, '')); 1610 expect(parsedFileContent).to.deep.equal(expectedJson); 1611 }); 1612});