• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  ES2ABC,
22  RELEASE,
23  OH_MODULES
24} from '../../../lib/fast_build/ark_compiler/common/ark_define';
25import { TS2ABC } from '../../../lib/pre_define';
26import CommonModeMock from '../mock/class_mock/common_mode_mock';
27import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
28import {
29  ES2ABC_PATH,
30  TS2ABC_PATH
31} from '../mock/rollup_mock/path_config';
32import {
33  CMD_DEBUG_INFO,
34  NODE,
35  EXPOSE_GC,
36  DEBUG,
37  MODULES
38} from '../mock/rollup_mock/common';
39import {
40  ArkTSInternalErrorDescription,
41  ArkTSErrorDescription,
42  ErrorCode,
43} from '../../../lib/fast_build/ark_compiler/error_code';
44import {
45  CommonLogger,
46  LogData,
47  LogDataFactory
48} from '../../../lib/fast_build/ark_compiler/logger';
49
50mocha.describe('test common_mode file api', function () {
51  mocha.before(function () {
52    this.rollup = new RollUpPluginMock();
53  });
54
55  mocha.after(() => {
56    delete this.rollup;
57  });
58
59  mocha.it('1-1-1: test initCmdEnv under build debug: projectConfig.pandaMode is Ts2Abc', function () {
60    this.rollup.build();
61    const modeMock = new CommonModeMock(this.rollup);
62    modeMock.projectConfig.pandaMode = TS2ABC;
63    modeMock.projectConfig.packageDir = OH_MODULES;
64    const args: string[] = modeMock.checkInitCmdEnv();
65    expect(args.length === 5).to.be.true;
66    expect(args[0] === NODE).to.be.true;
67    expect(args[1].indexOf(EXPOSE_GC) > 0).to.be.true;
68    expect(args[2].indexOf(TS2ABC_PATH) > 0).to.be.true;
69    expect(args[3].indexOf(DEBUG) > 0).to.be.true;
70    expect(args[4].indexOf(MODULES) > 0).to.be.true;
71  });
72
73  mocha.it('1-1-2: test initCmdEnv under build debug: projectConfig.pandaMode is Es2Abc', function () {
74    this.rollup.build();
75    const modeMock = new CommonModeMock(this.rollup);
76    const args: string[] = modeMock.checkInitCmdEnv();
77    expect(args.length === 2).to.be.true;
78    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
79    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
80  });
81
82  mocha.it('1-1-3: test initCmdEnv under build debug: projectConfig.pandaMode is invalid value', function () {
83    this.rollup.build();
84    const errInfo: LogData = LogDataFactory.newInstance(
85      ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE,
86      ArkTSInternalErrorDescription,
87      'Invalid compilation mode. ' +
88      `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.`
89    );
90    const modeMock = new CommonModeMock(this.rollup);
91    const stub = sinon.stub(modeMock.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit');
92    modeMock.projectConfig.pandaMode = 'invalid value';
93    try {
94      modeMock.checkInitCmdEnv();
95    } catch (e) {
96    }
97    expect(stub.calledWith(errInfo)).to.be.true;
98    stub.restore();
99  });
100
101  mocha.it('1-1-4: test initCmdEnv under build debug: projectConfig.pandaMode is invalid value ' +
102    'without getHvigorConsoleLogger', function () {
103    this.rollup.build();
104    const errInfo: LogData = LogDataFactory.newInstance(
105      ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE,
106      ArkTSInternalErrorDescription,
107      'Invalid compilation mode. ' +
108      `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.`
109    );
110    CommonLogger.destroyInstance();
111    const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger;
112    this.rollup.share.getHvigorConsoleLogger = undefined;
113    const modeMock = new CommonModeMock(this.rollup);
114    const stub = sinon.stub(modeMock.logger, 'throwArkTsCompilerError');
115    modeMock.projectConfig.pandaMode = 'invalid value';
116    try {
117      modeMock.checkInitCmdEnv();
118    } catch (e) {
119    }
120    expect(stub.calledWith(errInfo.toString())).to.be.true;
121    CommonLogger.destroyInstance();
122    this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger;
123    stub.restore();
124  });
125
126  mocha.it('1-2: test initCmdEnv under build release', function () {
127    this.rollup.build(RELEASE);
128    const modeMock = new CommonModeMock(this.rollup);
129    const args: string[] = modeMock.checkInitCmdEnv();
130    expect(args.length === 1).to.be.true;
131    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
132    expect(args[0] === CMD_DEBUG_INFO).to.be.false;
133  });
134
135  mocha.it('1-3: test initCmdEnv under preview debug', function () {
136    this.rollup.preview();
137    const modeMock = new CommonModeMock(this.rollup);
138    const args: string[] = modeMock.checkInitCmdEnv();
139    expect(args.length === 2).to.be.true;
140    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
141    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
142  });
143
144  mocha.it('1-4: test initCmdEnv under hot reload debug', function () {
145    this.rollup.hotReload();
146    const modeMock = new CommonModeMock(this.rollup);
147    const args: string[] = modeMock.checkInitCmdEnv();
148    expect(args.length === 2).to.be.true;
149    expect(args[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
150    expect(args[1] === CMD_DEBUG_INFO).to.be.true;
151  });
152});