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 16import { expect } from 'chai'; 17import mocha from 'mocha'; 18import sinon from 'sinon'; 19 20import { 21 getOhmUrlByFilepath, 22 getOhmUrlByExternalPackage, 23 getOhmUrlBySystemApiOrLibRequest, 24 getNormalizedOhmUrlByFilepath, 25 getNormalizedOhmUrlByAliasName 26} from '../../../../lib/ark_utils'; 27import { PACKAGES } from '../../../../lib/pre_define'; 28import projectConfig from '../../utils/processProjectConfig'; 29import { projectConfig as mainProjectConfig } from '../../../../main'; 30import RollUpPluginMock from '../../mock/rollup_mock/rollup_plugin_mock'; 31import { GEN_ABC_PLUGIN_NAME } from '../../../../lib/fast_build/ark_compiler/common/ark_define'; 32import { ModuleSourceFile } from '../../../../lib/fast_build/ark_compiler/module/module_source_file'; 33const PRVIEW_MOCK_CONFIG : Object = { 34 // system api mock 35 "@ohos.bluetooth": { 36 "source": "src/main/mock/ohos/bluetooth.mock.ts" 37 }, 38 // local function mock 39 "./src/main/ets/calc": { 40 "source": "src/main/mock/module/calc.mock.ts" 41 }, 42 // ohpm dependency mock 43 "lib": { 44 "source": "src/main/mock/module/bigInt.mock.ts" 45 }, 46 // native mock 47 "libentry.so": { 48 "source": "src/main/mock/native/libentry.mock.ts" 49 } 50} 51 52const MOCK_CONFIG_FILEPATH = { 53 'lib': `${projectConfig.projectRootPath}/oh_modules/lib/dist/index.js`, 54 './src/main/ets/calc': `${projectConfig.projectRootPath}/entry/src/main/ets/calc.ets`, 55} 56 57mocha.describe('generate ohmUrl', function () { 58 mocha.before(function () { 59 this.rollup = new RollUpPluginMock(); 60 }); 61 62 mocha.after(() => { 63 delete this.rollup; 64 }); 65 66 mocha.it('nested src main ets|js in filePath', function () { 67 const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` 68 + `subfeature/src/main/ets/pages/test.ts`; 69 const moduleName: string = 'entry'; 70 const moduleNamespace: string = 'library'; 71 let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 72 let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); 73 let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 74 let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 75 expect(ohmUrl_1 == expected_1).to.be.true; 76 expect(ohmUrl_2 == expected_2).to.be.true; 77 }); 78 79 mocha.it('nested src ohosTest ets|js in filePath', function () { 80 const filePath: string = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` 81 + `subfeature/src/main/ets/pages/test.ts`; 82 const moduleName: string = 'entry'; 83 const moduleNamespace: string = 'library'; 84 let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 85 let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); 86 let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 87 let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; 88 expect(ohmUrl_1 == expected_1).to.be.true; 89 expect(ohmUrl_2 == expected_2).to.be.true; 90 }); 91 92 mocha.it('system builtins & app builtins', function () { 93 mainProjectConfig.bundleName = 'UtTestApplication'; 94 mainProjectConfig.moduleName = 'entry'; 95 const systemModuleRequest: string = '@system.app'; 96 const ohosModuleRequest: string = '@ohos.hilog'; 97 const appSoModuleRequest: string = 'libapplication.so'; 98 const systemOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(systemModuleRequest); 99 const ohosOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(ohosModuleRequest); 100 const appOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(appSoModuleRequest); 101 const expectedSystemOhmUrl: string = '@native:system.app'; 102 const expectedOhosOhmUrl: string = '@ohos:hilog'; 103 const expectedappOhmUrl: string = '@app:UtTestApplication/entry/application'; 104 expect(systemOhmUrl == expectedSystemOhmUrl).to.be.true; 105 expect(ohosOhmUrl == expectedOhosOhmUrl).to.be.true; 106 expect(appOhmUrl == expectedappOhmUrl).to.be.true; 107 }); 108 109 mocha.it('shared library', function () { 110 const sharedLibraryPackageName: string = "@ohos/sharedLibrary"; 111 const sharedLibraryPackageNameSlashes: string = "@ohos/library///"; 112 const sharedLibraryPage: string = "@ohos/sharedLibrary/src/main/ets/pages/page1"; 113 const errorSharedLibrary: string = "@ohos/staticLibrary"; 114 const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPackageName, projectConfig); 115 const sharedLibraryPackageNameSlashesOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPackageNameSlashes, projectConfig, ModuleSourceFile.logger, true); 116 const sharedLibraryPageOhmUrl: string = getOhmUrlByExternalPackage(sharedLibraryPage, projectConfig); 117 const errorSharedLibraryOhmUrl = getOhmUrlByExternalPackage(errorSharedLibrary, projectConfig); 118 const expectedSharedLibraryOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/index"; 119 const expectedSharedLibrarySlashesOhmUrl: string = "@normalized:N&&&@ohos/library/Index&1.0.0"; 120 const expectedSharedLibraryPageOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/pages/page1"; 121 const expectedErrorSharedLibraryOhmUrl = undefined; 122 expect(sharedLibraryPackageNameOhmUrl == expectedSharedLibraryOhmUrl).to.be.true; 123 expect(sharedLibraryPackageNameSlashesOhmUrl == expectedSharedLibrarySlashesOhmUrl).to.be.true; 124 expect(sharedLibraryPageOhmUrl == expectedSharedLibraryPageOhmUrl).to.be.true; 125 expect(errorSharedLibraryOhmUrl == expectedErrorSharedLibraryOhmUrl).to.be.true; 126 }); 127 128 mocha.it('project module', function () { 129 const filePath: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; 130 const harFilePath = `${projectConfig.projectRootPath}/library/src/main/ets/pages/test.ts`; 131 const moduleName: string = 'entry'; 132 const moduleNamespace: string = 'library'; 133 const ohmUrl = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); 134 const harOhmUrl = getOhmUrlByFilepath(harFilePath, projectConfig, undefined, moduleNamespace); 135 const expected = 'UtTestApplication/entry/ets/pages/test'; 136 const harOhmUrlExpected = 'UtTestApplication/entry@library/ets/pages/test'; 137 expect(ohmUrl == expected).to.be.true; 138 expect(harOhmUrl == harOhmUrlExpected).to.be.true; 139 }); 140 141 mocha.it('thirdParty module', function () { 142 const moduleLevelPkgPath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 143 const projectLevelPkgPath = `${projectConfig.projectRootPath}/oh_modules/json5/dist/index.js`; 144 const moduleName: string = 'entry'; 145 const moduleLevelPkgOhmUrl = getOhmUrlByFilepath(moduleLevelPkgPath, projectConfig, undefined, undefined); 146 const projectLevelPkgOhmUrl = getOhmUrlByFilepath(projectLevelPkgPath, projectConfig, undefined, undefined); 147 const moduleLevelPkgOhmUrlExpected = `${PACKAGES}@${moduleName}/json5/dist/index`; 148 const projectLevelPkgOhmUrlExpected = `${PACKAGES}/json5/dist/index`; 149 expect(moduleLevelPkgOhmUrl == moduleLevelPkgOhmUrlExpected).to.be.true; 150 expect(projectLevelPkgOhmUrl == projectLevelPkgOhmUrlExpected).to.be.true; 151 }); 152 153 mocha.it('static library entry', function () { 154 const staticLibraryEntry = `${projectConfig.projectRootPath}/library/index.ets`; 155 const moduleNamespace: string = 'library'; 156 const staticLibraryEntryOhmUrl = 157 getOhmUrlByFilepath(staticLibraryEntry, projectConfig, undefined, moduleNamespace); 158 const staticLibraryEntryOhmUrlExpected = 'UtTestApplication/entry@library/index'; 159 expect(staticLibraryEntryOhmUrl == staticLibraryEntryOhmUrlExpected).to.be.true; 160 }); 161 162 mocha.it('ohosTest module', function () { 163 const ohosTestfilePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/pages/test.ts`; 164 const moduleName: string = 'entry'; 165 const ohmUrl = getOhmUrlByFilepath(ohosTestfilePath, projectConfig, undefined, moduleName); 166 const expected = 'UtTestApplication/entry/ets/pages/test'; 167 expect(ohmUrl == expected).to.be.true; 168 }); 169 170 mocha.it('the error message of processPackageDir', function () { 171 this.rollup.build(); 172 projectConfig.modulePathMap = {}; 173 const red: string = '\u001b[31m'; 174 const reset: string = '\u001b[39m'; 175 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 176 const moduleName: string = 'entry'; 177 const importerFile: string = 'importTest.ts'; 178 const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) 179 const loggerStub = sinon.stub(logger, 'error'); 180 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 181 expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + 182 `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + 183 `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + 184 '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; 185 loggerStub.restore(); 186 }); 187 188 mocha.it('the error message of processPackageDir(packageDir is invalid value)', function () { 189 this.rollup.build(); 190 projectConfig.packageDir = undefined; 191 projectConfig.modulePathMap = {}; 192 const red: string = '\u001b[31m'; 193 const reset: string = '\u001b[39m'; 194 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 195 const moduleName: string = 'entry'; 196 const importerFile: string = 'importTest.ts'; 197 const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) 198 const loggerStub = sinon.stub(logger, 'error'); 199 getOhmUrlByFilepath(filePath, projectConfig, logger, moduleName, importerFile); 200 expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + 201 `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + 202 `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + 203 '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; 204 loggerStub.restore(); 205 }); 206 207 mocha.it('NormalizedOHMUrl inter-app hsp self import', function () { 208 this.rollup.build(); 209 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 210 this.rollup.share.projectConfig.pkgContextInfo = { 211 'pkghsp': { 212 'packageName': 'pkghsp', 213 'bundleName': 'com.test.testHsp', 214 'moduleName': '', 215 'version': '', 216 'entryPath': 'Index.ets', 217 'isSO': false 218 } 219 } 220 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 221 const moduleInfo = { 222 id: filePath, 223 meta: { 224 pkgName: 'pkghsp', 225 pkgPath: '/testHsp/hsp' 226 } 227 } 228 this.rollup.moduleInfos.push(moduleInfo); 229 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 230 const relativePath: string = '../utils/Calc'; 231 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 232 const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; 233 const moduleSourceFile: string = new ModuleSourceFile(); 234 ModuleSourceFile.initPluginEnv(this.rollup); 235 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 236 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 237 importerFile); 238 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 239 importerFile); 240 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; 241 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 242 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 243 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 244 }); 245 246 mocha.it('NormalizedOHMUrl inter-app hsp others import', function () { 247 this.rollup.build(); 248 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 249 this.rollup.share.projectConfig.pkgContextInfo = { 250 'pkghsp': { 251 'packageName': 'pkghsp', 252 'bundleName': 'com.test.testHsp', 253 'moduleName': 'hsp', 254 'version': '', 255 'entryPath': 'Index.ets', 256 'isSO': false 257 } 258 } 259 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 260 ['pkghsp_alias', 'pkghsp'] 261 ]); 262 this.rollup.share.projectConfig.harNameOhmMap = { 263 'pkghsp_alias': '@bundle:com.test.testHsp/src/main/ets/utils/Calc' 264 } 265 const filePath: string = 'pkghsp/src/main/ets/utils/Calc'; 266 const indexFilePath: string = 'pkghsp_alias'; 267 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 268 const importByPkgName = 'pkghsp_alias'; 269 const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 270 const moduleSourceFile: string = new ModuleSourceFile(); 271 ModuleSourceFile.initPluginEnv(this.rollup); 272 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 273 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 274 importerFile); 275 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&com.test.testHsp&pkghsp/Index&'; 276 const standardImportPathNormalizedOhmUrl: string = 277 '@normalized:N&hsp&com.test.testHsp&pkghsp/src/main/ets/utils/Calc&'; 278 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 279 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 280 }); 281 282 mocha.it('NormalizedOHMUrl in-app hsp self import', function () { 283 this.rollup.build(); 284 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 285 this.rollup.share.projectConfig.pkgContextInfo = { 286 'pkghsp': { 287 'packageName': 'pkghsp', 288 'bundleName': '', 289 'moduleName': '', 290 'version': '', 291 'entryPath': 'Index.ets', 292 'isSO': false 293 } 294 } 295 const filePath: string = '/testHsp/hsp/src/main/ets/utils/Calc.ets'; 296 const moduleInfo = { 297 id: filePath, 298 meta: { 299 pkgName: 'pkghsp', 300 pkgPath: '/testHsp/hsp' 301 } 302 } 303 this.rollup.moduleInfos.push(moduleInfo); 304 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 305 const relativePath: string = '../utils/Calc'; 306 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 307 const standardImportPath: string = 'pkghsp/src/main/ets/utils/Calc'; 308 const moduleSourceFile: string = new ModuleSourceFile(); 309 ModuleSourceFile.initPluginEnv(this.rollup); 310 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 311 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 312 importerFile); 313 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 314 importerFile); 315 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghsp/src/main/ets/utils/Calc&'; 316 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 317 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 318 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 319 }); 320 321 mocha.it('NormalizedOHMUrl in-app hsp others import', function () { 322 this.rollup.build(); 323 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 324 this.rollup.share.projectConfig.pkgContextInfo = { 325 'pkghsp': { 326 'packageName': 'pkghsp', 327 'bundleName': '', 328 'moduleName': 'hsp', 329 'version': '', 330 'entryPath': 'Index.ets', 331 'isSO': false 332 } 333 } 334 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 335 ['pkghsp_alias', 'pkghsp'] 336 ]); 337 this.rollup.share.projectConfig.harNameOhmMap = { 338 'pkghsp_alias': '@bundle:com.test.testHap/src/main/ets/utils/Calc' 339 } 340 const filePath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 341 const indexFilePath: string = 'pkghsp_alias'; 342 343 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 344 const importByPkgName = 'pkghsp_alias'; 345 const standardImportPath: string = 'pkghsp_alias/src/main/ets/utils/Calc'; 346 const moduleSourceFile: string = new ModuleSourceFile(); 347 ModuleSourceFile.initPluginEnv(this.rollup); 348 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 349 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 350 importerFile); 351 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/Index&'; 352 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&hsp&&pkghsp/src/main/ets/utils/Calc&'; 353 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 354 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 355 }); 356 357 mocha.it('NormalizedOHMUrl hap self import', function () { 358 this.rollup.build(); 359 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 360 this.rollup.share.projectConfig.pkgContextInfo = { 361 'entry': { 362 'packageName': 'entry', 363 'bundleName': '', 364 'moduleName': '', 365 'version': '', 366 'entryPath': '', 367 'isSO': false 368 } 369 } 370 const filePath: string = '/testHap/entry/src/main/ets/utils/Calc.ets'; 371 const moduleInfo = { 372 id: filePath, 373 meta: { 374 pkgName: 'entry', 375 pkgPath: '/testHap/entry' 376 } 377 } 378 this.rollup.moduleInfos.push(moduleInfo); 379 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 380 const relativePath: string = '../utils/Calc'; 381 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 382 const standardImportPath: string = 'entry/src/main/ets/utils/Calc'; 383 const moduleSourceFile: string = new ModuleSourceFile(); 384 ModuleSourceFile.initPluginEnv(this.rollup); 385 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 386 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 387 importerFile); 388 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 389 importerFile); 390 const expectedNormalizedOhmUrl: string = '@normalized:N&&&entry/src/main/ets/utils/Calc&'; 391 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 392 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 393 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 394 }); 395 396 mocha.it('NormalizedOHMUrl source code har self import (hap/in-app hsp)', function () { 397 this.rollup.build(); 398 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 399 this.rollup.share.projectConfig.pkgContextInfo = { 400 'pkghar': { 401 'packageName': 'pkghar', 402 'bundleName': '', 403 'moduleName': '', 404 'version': '1.0.1', 405 'entryPath': 'Index.ets', 406 'isSO': false 407 } 408 } 409 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 410 const moduleInfo = { 411 id: filePath, 412 meta: { 413 pkgName: 'pkghar', 414 pkgPath: '/testHar/har' 415 } 416 } 417 this.rollup.moduleInfos.push(moduleInfo); 418 const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' 419 const relativePath: string = '../utils/Calc'; 420 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 421 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 422 const moduleSourceFile: string = new ModuleSourceFile(); 423 ModuleSourceFile.initPluginEnv(this.rollup); 424 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 425 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 426 importerFile); 427 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 428 importerFile); 429 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 430 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 431 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 432 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 433 }); 434 435 mocha.it('NormalizedOHMUrl source code har others import (hap/in-app hsp)', function () { 436 this.rollup.build(); 437 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 438 this.rollup.share.projectConfig.pkgContextInfo = { 439 'pkghar': { 440 'packageName': 'pkghar', 441 'bundleName': '', 442 'moduleName': '', 443 'version': '1.0.1', 444 'entryPath': 'Index.ets', 445 'isSO': false 446 } 447 } 448 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 449 const indexFilePath: string = '/testHar/har/Index.ets'; 450 for (let file of [filePath, indexFilePath]) { 451 const moduleInfo = { 452 id: file, 453 meta: { 454 pkgName: 'pkghar', 455 pkgPath: '/testHar/har' 456 } 457 } 458 this.rollup.moduleInfos.push(moduleInfo); 459 } 460 const importerFile: string = '/testHar/entry/src/main/ets/pages/Index.ets' 461 const importByPkgName = 'pkghar'; 462 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 463 const moduleSourceFile: string = new ModuleSourceFile(); 464 ModuleSourceFile.initPluginEnv(this.rollup); 465 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 466 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 467 importerFile); 468 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; 469 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 470 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 471 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 472 }); 473 474 mocha.it('NormalizedOHMUrl source code har self import (inter-app hsp)', function () { 475 this.rollup.build(); 476 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 477 this.rollup.share.projectConfig.pkgContextInfo = { 478 'pkghar': { 479 'packageName': 'pkghar', 480 'bundleName': 'com.test.testHsp', 481 'moduleName': '', 482 'version': '1.0.1', 483 'entryPath': 'Index.ets', 484 'isSO': false 485 } 486 } 487 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 488 const moduleInfo = { 489 id: filePath, 490 meta: { 491 pkgName: 'pkghar', 492 pkgPath: '/testHsp/har' 493 } 494 } 495 this.rollup.moduleInfos.push(moduleInfo); 496 const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' 497 const relativePath: string = '../utils/Calc'; 498 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 499 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 500 const moduleSourceFile: string = new ModuleSourceFile(); 501 ModuleSourceFile.initPluginEnv(this.rollup); 502 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 503 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 504 importerFile); 505 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 506 importerFile); 507 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 508 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 509 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 510 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 511 }); 512 513 mocha.it('NormalizedOHMUrl source code har others import (inter-app hsp)', function () { 514 this.rollup.build(); 515 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 516 this.rollup.share.projectConfig.pkgContextInfo = { 517 'pkghar': { 518 'packageName': 'pkghar', 519 'bundleName': 'com.test.testHsp', 520 'moduleName': '', 521 'version': '1.0.1', 522 'entryPath': 'Index.ets', 523 'isSO': false 524 } 525 } 526 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 527 const indexFilePath: string = '/testHsp/har/Index.ets'; 528 for (let file of [filePath, indexFilePath]) { 529 const moduleInfo = { 530 id: file, 531 meta: { 532 pkgName: 'pkghar', 533 pkgPath: '/testHsp/har' 534 } 535 } 536 this.rollup.moduleInfos.push(moduleInfo); 537 } 538 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 539 const importByPkgName = 'pkghar'; 540 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 541 const moduleSourceFile: string = new ModuleSourceFile(); 542 ModuleSourceFile.initPluginEnv(this.rollup); 543 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 544 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 545 importerFile); 546 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; 547 const standardImportPathNormalizedOhmUrl: string = 548 '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 549 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 550 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 551 }); 552 553 mocha.it('NormalizedOHMUrl product har self import (hap/in-app hsp)', function () { 554 this.rollup.build(); 555 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 556 this.rollup.share.projectConfig.pkgContextInfo = { 557 'pkghar': { 558 'packageName': 'pkghar', 559 'bundleName': '', 560 'moduleName': '', 561 'version': '1.0.1', 562 'entryPath': 'Index.ets', 563 'isSO': false 564 } 565 } 566 const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets'; 567 const moduleInfo = { 568 id: filePath, 569 meta: { 570 pkgName: 'pkghar', 571 pkgPath: '/testHar/har' 572 } 573 } 574 this.rollup.moduleInfos.push(moduleInfo); 575 const importerFile: string = '/testHar/har/src/main/ets/pages/Index.ets' 576 const relativePath: string = '../utils/Calc'; 577 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 578 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 579 const moduleSourceFile: string = new ModuleSourceFile(); 580 ModuleSourceFile.initPluginEnv(this.rollup); 581 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 582 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 583 importerFile); 584 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 585 importerFile); 586 const expectedNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 587 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 588 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 589 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 590 }); 591 592 mocha.it('NormalizedOHMUrl product har others import (hap/in-app hsp)', function () { 593 this.rollup.build(); 594 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 595 this.rollup.share.projectConfig.pkgContextInfo = { 596 'pkghar': { 597 'packageName': 'pkghar', 598 'bundleName': '', 599 'moduleName': '', 600 'version': '1.0.1', 601 'entryPath': 'Index.ets', 602 'isSO': false 603 } 604 } 605 const filePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/src/main/ets/utils/Calc.ets'; 606 const indexFilePath: string = '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar/Index.ets'; 607 for (let file of [filePath, indexFilePath]) { 608 const moduleInfo = { 609 id: file, 610 meta: { 611 pkgName: 'pkghar', 612 pkgPath: '/testHap/oh_modules/.ohpm/pkghar@test=/oh_modules/pkghar' 613 } 614 } 615 this.rollup.moduleInfos.push(moduleInfo); 616 } 617 const importerFile: string = '/testHar/entry/src/main/ets/pages/index.ets' 618 const importByPkgName = 'pkghar'; 619 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 620 const moduleSourceFile: string = new ModuleSourceFile(); 621 ModuleSourceFile.initPluginEnv(this.rollup); 622 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 623 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 624 importerFile); 625 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&pkghar/Index&1.0.1'; 626 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&pkghar/src/main/ets/utils/Calc&1.0.1'; 627 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 628 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 629 }); 630 631 mocha.it('NormalizedOHMUrl remote source code har self import (inter-app hsp)', function () { 632 this.rollup.build(); 633 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 634 this.rollup.share.projectConfig.pkgContextInfo = { 635 'pkghar': { 636 'packageName': 'pkghar', 637 'bundleName': 'com.test.testHsp', 638 'moduleName': '', 639 'version': '1.0.1', 640 'entryPath': 'Index.ets', 641 'isSO': false 642 } 643 } 644 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 645 const moduleInfo = { 646 id: filePath, 647 meta: { 648 pkgName: 'pkghar', 649 pkgPath: '/testHsp/har' 650 } 651 } 652 this.rollup.moduleInfos.push(moduleInfo); 653 const importerFile: string = '/testHsp/har/src/main/ets/pages/Index.ets' 654 const relativePath: string = '../utils/Calc'; 655 const etsBasedAbsolutePath: string = 'ets/utils/Calc'; 656 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 657 const moduleSourceFile: string = new ModuleSourceFile(); 658 ModuleSourceFile.initPluginEnv(this.rollup); 659 const relativePathOhmUrl: string = moduleSourceFile.getOhmUrl(this.rollup, relativePath, filePath, importerFile); 660 const etsBasedAbsolutePathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, etsBasedAbsolutePath, filePath, 661 importerFile); 662 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 663 importerFile); 664 const expectedNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 665 expect(relativePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 666 expect(etsBasedAbsolutePathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 667 expect(standardImportPathOhmUrl == expectedNormalizedOhmUrl).to.be.true; 668 }); 669 670 mocha.it('NormalizedOHMUrl remote source code har others import (inter-app hsp)', function () { 671 this.rollup.build(); 672 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 673 this.rollup.share.projectConfig.pkgContextInfo = { 674 'pkghar': { 675 'packageName': 'pkghar', 676 'bundleName': 'com.test.testHsp', 677 'moduleName': '', 678 'version': '1.0.1', 679 'entryPath': 'Index.ets', 680 'isSO': false 681 } 682 } 683 const filePath: string = '/testHsp/har/src/main/ets/utils/Calc.ets'; 684 const indexFilePath: string = '/testHsp/har/Index.ets'; 685 for (let file of [filePath, indexFilePath]) { 686 const moduleInfo = { 687 id: file, 688 meta: { 689 pkgName: 'pkghar', 690 pkgPath: '/testHsp/har' 691 } 692 } 693 this.rollup.moduleInfos.push(moduleInfo); 694 } 695 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 696 const importByPkgName = 'pkghar'; 697 const standardImportPath: string = 'pkghar/src/main/ets/utils/Calc'; 698 const moduleSourceFile: string = new ModuleSourceFile(); 699 ModuleSourceFile.initPluginEnv(this.rollup); 700 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 701 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 702 importerFile); 703 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&pkghar/Index&1.0.1'; 704 const standardImportPathNormalizedOhmUrl: string = 705 '@normalized:N&&com.test.testHsp&pkghar/src/main/ets/utils/Calc&1.0.1'; 706 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 707 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 708 }); 709 710 mocha.it('NormalizedOHMUrl native so others import (hap/in-app hsp)', function () { 711 this.rollup.build(); 712 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 713 this.rollup.share.projectConfig.pkgContextInfo = { 714 'libproduct.so': { 715 'packageName': 'libproduct.so', 716 'bundleName': '', 717 'moduleName': '', 718 'version': '', 719 'entryPath': '', 720 'isSO': true 721 } 722 } 723 const importerFile: string = '/testHap/hsp/src/main/ets/pages/Index.ets' 724 const moduleRequest = 'libproduct.so'; 725 const moduleSourceFile: string = new ModuleSourceFile(); 726 ModuleSourceFile.initPluginEnv(this.rollup); 727 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 728 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libproduct.so&'; 729 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 730 }); 731 732 mocha.it('NormalizedOHMUrl native so others import (inter-app hsp)', function () { 733 this.rollup.build(); 734 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 735 this.rollup.share.projectConfig.pkgContextInfo = { 736 'libproduct.so': { 737 'packageName': 'libproduct.so', 738 'bundleName': 'com.test.testHsp', 739 'moduleName': '', 740 'version': '', 741 'entryPath': '', 742 'isSO': true 743 } 744 } 745 const importerFile: string = '/testHsp/hsp/src/main/ets/pages/Index.ets' 746 const moduleRequest = 'libproduct.so'; 747 const moduleSourceFile: string = new ModuleSourceFile(); 748 ModuleSourceFile.initPluginEnv(this.rollup); 749 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 750 const expectedNormalizedOhmUrl: string = '@normalized:Y&&com.test.testHsp&libproduct.so&'; 751 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 752 }); 753 754 mocha.it('NormalizedOHMUrl native so others import (source code har)', function () { 755 this.rollup.build(); 756 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 757 this.rollup.share.projectConfig.pkgContextInfo = { 758 'libhar.so': { 759 'packageName': 'libhar.so', 760 'bundleName': '', 761 'moduleName': '', 762 'version': '', 763 'entryPath': '', 764 'isSO': true 765 } 766 } 767 const importerFile: string = '/testHap/har/src/main/ets/pages/Index.ets' 768 const moduleRequest = 'libhar.so'; 769 const moduleSourceFile: string = new ModuleSourceFile(); 770 ModuleSourceFile.initPluginEnv(this.rollup); 771 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 772 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; 773 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 774 }); 775 776 mocha.it('NormalizedOHMUrl native so others import (product har)', function () { 777 this.rollup.build(); 778 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 779 this.rollup.share.projectConfig.pkgContextInfo = { 780 'libhar.so': { 781 'packageName': 'libhar.so', 782 'bundleName': '', 783 'moduleName': '', 784 'version': '', 785 'entryPath': '', 786 'isSO': true 787 } 788 } 789 const importerFile: string = 790 '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; 791 const moduleRequest = 'libhar.so'; 792 const moduleSourceFile: string = new ModuleSourceFile(); 793 ModuleSourceFile.initPluginEnv(this.rollup); 794 const moduleRequestOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, undefined, importerFile); 795 const expectedNormalizedOhmUrl: string = '@normalized:Y&&&libhar.so&'; 796 expect(moduleRequestOhmUrl == expectedNormalizedOhmUrl).to.be.true; 797 }); 798 799 mocha.it('NormalizedOHMUrl ohpm package others import (hap/in-app hsp)', function () { 800 this.rollup.build(); 801 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 802 this.rollup.share.projectConfig.pkgContextInfo = { 803 '@ohos/Test': { 804 'packageName': '@ohos/Test', 805 'bundleName': '', 806 'moduleName': '', 807 'version': '2.3.1', 808 'entryPath': 'index.ets', 809 'isSO': false 810 } 811 } 812 const filePath: string = 813 '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' 814 const indexFilePath: string = '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; 815 for (let file of [filePath, indexFilePath]) { 816 const moduleInfo = { 817 id: file, 818 meta: { 819 pkgName: '@ohos/Test', 820 pkgPath: '/testHap/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' 821 } 822 } 823 this.rollup.moduleInfos.push(moduleInfo); 824 } 825 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 826 const importByPkgName = '@ohos/Test'; 827 const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; 828 const moduleSourceFile: string = new ModuleSourceFile(); 829 ModuleSourceFile.initPluginEnv(this.rollup); 830 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 831 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 832 importerFile); 833 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/index&2.3.1'; 834 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; 835 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 836 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 837 }); 838 839 mocha.it('NormalizedOHMUrl ohpm package others import (inter-app hsp)', function () { 840 this.rollup.build(); 841 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 842 this.rollup.share.projectConfig.pkgContextInfo = { 843 '@ohos/Test': { 844 'packageName': '@ohos/Test', 845 'bundleName': 'com.test.testHsp', 846 'moduleName': '', 847 'version': '2.3.1', 848 'entryPath': 'index.ets', 849 'isSO': false 850 } 851 } 852 const filePath: string = 853 '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/src/main/ets/utils/Calc.ets' 854 const indexFilePath: string = '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test/index.ets'; 855 for (let file of [filePath, indexFilePath]) { 856 const moduleInfo = { 857 id: file, 858 meta: { 859 pkgName: '@ohos/Test', 860 pkgPath: '/testHsp/oh_modules/.ohpm/@ohos+test@2.3.1/oh_modules/@ohos/test' 861 } 862 } 863 this.rollup.moduleInfos.push(moduleInfo); 864 } 865 const importerFile: string = '/testHsp/entry/src/main/ets/pages/index.ets' 866 const importByPkgName = '@ohos/Test'; 867 const standardImportPath: string = '@ohos/Test/src/main/ets/utils/Calc'; 868 const moduleSourceFile: string = new ModuleSourceFile(); 869 ModuleSourceFile.initPluginEnv(this.rollup); 870 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 871 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 872 importerFile); 873 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&com.test.testHsp&@ohos/Test/index&2.3.1'; 874 const standardImportPathNormalizedOhmUrl: string = 875 '@normalized:N&&com.test.testHsp&@ohos/Test/src/main/ets/utils/Calc&2.3.1'; 876 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 877 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 878 }); 879 880 mocha.it('the error message of getNormalizedOhmUrlByFilepath', function () { 881 this.rollup.build(); 882 const pkgParams = { 883 pkgName: 'json5', 884 pkgPath: `${projectConfig.projectRootPath}/entry/oh_modules/json5`, 885 isRecordName: false 886 }; 887 projectConfig.pkgContextInfo = { 888 'json5': undefined 889 }; 890 const red: string = '\u001b[31m'; 891 const reset: string = '\u001b[39m'; 892 const filePath: string = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; 893 const moduleName: string = 'entry'; 894 const importerFile: string = 'importTest.ts'; 895 const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) 896 const loggerStub = sinon.stub(logger, 'error'); 897 try { 898 getNormalizedOhmUrlByFilepath(filePath, projectConfig, logger, pkgParams, importerFile); 899 } catch (e) { 900 } 901 expect(loggerStub.calledWith(red, 'ArkTS:ERROR Failed to resolve OhmUrl.\n' + 902 `Error Message: Failed to get a resolved OhmUrl for "${filePath}" imported by "${importerFile}".\n` + 903 `Solutions: > Check whether the module which ${filePath} belongs to is correctly configured.` + 904 '> Check the corresponding file name is correct(including case-sensitivity).', reset)).to.be.true; 905 loggerStub.restore(); 906 }); 907 908 mocha.it('transform mockConfigInfo', function () { 909 this.rollup.preview(); 910 ModuleSourceFile.mockConfigInfo = PRVIEW_MOCK_CONFIG; 911 this.rollup.share.projectConfig.modulePath = `${projectConfig.projectRootPath}/entry`; 912 this.rollup.share.projectConfig.mockParams = { 913 etsSourceRootPath: 'src/main/ets', 914 mockConfigPath: `${projectConfig.projectRootPath}/entry/src/mock/mock-config.json5` 915 } 916 this.rollup.share.projectConfig.entryModuleName = 'entry'; 917 const importerFile: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/index.ets`; 918 const moduleInfo = { 919 id: importerFile, 920 meta: { 921 moduleName: 'entry', 922 } 923 }; 924 this.rollup.moduleInfos.push(moduleInfo); 925 for (let moduleRequest in PRVIEW_MOCK_CONFIG) { 926 let mockPath = PRVIEW_MOCK_CONFIG[moduleRequest] 927 let filePath: string; 928 if (Object.prototype.hasOwnProperty.call(MOCK_CONFIG_FILEPATH, moduleRequest)) { 929 filePath = MOCK_CONFIG_FILEPATH[moduleRequest]; 930 const moduleInfo = { 931 id: filePath, 932 meta: { 933 moduleName: moduleRequest === 'lib' ? 'lib' : 'entry', 934 pkgName: moduleRequest === 'lib' ? 'lib' : 'entry', 935 pkgPath: moduleRequest === 'lib' ? `${projectConfig.projectRootPath}/oh_modules/lib` : 936 `${projectConfig.projectRootPath}/entry` 937 } 938 }; 939 this.rollup.moduleInfos.push(moduleInfo); 940 } 941 const moduleSourceFile: string = new ModuleSourceFile(); 942 ModuleSourceFile.initPluginEnv(this.rollup); 943 ModuleSourceFile.setProcessMock(this.rollup); 944 moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, filePath, importerFile); 945 } 946 const expectMockConfig: Object = { 947 '@ohos:bluetooth': { 948 source: '@bundle:com.example.app/entry/mock/ohos/bluetooth.mock' 949 }, 950 '@bundle:com.example.app/entry/ets/calc': { 951 source: '@bundle:com.example.app/entry/mock/module/calc.mock' 952 }, 953 '@bundle:/testProjectRootPath/oh_modules/lib/dist/index.js': { 954 source: '@bundle:com.example.app/entry/mock/module/bigInt.mock' 955 }, 956 '@app:UtTestApplication/entry/entry': { 957 source: '@bundle:com.example.app/entry/mock/native/libentry.mock' 958 } 959 }; 960 expect(ModuleSourceFile.newMockConfigInfo.toString() === expectMockConfig.toString()).to.be.true; 961 ModuleSourceFile.cleanUpObjects(); 962 }); 963 964 mocha.it('NormalizedOHMUrl transform mockConfigInfo', function () { 965 this.rollup.preview(); 966 this.rollup.useNormalizedOHMUrl(); 967 this.rollup.share.projectConfig.pkgContextInfo = { 968 'entry': { 969 'packageName': 'entry', 970 'bundleName': '', 971 'moduleName': '', 972 'version': '', 973 'entryPath': 'index.ets', 974 'isSO': false 975 }, 976 'lib': { 977 'packageName': 'lib', 978 'bundleName': '', 979 'moduleName': 'lib', 980 'version': '', 981 'entryPath': 'index.ets', 982 'isSO': false 983 }, 984 'libentry.so': { 985 'packageName': 'libentry.so', 986 'bundleName': '', 987 'moduleName': '', 988 'version': '', 989 'entryPath': '', 990 'isSO': true 991 } 992 }; 993 ModuleSourceFile.mockConfigInfo = PRVIEW_MOCK_CONFIG; 994 this.rollup.share.projectConfig.modulePath = `${projectConfig.projectRootPath}/entry`; 995 this.rollup.share.projectConfig.mockParams = { 996 etsSourceRootPath: 'src/main/ets', 997 mockConfigPath: `${projectConfig.projectRootPath}/entry/src/mock/mock-config.json5` 998 } 999 this.rollup.share.projectConfig.entryModuleName = 'entry'; 1000 1001 const importerFile: string = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/index.ets`; 1002 const moduleInfo = { 1003 id: importerFile, 1004 meta: { 1005 moduleName: 'entry', 1006 pkgName: 'entry', 1007 pkgPath: `${projectConfig.projectRootPath}/entry` 1008 } 1009 } 1010 this.rollup.moduleInfos.push(moduleInfo); 1011 1012 for (let moduleRequest in PRVIEW_MOCK_CONFIG) { 1013 let mockPath = PRVIEW_MOCK_CONFIG[moduleRequest] 1014 let filePath: string; 1015 if (Object.prototype.hasOwnProperty.call(MOCK_CONFIG_FILEPATH, moduleRequest)) { 1016 filePath = MOCK_CONFIG_FILEPATH[moduleRequest]; 1017 const moduleInfo = { 1018 id: filePath, 1019 meta: { 1020 moduleName: moduleRequest === 'lib' ? 'lib' : 'entry', 1021 pkgName: moduleRequest === 'lib' ? 'lib' : 'entry', 1022 pkgPath: moduleRequest === 'lib' ? `${projectConfig.projectRootPath}/oh_modules/lib` : 1023 `${projectConfig.projectRootPath}/entry` 1024 } 1025 } 1026 this.rollup.moduleInfos.push(moduleInfo); 1027 } 1028 const moduleSourceFile: string = new ModuleSourceFile(); 1029 ModuleSourceFile.initPluginEnv(this.rollup); 1030 ModuleSourceFile.setProcessMock(this.rollup); 1031 moduleSourceFile.getOhmUrl(this.rollup, moduleRequest, filePath, importerFile); 1032 } 1033 const expectMockConfig = { 1034 '@ohos:bluetooth': { 1035 source: '@normalized:N&&&entry/src/main/mock/ohos/bluetooth.mock&' 1036 }, 1037 '@normalized:N&&&entry/src/main/ets/calc&': { 1038 source: '@normalized:N&&&entry/src/main/mock/module/calc.mock&' 1039 }, 1040 '@normalized:N&lib&&lib/dist/index&': { 1041 source: '@normalized:N&&&entry/src/main/mock/module/bigInt.mock&' 1042 }, 1043 '@normalized:Y&&&libentry.so&': { 1044 source: '@normalized:N&&&entry/src/main/mock/native/libentry.mock&' 1045 } 1046 }; 1047 expect(ModuleSourceFile.newMockConfigInfo.toString() === expectMockConfig.toString()).to.be.true; 1048 ModuleSourceFile.cleanUpObjects(); 1049 }); 1050 1051 mocha.it('NormalizedOHMUrl bytecode har import', function () { 1052 this.rollup.build(); 1053 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1054 this.rollup.share.projectConfig.pkgContextInfo = { 1055 'bytecode_har': { 1056 'packageName': 'bytecode_har', 1057 'bundleName': '', 1058 'moduleName': '', 1059 'version': '1.0.0', 1060 'entryPath': 'Index.ets', 1061 'isSO': false 1062 }, 1063 'bytecode_alias_oh': { 1064 'packageName': 'bytecode_alias_oh', 1065 'bundleName': '', 1066 'moduleName': '', 1067 'version': '1.0.0', 1068 'entryPath': 'Index.ets', 1069 'isSO': false 1070 } 1071 } 1072 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 1073 ['bytecode_alias', 'bytecode_har', 'bytecode_alias_oh'] 1074 ]); 1075 this.rollup.share.projectConfig.byteCodeHarInfo = { 1076 'bytecode_alias': { 1077 'abcPath':'D:\\projectPath\\bytecode_har\\modules.abc' 1078 }, 1079 'bytecode_alias_oh': { 1080 'abcPath':'D:\\projectPath\\bytecode_alias_oh\\modules.abc' 1081 } 1082 } 1083 const filePath: string = 'bytecode_alias/src/main/ets/utils/Calc'; 1084 const indexFilePath: string = 'bytecode_alias'; 1085 1086 const importerFile: string = '/testHap/entry/src/main/ets/pages/index.ets' 1087 const importByPkgName = 'bytecode_alias'; 1088 const standardImportPath: string = 'bytecode_alias/src/main/ets/utils/Calc'; 1089 const importByPkgNameSlashes = 'bytecode_alias///'; 1090 const importByPkgNameSlashesOh = 'bytecode_alias_oh///'; 1091 const moduleSourceFile: string = new ModuleSourceFile(); 1092 ModuleSourceFile.initPluginEnv(this.rollup); 1093 const importByPkgNameOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, importByPkgName, indexFilePath, importerFile); 1094 const standardImportPathOhmUrl = moduleSourceFile.getOhmUrl(this.rollup, standardImportPath, filePath, 1095 importerFile); 1096 const importByPkgNameOhmUrlSlashes = moduleSourceFile.getOhmUrl(this.rollup, importByPkgNameSlashes, indexFilePath, importerFile); 1097 const importByPkgNameOhmUrlSlashesOh = moduleSourceFile.getOhmUrl(this.rollup, importByPkgNameSlashesOh, indexFilePath, importerFile); 1098 const importByPkgNameNormalizedOhmUrl: string = '@normalized:N&&&bytecode_har/Index&1.0.0'; 1099 const standardImportPathNormalizedOhmUrl: string = '@normalized:N&&&bytecode_har/src/main/ets/utils/Calc&1.0.0'; 1100 const importByPkgNameNormalizedOhmUrlSlashes: string = '@normalized:N&&&bytecode_har/Index&1.0.0'; 1101 const importByPkgNameNormalizedOhmUrlSlashesOh: string = '@normalized:N&&&bytecode_alias_oh/Index&1.0.0'; 1102 expect(importByPkgNameOhmUrl == importByPkgNameNormalizedOhmUrl).to.be.true; 1103 expect(standardImportPathOhmUrl == standardImportPathNormalizedOhmUrl).to.be.true; 1104 expect(importByPkgNameOhmUrlSlashes == importByPkgNameNormalizedOhmUrlSlashes).to.be.true; 1105 expect(importByPkgNameOhmUrlSlashesOh == importByPkgNameNormalizedOhmUrlSlashesOh).to.be.true; 1106 }); 1107 1108 mocha.it('useNormalizedOHMUrl app builtins error message', function () { 1109 this.rollup.build(); 1110 this.rollup.useNormalizedOHMUrl(); 1111 this.rollup.share.projectConfig.pkgContextInfo = {}; 1112 const red: string = '\u001b[31m'; 1113 const reset: string = '\u001b[39m'; 1114 const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME); 1115 const loggerStub = sinon.stub(logger, 'error'); 1116 const importerFile: string = 1117 '/testHap/oh_modules/.ohpm/pkghar@test+har=/oh_modules/pkghar/src/main/ets/pages/Index.ets'; 1118 const appSoModuleRequest: string = 'libapplication.so'; 1119 try { 1120 getOhmUrlBySystemApiOrLibRequest(appSoModuleRequest, this.rollup.share.projectConfig, logger, 1121 importerFile, true); 1122 } catch (e) { 1123 } 1124 expect(loggerStub.calledWith(red, 'ArkTS:INTERNAL ERROR: Can not get pkgContextInfo of package ' + 1125 `'${appSoModuleRequest}' which being imported by '${importerFile}'`, reset)).to.be.true; 1126 loggerStub.restore(); 1127 }); 1128 1129 mocha.it('the error message of getNormalizedOhmUrlByAliasName', function () { 1130 this.rollup.build(); 1131 this.rollup.share.projectConfig.useNormalizedOHMUrl = true; 1132 this.rollup.share.projectConfig.pkgContextInfo = { 1133 'bytecode_har': { 1134 'packageName': 'bytecode_har', 1135 'bundleName': '', 1136 'moduleName': '', 1137 'version': '1.0.0', 1138 'entryPath': 'Index.ets', 1139 'isSO': false 1140 } 1141 } 1142 this.rollup.share.projectConfig.dependencyAliasMap = new Map([ 1143 ['bytecode_alias', 'bytecode_har'] 1144 ]); 1145 this.rollup.share.projectConfig.byteCodeHarInfo = { 1146 'bytecode_alias': { 1147 'abcPath': 'D:\\projectPath\\bytecode_har\\modules.abc' 1148 } 1149 } 1150 1151 const aliasPkgName = 'bytecode_alias'; 1152 const pkgName = 'bytecode_har'; 1153 const logger = this.rollup.share.getLogger(GEN_ABC_PLUGIN_NAME) 1154 const loggerStub = sinon.stub(logger, 'error'); 1155 const red: string = '\u001b[31m'; 1156 const reset: string = '\u001b[39m'; 1157 1158 try { 1159 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']['entryPath']; 1160 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1161 } catch (e) { 1162 } 1163 1164 expect(loggerStub.getCall(0).calledWith(red, `ArkTS:INTERNAL ERROR: Failed to find entry file of '${pkgName}'.\n` + 1165 `Error Message: Failed to obtain the entry file information of '${pkgName}' from the package context information.`, reset)).to.be.true; 1166 1167 try { 1168 delete this.rollup.share.projectConfig.pkgContextInfo['bytecode_har']; 1169 getNormalizedOhmUrlByAliasName(aliasPkgName, this.rollup.share.projectConfig, logger); 1170 } catch (e) { 1171 } 1172 1173 expect(loggerStub.getCall(1).calledWith(red, `ArkTS:INTERNAL ERROR: Failed to find package '${pkgName}'.\n` + 1174 `Error Message: Failed to obtain package '${pkgName}' from the package context information.`, reset)).to.be.true; 1175 1176 loggerStub.restore(); 1177 }); 1178});