From 36b0fbbb4e34f8e3ac220a037b8b592c1b0084e1 Mon Sep 17 00:00:00 2001 From: wuhailong Date: Thu, 15 May 2025 11:20:07 +0800 Subject: [PATCH] Fix the mock bug Issue: #IC8J8E Signed-off-by: wuhailong Change-Id: I39ead837c0de7d604f6dc1b7708cc1ef737b7b40 --- .../ark_compiler/module/module_source_file.ts | 26 +++++- .../module/module_source_file.test.ts | 92 +++++++++++++++++-- 2 files changed, 104 insertions(+), 14 deletions(-) diff --git a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts index c6b75a9d6..f2a2669f1 100644 --- a/compiler/src/fast_build/ark_compiler/module/module_source_file.ts +++ b/compiler/src/fast_build/ark_compiler/module/module_source_file.ts @@ -125,6 +125,7 @@ export class ModuleSourceFile { // "etsSourceRootPath": "path of ets source root", // "mockConfigPath": "path of mock configuration file" // "mockConfigKey2ModuleInfo": "moduleInfo of mock-config key" + // "source2ModuleIdMap": "moduleId of mock-config source value" // } ModuleSourceFile.needProcessMock = (rollupObject.share.projectConfig.mockParams && rollupObject.share.projectConfig.mockParams.etsSourceRootPath && @@ -203,12 +204,17 @@ export class ModuleSourceFile { let mockFile: string = ModuleSourceFile.transformedHarOrHspMockConfigInfo[transKey] ? ModuleSourceFile.transformedHarOrHspMockConfigInfo[transKey].source : ModuleSourceFile.mockConfigInfo[originKey].source; - let mockFilePath: string = `${toUnixPath(rollupObject.share.projectConfig.modulePath)}/${mockFile}`; + let source2ModuleIdMap: Map = new Map; + if (rollupObject.share.projectConfig.mockParams?.source2ModuleIdMap && + rollupObject.share.projectConfig.mockParams?.source2ModuleIdMap.size > 0) { + source2ModuleIdMap = rollupObject.share.projectConfig.mockParams?.source2ModuleIdMap; + } + let mockFilePath: string = source2ModuleIdMap.size > 0 ? + source2ModuleIdMap.get(mockFile) : + `${toUnixPath(rollupObject.share.projectConfig.modulePath)}/${mockFile}`; let mockFileOhmUrl: string = ''; if (useNormalizedOHMUrl) { - // For file A that imports file B, the mock file of file B will be located in the same package of file A. So the - // moduleInfo for mock file should be the same with file A. - const targetModuleInfo: Object = rollupObject.getModuleInfo(importerFile); + const targetModuleInfo: Object = ModuleSourceFile.getModuleInfoOfMockFile(mockFilePath, rollupObject, source2ModuleIdMap); mockFileOhmUrl = ModuleSourceFile.spliceNormalizedOhmurl(targetModuleInfo, mockFilePath, importerFile); } else { mockFileOhmUrl = getOhmUrlByFilepath(mockFilePath, @@ -223,6 +229,18 @@ export class ModuleSourceFile { ModuleSourceFile.addMockConfig(ModuleSourceFile.newMockConfigInfo, transKey, mockFileOhmUrl); } + static getModuleInfoOfMockFile(filePath: string, rollupObject: Object, source2ModuleIdMap: Map): Object { + if (source2ModuleIdMap.size > 0) { + return rollupObject.getModuleInfo(filePath); + } + return { + meta: { + pkgName: rollupObject.share.projectConfig.entryPackageName, + pkgPath: rollupObject.share.projectConfig.modulePath + } + }; + } + static isMockFile(file: string, rollupObject: Object): boolean { if (!ModuleSourceFile.needProcessMock) { return false; diff --git a/compiler/test/ark_compiler_ut/module/module_source_file.test.ts b/compiler/test/ark_compiler_ut/module/module_source_file.test.ts index 5828a78f8..100481cdd 100644 --- a/compiler/test/ark_compiler_ut/module/module_source_file.test.ts +++ b/compiler/test/ark_compiler_ut/module/module_source_file.test.ts @@ -967,16 +967,7 @@ mocha.describe('test module_source_file file api', function () { expect(ModuleSourceFile.newMockConfigInfo).to.deep.equal(EXPECT_NEW_MOCK_INFO); }); - - mocha.it('5-5: test isMockFile under LocalTest mode', function () { - ModuleSourceFile.needProcessMock = true; - ModuleSourceFile.mockFiles = ["src/mock/ohos/I18nMock.ts"]; - let file = this.rollup.share.projectConfig.aceModuleRoot +'/mock/ohos/I18nMock.ts'; - expect(ModuleSourceFile.isMockFile(file, this.rollup)).to.be.true; - ModuleSourceFile.needProcessMock = false; - }); - - mocha.it('5-6: test generateMockConfigFile under LocalTest mode', function () { + mocha.it('5-5: test generateMockConfigFile under LocalTest mode', function () { this.rollup.share.projectConfig.isLocalTest = true; ModuleSourceFile.newMockConfigInfo = { "@ohos:i18n": { @@ -1011,6 +1002,79 @@ mocha.describe('test module_source_file file api', function () { fs.unlinkSync(this.rollup.share.projectConfig.cachePath + '/mock-config.json5'); }); + mocha.it('5-6: test generateNewMockInfo under LocalTest mode (useNormalizedOHMUrl is true)', function () { + this.rollup.share.projectConfig.isLocalTest = true; + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + ModuleSourceFile.projectConfig.pkgContextInfo = { + 'entry': { + 'packageName': 'entry', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + ModuleSourceFile.mockConfigInfo = { + "@ohos.utils": { + "source": "src/mock/utilsMock.ts" + }, + } + let originKey = "@ohos.utils"; + let transKey = "@ohos:utils"; + ModuleSourceFile.generateNewMockInfo(originKey, transKey, this.rollup, ''); + let expectOHMUrl: string = "@normalized:N&&&entry/src/mock/utilsMock&"; + expect(ModuleSourceFile.newMockConfigInfo[transKey].source === expectOHMUrl).to.be.true; + this.rollup.share.projectConfig.useNormalizedOHMUrl = false; + ModuleSourceFile.projectConfig.pkgContextInfo = {}; + }); + + mocha.it('5-7: test generateNewMockInfo under LocalTest mode (has source2ModuleIdMap)', function () { + this.rollup.share.projectConfig.isLocalTest = true; + this.rollup.share.projectConfig.useNormalizedOHMUrl = true; + const mockFilePath = `${this.rollup.share.projectConfig.modulePath}/src/mock/utilsMock.ts`; + const moduleInfo = { + mockFilePath: { + meta: { + pkgName: 'entry', + pkgPath: this.rollup.share.projectConfig.modulePath + } + } + }; + this.rollup.moduleInfos.push(moduleInfo); + this.rollup.share.projectConfig.mockParams = { + decorator: "@MockSetup", + packageName: "@ohos/hamock", + etsSourceRootPath: "src/main", + mockConfigPath: this.rollup.share.projectConfig.aceModuleRoot + '/mock/mock-config.json5', + source2ModuleIdMap: { + "src/mock/utilsMock.ts": mockFilePath + } + } + ModuleSourceFile.projectConfig.pkgContextInfo = { + 'entry': { + 'packageName': 'entry', + 'bundleName': '', + 'moduleName': '', + 'version': '', + 'entryPath': 'Index.ets', + 'isSO': false + } + } + ModuleSourceFile.mockConfigInfo = { + "@ohos.utils": { + "source": "src/mock/utilsMock.ts" + }, + } + let originKey = "@ohos.utils"; + let transKey = "@ohos:utils"; + ModuleSourceFile.generateNewMockInfo(originKey, transKey, this.rollup, ''); + let expectOHMUrl: string = "@normalized:N&&&entry/src/mock/utilsMock&"; + expect(ModuleSourceFile.newMockConfigInfo[transKey].source === expectOHMUrl).to.be.true; + this.rollup.share.projectConfig.useNormalizedOHMUrl = false; + ModuleSourceFile.projectConfig.pkgContextInfo = {}; + }); + mocha.it('6-1: test removePotentialMockConfigCache delete mock-config', function () { const transformedMockConfigCache: string = path.resolve(this.rollup.share.projectConfig.cachePath, `./${TRANSFORMED_MOCK_CONFIG}`); @@ -1066,4 +1130,12 @@ mocha.describe('test module_source_file file api', function () { this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; stub.restore(); }); + + mocha.it('8-1: test isMockFile under LocalTest mode', function () { + ModuleSourceFile.needProcessMock = true; + ModuleSourceFile.mockFiles = ["src/mock/ohos/I18nMock.ts"]; + let file = this.rollup.share.projectConfig.aceModuleRoot +'/mock/ohos/I18nMock.ts'; + expect(ModuleSourceFile.isMockFile(file, this.rollup)).to.be.true; + ModuleSourceFile.needProcessMock = false; + }); }); \ No newline at end of file -- Gitee