diff --git a/compiler/test/ark_compiler_ut/mock/README_zh.md b/compiler/test/ark_compiler_ut/mock/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..c93c5b971a8ad56ec6e979255932bcebef3bd751 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/README_zh.md @@ -0,0 +1,70 @@ +# Mock + +#### 介绍 + +提供上游(IDE)触发编译构建时的输入rollup对象、类私有方法导出的Mock能力,供UT测试使用。 + +#### 软件架构 + +* mock/class_mock: 类私有方法Export Mock源码 +* mock/rollup_mock: DevEco上游rollup环节输入 Mock源码 + +#### 快速开始 + +ets-loader UT测试采用mocha架构实现,在此基础上介绍Rollup Mock、类私用方法Mock使用方法。 + +##### Rollup Mock使用 + +``` +import RollUpMock from './mock/rollup_mock/rollup_plugin_mock'; + +mocha.beforeEach(function () { + this.rollup = new RollUpMock(); +}); + +mocha.afterEach(() => { + delete this.rollup; +}); + +mocha.it('1-1: build debug, getBuildModeInLowerCase', function () { + this.rollup.build(); + let buildMode = getBuildModeInLowerCase(this.rollup.share.projectConfig); + expect(buildMode == 'debug').to.be.true; +}); +``` +如上,使用rollup数据前,请先在beforeEach/before时机创建RollupMock对象(默认为debug模式,请根据实际场景选择使用beforeEach或者before),调用build接口模拟IDE build Rollup初始化过程。同理preview接口模拟IDE preview Rollup初始化过程,hotreload接口模拟IDE hot reload Rollup初始化过程。 + +##### 类私用方法Mock使用 + +``` +import { ModuleHotfixMode } from '../../../../lib/fast_build/ark_compiler/module/module_hotfix_mode'; + +class ModuleHotfixModeMock extends ModuleHotfixMode { + generateEs2AbcCmdForHotfixMock() { + this.generateEs2AbcCmdForHotfix(); + } +} + +export default ModuleHotfixModeMock; +``` + +在测试私有方法前,请先参照上述案例追加类私有方法Mock实现。 +``` +import RollUpMock from '../mock/rollup_mock/rollup_plugin_mock'; +import ModuleHotfixModeMock from '../mock/class_mock/module_hotfix_mode_mock'; + +mocha.beforeEach(function () { + this.rollup = new RollUpMock(); +}); + +mocha.afterEach(() => { + delete this.rollup; +}); + +mocha.it('1-1: build debug, generateEs2AbcCmdForHotfix', function () { + this.rollup.build(); + let hotFixMode = new ModuleHotfixModeMock(this.rollup); + hotFixMode.generateEs2AbcCmdForHotfixMock(); +}); +``` +如上,调用Mock提供的接口,间接测试类私有方法。 diff --git a/compiler/test/ark_compiler_ut/mock/class_mock/module_hotfix_mode_mock.ts b/compiler/test/ark_compiler_ut/mock/class_mock/module_hotfix_mode_mock.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f2855296898bf9ad56c0bb8652fe6efd9e04885 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/class_mock/module_hotfix_mode_mock.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ModuleHotfixMode } from '../../../../lib/fast_build/ark_compiler/module/module_hotfix_mode'; + +class ModuleHotfixModeMock extends ModuleHotfixMode { + generateEs2AbcCmdForHotfixMock() { + this.generateEs2AbcCmdForHotfix(); + } +} + +export default ModuleHotfixModeMock; diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/cache.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/cache.ts new file mode 100644 index 0000000000000000000000000000000000000000..de734eca2e19292aa4d434d6b0758a5b81703857 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/cache.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + ARK_COMPILER_META_INFO, + IS_CACHE_INVALID +} from '../../../../lib/fast_build/ark_compiler/common/ark_define'; + +class Cache { + private cacheInfo: Map; + + constructor() { + this.cacheInfo = new Map(); + this.cacheInfo.set(ARK_COMPILER_META_INFO, {}); + this.cacheInfo.set(IS_CACHE_INVALID, undefined); + } + + public delete(key: string) { } + + public get(key: string) { + if (this.cacheInfo.has(key)) { + return this.cacheInfo.get(key); + } + return undefined; + } + + public has(key: string) { } + + public set(key: string, value: any) { + if (this.cacheInfo.has(key)) { + this.cacheInfo.set(key, value); + } + } +} + +export default Cache diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4041b74843cf418b0023c99ee413cb1efb59369 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/common.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const SDK_VERSION: number = 10; +export const ETS_LOADER_VERSION: string = '4.1.2.3'; + +export const BUNDLE_NAME_DEFAULT: string = 'com.example.app'; +export const ENTRY_MODULE_NAME_DEFAULT: string = 'entry'; + +export const RUNTIME_OS_OPENHARMONY: string = 'OpenHarmony'; +export const MODULE_NAME_HASH_DEFAULT: string = '1043bfc77febe75fafec0c4309faccf1'; +export const RESOURCE_TABLE_HASH_DEFAULT: string = '790527e39c8c2be7fbbc762f7966104e'; +export const DEVICE_TYPE: string = 'default,tablet'; + +export const NODE_JS_PATH: string = '/usr/local/nodejs'; +export const PORT_DEFAULT: string = '29900'; \ No newline at end of file diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1e52a50c566f7372f2b618feaf623f5d7e16d35 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/module_info.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Meta { + hostModulesInfo: Array; + moduleName: string; + isLocalDependency: boolean; + isNodeEntryFile: boolean; + pkgPath: string; + + constructor(entryModuleName: string, modulePath: string) { + this.hostModulesInfo = []; + this.moduleName = entryModuleName; + this.isLocalDependency = true; + this.isNodeEntryFile = false; + this.pkgPath = modulePath; + } +}; + +class ModuleInfo { + meta: Meta; + id: string; + + constructor(id: string, entryModuleName: string, modulePath: string) { + this.meta = new Meta(entryModuleName, modulePath); + this.id = id; + } +} + +export default ModuleInfo diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/path_config.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/path_config.ts new file mode 100644 index 0000000000000000000000000000000000000000..fe309614a00aecdaf15741a76e2a8956cd836bef --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/path_config.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import path from "path"; + +// project oh_modules path +export const OH_MODULES_OHPM_HYPIUM: string = 'oh_modules/.ohpm/@ohos+hypium@1.0.6/oh_modules/@ohos/hypium'; +export const OH_MODULES_OHOS_HYPIUM: string = 'oh_modules/@ohos/hypium'; + +// project root path, default project name +export const PROJECT_ROOT = path.resolve(__dirname, '../../../../test/ark_compiler_ut/testdata'); +export const DEFAULT_PROJECT: string = 'testcase_def'; + +// project module id +export const MODULE_ID_ROLLUP_PLACEHOLDER = "\x00rollup_plugin_ignore_empty_module_placeholder"; + +// project build node_modules +export const NODE_MODULES_PATH = "default/intermediates/loader_out/default/node_modules"; \ No newline at end of file diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts new file mode 100644 index 0000000000000000000000000000000000000000..b2e52aa0cf8ed2fe827afc166e424ad700444c13 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/project_config.ts @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { PROJECT_ROOT } from "./path_config"; +import { + SDK_VERSION, + BUNDLE_NAME_DEFAULT, + ETS_LOADER_VERSION, + ENTRY_MODULE_NAME_DEFAULT, + RUNTIME_OS_OPENHARMONY, + MODULE_NAME_HASH_DEFAULT, + RESOURCE_TABLE_HASH_DEFAULT, + DEVICE_TYPE, + NODE_JS_PATH, + PORT_DEFAULT +} from "./common"; +import { ESMODULE, OHPM, RELEASE } from "../../../../lib/fast_build/ark_compiler/common/ark_define"; + +interface IArkProjectConfig { + projectRootPath: string, + modulePathMap: any, + isOhosTest: any, + oldMapFilePath?: any, + processTs: boolean, + pandaMode: string, + anBuildOutPut?: any, + anBuildMode?: any, + apPath?: any, + nodeModulesPath?: any, + harNameOhmMap: any, + minPlatformVersion: number, + moduleName: string, + bundleName: string, + hotReload: any, + patchAbcPath: any, + changedFileList: any, + compileMode: string +} + +class ProjectConfig { + compileMode: string = ESMODULE; + packageManagerType: string = OHPM; + compileSdkVersion: number = SDK_VERSION; + compatibleSdkVersion: number = SDK_VERSION; + bundleName: string = BUNDLE_NAME_DEFAULT; + etsLoaderVersion: string = ETS_LOADER_VERSION; + etsLoaderReleaseType: string = RELEASE; + entryModuleName: string = ENTRY_MODULE_NAME_DEFAULT; + allModuleNameHash: string = MODULE_NAME_HASH_DEFAULT; + resourceTableHash: string = RESOURCE_TABLE_HASH_DEFAULT; + runtimeOS: string = RUNTIME_OS_OPENHARMONY; + sdkInfo: string = `true:${SDK_VERSION}:${ETS_LOADER_VERSION}:${RELEASE}`; + + watchMode: string; + isPreview: boolean; + buildMode: string; + localPropertiesPath: string; + aceProfilePath: string; + etsLoaderPath: string; + modulePath: string; + needCoverageInsert: boolean; + projectTopDir: string; + apPath: string; + aceModuleJsonPath: string; + appResource: string; + aceModuleRoot: string; + aceSuperVisualPath: string; + aceBuildJson: string; + cachePath: string; + aceModuleBuild: string; + supportChunks: boolean; + projectPath: string; + resolveModulePaths: Array; + compileHar: boolean; + compileShared: boolean; + moduleRootPath: any; + buildPath: string; + + deviceType?: string; + checkEntry?: string; + Path?: string; + note?: string; + hapMode?: string; + img2bin?: string; + projectProfilePath?: string; + logLevel?: string; + stageRouterConfig?: Array; + port?: string; + aceSoPath?: string; + + constructor(buildMode: string) { + this.watchMode = 'false'; + this.isPreview = false; + this.buildMode = buildMode; + this.needCoverageInsert = false; + this.supportChunks = true; + this.compileHar = false; + this.compileShared = false; + } + + public scan(testcase: string) { + this.initPath(`${PROJECT_ROOT}/${testcase}`); + } + + public setPreview(isPreview: boolean) { + this.isPreview = isPreview; + this.watchMode = String(isPreview); + } + + public setCompilerVersion(version: number) { + this.compileSdkVersion = version; + this.compatibleSdkVersion = version; + } + + private initPath(proPath: string) { + // build and preview + let mode = this.isPreview ? '.preview' : 'build'; + this.localPropertiesPath = `${proPath}/local.properties`; + this.aceProfilePath = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/res/default/resources/base/profile`; + this.etsLoaderPath = `/${this.runtimeOS}/Sdk/${this.compileSdkVersion}/ets/build-tools/ets-loader`; + this.modulePath = `${proPath}/${this.entryModuleName}`; + this.projectTopDir = `${proPath}`; + this.apPath = ''; + this.aceModuleJsonPath = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/res/default/module.json`; + this.appResource = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/res/default/ResourceTable.txt`; + this.aceModuleRoot = `${proPath}/${this.entryModuleName}/src/main/ets`; + this.aceSuperVisualPath = `${proPath}/${this.entryModuleName}/src/main/supervisual`; + this.aceBuildJson = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/loader/default/loader.json`; + this.cachePath = `${proPath}/${this.entryModuleName}/${mode}/default/cache/default/default@CompileArkTS/esmodule/${this.buildMode}`; + this.aceModuleBuild = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/loader_out/default/ets`; + this.projectPath = `${proPath}/${this.entryModuleName}/src/main/ets`; + this.moduleRootPath = undefined; + this.buildPath = `${proPath}/${this.entryModuleName}/${mode}/default/intermediates/loader_out/default/ets`; + + if (this.isPreview) { + this.previewUniqueConfig(); + } + } + + private previewUniqueConfig() { + this.deviceType = DEVICE_TYPE; + this.checkEntry = 'true'; + this.Path = NODE_JS_PATH; + this.note = 'false'; + this.hapMode = 'false'; + this.img2bin = 'true'; + this.projectProfilePath = `${this.projectTopDir}/build-profile.json5`; + this.logLevel = '3'; + this.stageRouterConfig = []; + this.port = PORT_DEFAULT; + this.aceSoPath = `${this.projectTopDir}/entry/.preview/cache/nativeDependencies.txt`; + } +} + +export { ProjectConfig, IArkProjectConfig } diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/rollup_plugin_mock.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/rollup_plugin_mock.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5a05594f34c4199f815aa567b6d2c8ea3c32a9b --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/rollup_plugin_mock.ts @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Cache from "./cache"; +import Share from "./share"; +import ModuleInfo from "./module_info"; +import { + SDK_VERSION, + BUNDLE_NAME_DEFAULT, + ETS_LOADER_VERSION, + RUNTIME_OS_OPENHARMONY, + MODULE_NAME_HASH_DEFAULT +} from "./common"; +import { DEFAULT_PROJECT, MODULE_ID_ROLLUP_PLACEHOLDER, NODE_MODULES_PATH } from "./path_config"; +import { scanFiles } from "../../utils/path_utils"; +import { IArkProjectConfig } from "./project_config"; +import { ESMODULE, RELEASE, DEBUG } from "../../../../lib/fast_build/ark_compiler/common/ark_define"; +import { + ARK_COMPILER_META_INFO, + IS_CACHE_INVALID +} from '../../../../lib/fast_build/ark_compiler/common/ark_define'; + +class RollUpPluginMock { + cache: Cache; + meta: any = { rollupVersion: '3.10.0', watchMode: false }; + moduleIds: any; + share: Share; + moduleInfos: Array; + + private isPreview: boolean = false; + + constructor() { + this.cache = new Cache(); + } + + public preConfig(buildMode: string = DEBUG) { + this.share = new Share(buildMode); + } + + public build(testcase: string = DEFAULT_PROJECT, buildMode: string = DEBUG) { + this.isPreview = false; + this.share = new Share(buildMode); + + this.share.projectConfig.setPreview(this.isPreview); + this.meta.watchMode = this.isPreview; + + this.doBuild(testcase); + } + + public preview(testcase: string = DEFAULT_PROJECT) { + this.isPreview = true; + this.share = new Share(DEBUG); + + this.share.projectConfig.setPreview(this.isPreview); + this.meta.watchMode = this.isPreview; + + this.doBuild(testcase); + } + + public hotReload(testcase: string = DEFAULT_PROJECT) { + this.isPreview = false; + this.share = new Share(DEBUG); + + this.share.projectConfig.setPreview(this.isPreview); + this.meta.watchMode = this.isPreview; + + this.doBuild(testcase); + } + + private doBuild(testcase: string) { + this.share.scan(testcase); + this.load(); + + // mock ets-loader build start + this.share.arkProjectConfig = this.mockArkProjectConfig(); + this.mockCheckArkCompilerCacheInfo(); + } + + private mockArkProjectConfig(): IArkProjectConfig { + const mode = this.isPreview ? '.preview' : 'build'; + const projectRootDir = this.share.projectConfig.projectTopDir; + const entryName = this.share.projectConfig.entryModuleName; + + return { + projectRootPath: projectRootDir, + modulePathMap: { entry: `${projectRootDir}/${entryName}` }, + isOhosTest: undefined, + processTs: false, + pandaMode: undefined, + nodeModulesPath: `${projectRootDir}/${entryName}/${mode}/${NODE_MODULES_PATH}`, + harNameOhmMap: {}, + minPlatformVersion: SDK_VERSION, + moduleName: `${entryName}`, + bundleName: BUNDLE_NAME_DEFAULT, + hotReload: undefined, + patchAbcPath: undefined, + changedFileList: undefined, + compileMode: ESMODULE + } + } + + private mockCheckArkCompilerCacheInfo(): void { + const metaInfos = [ + SDK_VERSION, SDK_VERSION, RUNTIME_OS_OPENHARMONY, `/OpenHarmony/Sdk/${SDK_VERSION}/ets/build-tools/app`, + ETS_LOADER_VERSION, RELEASE, BUNDLE_NAME_DEFAULT, + MODULE_NAME_HASH_DEFAULT, 'null_aotCompileMode', 'null_apPath' + ] + const metaInfo = metaInfos.join(':'); + this.cache.set(IS_CACHE_INVALID, true); + this.cache.set(ARK_COMPILER_META_INFO, metaInfo); + } + + public addWatchFile() { } + + public async(func: Function) { + if (func) { + func(); + } + } + + public block() { } + + public emitFile() { } + + public error() { } + + public getFileName() { } + + public getModuleIds(): IterableIterator { + return this.share.allFiles ? this.share.allFiles.values() : undefined; + } + + public getModuleInfo(id: string) { + for (let i = 0; i < this.moduleInfos.length - 1; i++) { + return this.moduleInfos.find(item => item.id === id); + } + } + + public getWatchFiles() { } + + public load() { + // load project files list + this.share.allFiles = new Set(); + scanFiles(this.share.projectConfig.projectPath, this.share.allFiles); + this.share.allFiles.add(MODULE_ID_ROLLUP_PLACEHOLDER); + + // load all files module info + const allFiles = Array.from(this.share.allFiles); + this.moduleInfos = new Array(); + for (let i = 0; i < allFiles.length - 1; i++) { + this.moduleInfos.push(new ModuleInfo(allFiles[i], + this.share.projectConfig.entryModuleName, + this.share.projectConfig.modulePath)); + } + } + + public parse() { } + + public resolve() { } + + public setAssetSource() { } + + public signal() { } + + public warn() { } +} + +export default RollUpPluginMock; diff --git a/compiler/test/ark_compiler_ut/mock/rollup_mock/share.ts b/compiler/test/ark_compiler_ut/mock/rollup_mock/share.ts new file mode 100644 index 0000000000000000000000000000000000000000..d13c2e6f0479a2d2ac9a65deca618eb842f43638 --- /dev/null +++ b/compiler/test/ark_compiler_ut/mock/rollup_mock/share.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { ProjectConfig, IArkProjectConfig } from "./project_config"; +import { OH_MODULES_OHPM_HYPIUM, OH_MODULES_OHOS_HYPIUM } from "./path_config"; + +class Logger { + private prefix: string; + + constructor(prefix: string) { + this.prefix = prefix; + } + + public debug(color: string, msg: string, reset: string) { + console.debug(`${color}${this.prefix}: ${JSON.stringify(msg)}${reset}`); + } + + public error(color: string, error: string, reset: string) { + console.error(`${color}${this.prefix}: ${JSON.stringify(error)}${reset}`); + } + +} + +class Share { + projectConfig: ProjectConfig; + arkProjectConfig: IArkProjectConfig; + symlinkMap = {}; + currentModuleMetaMap = {}; + + allComponents?: Map>; + allFiles?: Set; + + constructor(buildMode: string) { + this.projectConfig = new ProjectConfig(buildMode); + } + + public throwArkTsCompilerError(error: any) { + console.error(JSON.stringify(error)); + } + + public getLogger(prefix: string): Logger { + return new Logger(prefix); + } + + public scan(testcase: string) { + if (!testcase) { + return + } + this.projectConfig.scan(testcase); + this.symlinkMap[`${this.projectConfig.projectTopDir}/${OH_MODULES_OHPM_HYPIUM}`] = [ + `${this.projectConfig.projectTopDir}/${OH_MODULES_OHOS_HYPIUM}` + ]; + } +} + +export default Share diff --git a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts index a1752e4b85b775a65e986be1b1a0561e9efc5842..2c0e5a6e4dc039a965cb056084b38850dda824f7 100644 --- a/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts +++ b/compiler/test/ark_compiler_ut/module/ohmUrl/ohmUrl.test.ts @@ -21,104 +21,104 @@ import { PACKAGES } from '../../../../lib/pre_define'; import projectConfig from '../../utils/processProjectConfig'; import { projectConfig as mainProjectConfig } from '../../../../main'; -mocha.describe('generate ohmUrl', function() { - mocha.it('nested src main ets|js in filePath', function() { - const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` - + `subfeature/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; - let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); - let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); - let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; - let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; - expect(ohmUrl_1 == expected_1).to.be.true; - expect(ohmUrl_2 == expected_2).to.be.true; - }); +mocha.describe('generate ohmUrl', function () { + mocha.it('nested src main ets|js in filePath', function () { + const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/feature/src/main/js/` + + `subfeature/src/main/ets/pages/test.ts`; + const moduleName = 'entry'; + const moduleNamespace = 'library'; + let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); + let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); + let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; + let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; + expect(ohmUrl_1 == expected_1).to.be.true; + expect(ohmUrl_2 == expected_2).to.be.true; + }); - mocha.it('nested src ohosTest ets|js in filePath', function() { - const filePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` - + `subfeature/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; - let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); - let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); - let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; - let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; - expect(ohmUrl_1 == expected_1).to.be.true; - expect(ohmUrl_2 == expected_2).to.be.true; - }); + mocha.it('nested src ohosTest ets|js in filePath', function () { + const filePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/feature/src/main/js/` + + `subfeature/src/main/ets/pages/test.ts`; + const moduleName = 'entry'; + const moduleNamespace = 'library'; + let ohmUrl_1 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); + let ohmUrl_2 = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleNamespace); + let expected_1 = 'UtTestApplication/entry/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; + let expected_2 = 'UtTestApplication/entry@library/ets/feature/src/main/js/subfeature/src/main/ets/pages/test'; + expect(ohmUrl_1 == expected_1).to.be.true; + expect(ohmUrl_2 == expected_2).to.be.true; + }); - mocha.it('system builtins & app builtins', function() { - mainProjectConfig.bundleName = 'UtTestApplication'; - mainProjectConfig.moduleName = 'entry'; - const systemModuleRequest: string = '@system.app'; - const ohosModuleRequest: string = '@ohos.hilog'; - const appSoModuleRequest: string = 'libapplication.so'; - const systemOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(systemModuleRequest); - const ohosOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(ohosModuleRequest); - const appOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(appSoModuleRequest); - const expectedSystemOhmUrl: string = '@native:system.app'; - const expectedOhosOhmUrl: string = '@ohos:hilog'; - const expectedappOhmUrl: string = '@app:UtTestApplication/entry/application'; - expect(systemOhmUrl == expectedSystemOhmUrl).to.be.true; - expect(ohosOhmUrl == expectedOhosOhmUrl).to.be.true; - expect(appOhmUrl == expectedappOhmUrl).to.be.true; - }); + mocha.it('system builtins & app builtins', function () { + mainProjectConfig.bundleName = 'UtTestApplication'; + mainProjectConfig.moduleName = 'entry'; + const systemModuleRequest: string = '@system.app'; + const ohosModuleRequest: string = '@ohos.hilog'; + const appSoModuleRequest: string = 'libapplication.so'; + const systemOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(systemModuleRequest); + const ohosOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(ohosModuleRequest); + const appOhmUrl: string = getOhmUrlBySystemApiOrLibRequest(appSoModuleRequest); + const expectedSystemOhmUrl: string = '@native:system.app'; + const expectedOhosOhmUrl: string = '@ohos:hilog'; + const expectedappOhmUrl: string = '@app:UtTestApplication/entry/application'; + expect(systemOhmUrl == expectedSystemOhmUrl).to.be.true; + expect(ohosOhmUrl == expectedOhosOhmUrl).to.be.true; + expect(appOhmUrl == expectedappOhmUrl).to.be.true; + }); - mocha.it('shared library', function() { - const sharedLibraryPackageName: string = "@ohos/sharedLibrary"; - const sharedLibraryPage: string = "@ohos/sharedLibrary/src/main/ets/pages/page1"; - const errorSharedLibrary: string = "@ohos/staticLibrary"; - const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByHarName(sharedLibraryPackageName, projectConfig); - const sharedLibraryPageOhmUrl: string = getOhmUrlByHarName(sharedLibraryPage, projectConfig); - const errorSharedLibraryOhmUrl = getOhmUrlByHarName(errorSharedLibrary, projectConfig); - const expectedSharedLibraryOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/index"; - const expectedSharedLibraryPageOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/pages/page1"; - const expectedErrorSharedLibraryOhmUrl = undefined; - expect(sharedLibraryPackageNameOhmUrl == expectedSharedLibraryOhmUrl).to.be.true; - expect(sharedLibraryPageOhmUrl == expectedSharedLibraryPageOhmUrl).to.be.true; - expect(errorSharedLibraryOhmUrl == expectedErrorSharedLibraryOhmUrl).to.be.true; - }); + mocha.it('shared library', function () { + const sharedLibraryPackageName: string = "@ohos/sharedLibrary"; + const sharedLibraryPage: string = "@ohos/sharedLibrary/src/main/ets/pages/page1"; + const errorSharedLibrary: string = "@ohos/staticLibrary"; + const sharedLibraryPackageNameOhmUrl: string = getOhmUrlByHarName(sharedLibraryPackageName, projectConfig); + const sharedLibraryPageOhmUrl: string = getOhmUrlByHarName(sharedLibraryPage, projectConfig); + const errorSharedLibraryOhmUrl = getOhmUrlByHarName(errorSharedLibrary, projectConfig); + const expectedSharedLibraryOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/index"; + const expectedSharedLibraryPageOhmUrl: string = "@bundle:UtTestApplication/sharedLibrary/ets/pages/page1"; + const expectedErrorSharedLibraryOhmUrl = undefined; + expect(sharedLibraryPackageNameOhmUrl == expectedSharedLibraryOhmUrl).to.be.true; + expect(sharedLibraryPageOhmUrl == expectedSharedLibraryPageOhmUrl).to.be.true; + expect(errorSharedLibraryOhmUrl == expectedErrorSharedLibraryOhmUrl).to.be.true; + }); - mocha.it('project module', function() { - const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; - const harFilePath = `${projectConfig.projectRootPath}/library/src/main/ets/pages/test.ts`; - const moduleName = 'entry'; - const moduleNamespace = 'library'; - const ohmUrl = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); - const harOhmUrl = getOhmUrlByFilepath(harFilePath, projectConfig, undefined, moduleNamespace); - const expected = 'UtTestApplication/entry/ets/pages/test'; - const harOhmUrlExpected = 'UtTestApplication/entry@library/ets/pages/test'; - expect(ohmUrl == expected).to.be.true; - expect(harOhmUrl == harOhmUrlExpected).to.be.true; - }); + mocha.it('project module', function () { + const filePath = `${projectConfig.projectRootPath}/entry/src/main/ets/pages/test.ts`; + const harFilePath = `${projectConfig.projectRootPath}/library/src/main/ets/pages/test.ts`; + const moduleName = 'entry'; + const moduleNamespace = 'library'; + const ohmUrl = getOhmUrlByFilepath(filePath, projectConfig, undefined, moduleName); + const harOhmUrl = getOhmUrlByFilepath(harFilePath, projectConfig, undefined, moduleNamespace); + const expected = 'UtTestApplication/entry/ets/pages/test'; + const harOhmUrlExpected = 'UtTestApplication/entry@library/ets/pages/test'; + expect(ohmUrl == expected).to.be.true; + expect(harOhmUrl == harOhmUrlExpected).to.be.true; + }); - mocha.it('thirdParty module', function() { - const moduleLevelPkgPath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; - const projectLevelPkgPath = `${projectConfig.projectRootPath}/oh_modules/json5/dist/index.js`; - const moduleName = 'entry'; - const moduleLevelPkgOhmUrl = getOhmUrlByFilepath(moduleLevelPkgPath, projectConfig, undefined, undefined); - const projectLevelPkgOhmUrl = getOhmUrlByFilepath(projectLevelPkgPath, projectConfig, undefined, undefined); - const moduleLevelPkgOhmUrlExpected = `${PACKAGES}@${moduleName}/json5/dist/index`; - const projectLevelPkgOhmUrlExpected = `${PACKAGES}/json5/dist/index`; - expect(moduleLevelPkgOhmUrl == moduleLevelPkgOhmUrlExpected).to.be.true; - expect(projectLevelPkgOhmUrl == projectLevelPkgOhmUrlExpected).to.be.true; - }); + mocha.it('thirdParty module', function () { + const moduleLevelPkgPath = `${projectConfig.projectRootPath}/entry/oh_modules/json5/dist/index.js`; + const projectLevelPkgPath = `${projectConfig.projectRootPath}/oh_modules/json5/dist/index.js`; + const moduleName = 'entry'; + const moduleLevelPkgOhmUrl = getOhmUrlByFilepath(moduleLevelPkgPath, projectConfig, undefined, undefined); + const projectLevelPkgOhmUrl = getOhmUrlByFilepath(projectLevelPkgPath, projectConfig, undefined, undefined); + const moduleLevelPkgOhmUrlExpected = `${PACKAGES}@${moduleName}/json5/dist/index`; + const projectLevelPkgOhmUrlExpected = `${PACKAGES}/json5/dist/index`; + expect(moduleLevelPkgOhmUrl == moduleLevelPkgOhmUrlExpected).to.be.true; + expect(projectLevelPkgOhmUrl == projectLevelPkgOhmUrlExpected).to.be.true; + }); - mocha.it('static library entry', function() { - const staticLibraryEntry = `${projectConfig.projectRootPath}/library/index.ets`; - const moduleNamespace = 'library'; - const staticLibraryEntryOhmUrl = - getOhmUrlByFilepath(staticLibraryEntry, projectConfig, undefined, moduleNamespace); - const staticLibraryEntryOhmUrlExpected = 'UtTestApplication/entry@library/index'; - expect(staticLibraryEntryOhmUrl == staticLibraryEntryOhmUrlExpected).to.be.true; - }); + mocha.it('static library entry', function () { + const staticLibraryEntry = `${projectConfig.projectRootPath}/library/index.ets`; + const moduleNamespace = 'library'; + const staticLibraryEntryOhmUrl = + getOhmUrlByFilepath(staticLibraryEntry, projectConfig, undefined, moduleNamespace); + const staticLibraryEntryOhmUrlExpected = 'UtTestApplication/entry@library/index'; + expect(staticLibraryEntryOhmUrl == staticLibraryEntryOhmUrlExpected).to.be.true; + }); - mocha.it('ohosTest module', function() { - const ohosTestfilePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/pages/test.ts`; - const moduleName = 'entry'; - const ohmUrl = getOhmUrlByFilepath(ohosTestfilePath, projectConfig, undefined, moduleName); - const expected = 'UtTestApplication/entry/ets/pages/test'; - expect(ohmUrl == expected).to.be.true; - }); + mocha.it('ohosTest module', function () { + const ohosTestfilePath = `${projectConfig.projectRootPath}/entry/src/ohosTest/ets/pages/test.ts`; + const moduleName = 'entry'; + const ohmUrl = getOhmUrlByFilepath(ohosTestfilePath, projectConfig, undefined, moduleName); + const expected = 'UtTestApplication/entry/ets/pages/test'; + expect(ohmUrl == expected).to.be.true; + }); }); \ No newline at end of file diff --git a/compiler/test/ark_compiler_ut/utils/path_utils.ts b/compiler/test/ark_compiler_ut/utils/path_utils.ts new file mode 100644 index 0000000000000000000000000000000000000000..d807d6479d95ac9a7c578e5b55a39d5391ed2da2 --- /dev/null +++ b/compiler/test/ark_compiler_ut/utils/path_utils.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use rollupObject file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import fs from "fs"; +import path from "path"; +import { PROJECT_ROOT } from "../mock/rollup_mock/path_config"; +import { DEFAULT_PROJECT } from "../mock/rollup_mock/path_config"; + +export function getProjectPath(name?: string) { + return name ? `${PROJECT_ROOT}/${name}` : `${PROJECT_ROOT}/${DEFAULT_PROJECT}`; +} + +export function scanFiles(filepath: string, fileList: Set) { + if (!fs.existsSync(filepath)) { + return; + } + const files = fs.readdirSync(filepath); + files.forEach((file) => { + const child = path.join(filepath, file); + const stat = fs.statSync(child); + if (stat.isDirectory()) { + scanFiles(child, fileList); + } else { + fileList.add(child); + } + }); +} \ No newline at end of file diff --git a/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts b/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts index 9b8350785a119892bb82203e42c31621a7b981f1..7a7101330ad6b288701b1388e027850362945b1a 100644 --- a/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts +++ b/compiler/test/ark_compiler_ut/utils/processProjectConfig.ts @@ -21,13 +21,13 @@ const projectConfig: any = {} projectConfig.aceModuleJsonPath = path.resolve(__dirname, '../../../test/ark_compiler_ut/config/module.json'); projectConfig.modulePathMap = { - "entry" : "/testProjectRootPath/entry", - "library": "/testProjectRootPath/library" + "entry": "/testProjectRootPath/entry", + "library": "/testProjectRootPath/library" } projectConfig.projectRootPath = "/testProjectRootPath"; projectConfig.packageDir = 'oh_modules'; projectConfig.harNameOhmMap = { - "@ohos/sharedLibrary": "@bundle:UtTestApplication/sharedLibrary/ets/index" + "@ohos/sharedLibrary": "@bundle:UtTestApplication/sharedLibrary/ets/index" } export default projectConfig; \ No newline at end of file