1/* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use rollupObject file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17import { expect } from 'chai'; 18import mocha from 'mocha'; 19import sinon from 'sinon'; 20 21import { 22 ES2ABC, 23 TS2ABC 24} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 25import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 26import { ModulePreviewMode } from '../../../lib/fast_build/ark_compiler/module/module_preview_mode'; 27import { 28 ArkTSInternalErrorDescription, 29 ErrorCode 30} from '../../../lib/fast_build/ark_compiler/error_code'; 31import { 32 CommonLogger, 33 LogData, 34 LogDataFactory 35} from '../../../lib/fast_build/ark_compiler/logger'; 36 37mocha.describe('test module_preview_mode file api', function () { 38 mocha.before(function () { 39 this.rollup = new RollUpPluginMock(); 40 }); 41 42 mocha.after(() => { 43 delete this.rollup; 44 }); 45 46 mocha.it('1-1: test the error message of ModulePreviewMode executeArkCompiler', function () { 47 this.rollup.preview(); 48 const errInfo: LogData = LogDataFactory.newInstance( 49 ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE, 50 ArkTSInternalErrorDescription, 51 'Invalid compilation mode. ' + 52 `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.` 53 ); 54 const modulePreviewMode = new ModulePreviewMode(this.rollup); 55 modulePreviewMode.projectConfig.pandaMode = 'invalid value' 56 const stub = sinon.stub(modulePreviewMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 57 modulePreviewMode.executeArkCompiler(); 58 expect(stub.calledWith(errInfo)).to.be.true; 59 stub.restore(); 60 }); 61 62 mocha.it('1-2: test the error message of ModulePreviewMode executeArkCompiler ' + 63 'without getHvigorConsoleLogger', function () { 64 this.rollup.preview(); 65 const errInfo: LogData = LogDataFactory.newInstance( 66 ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE, 67 ArkTSInternalErrorDescription, 68 'Invalid compilation mode. ' + 69 `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.` 70 ); 71 CommonLogger.destroyInstance(); 72 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 73 this.rollup.share.getHvigorConsoleLogger = undefined; 74 const modulePreviewMode = new ModulePreviewMode(this.rollup); 75 modulePreviewMode.projectConfig.pandaMode = 'invalid value' 76 const stub = sinon.stub(modulePreviewMode.logger, 'throwArkTsCompilerError'); 77 modulePreviewMode.executeArkCompiler(); 78 expect(stub.calledWith(errInfo.toString())).to.be.true; 79 CommonLogger.destroyInstance(); 80 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 81 stub.restore(); 82 }); 83});