• 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
16
17import { expect } from 'chai';
18import mocha from 'mocha';
19import path from "path";
20import fs from "fs";
21import sinon from 'sinon';
22
23import {
24  ENTRYABILITY_TS_PATH_DEFAULT,
25  ENTRYABILITY_JS_PATH_DEFAULT,
26  INDEX_ETS_PATH_DEFAULT
27} from '../mock/rollup_mock/common';
28import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
29import { ModuleHotreloadMode } from '../../../lib/fast_build/ark_compiler/module/module_hotreload_mode';
30import { toUnixPath } from '../../../lib/utils';
31import {
32  SOURCEMAPS,
33  EXTNAME_TS,
34  EXTNAME_ETS
35} from '../../../lib/fast_build/ark_compiler/common/ark_define';
36import {
37  ES2ABC_PATH,
38  SYMBOLMAP_MAP,
39  DEFAULT_ETS,
40  DEBUG_INFO,
41  SIMBOL_TABLE
42} from '../mock/rollup_mock/path_config';
43import {
44  ENTRYABILITY_TS_PATH,
45  INDEX_ETS_PATH,
46  FILE,
47  SOURCE
48} from '../mock/rollup_mock/common';
49import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap';
50import {
51  ArkTSInternalErrorDescription,
52  ErrorCode
53} from '../../../lib/fast_build/ark_compiler/error_code';
54import {
55  CommonLogger,
56  LogData,
57  LogDataFactory
58} from '../../../lib/fast_build/ark_compiler/logger';
59
60mocha.describe('test module_hotreload_mode file api', function () {
61  mocha.before(function () {
62    this.rollup = new RollUpPluginMock();
63  });
64
65  mocha.after(() => {
66    delete this.rollup;
67  });
68
69  mocha.it('1-1: test updateSourceMapFromFileList under hot reload debug', function () {
70    this.rollup.hotReload();
71    this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS;
72    const moduleMode = new ModuleHotreloadMode(this.rollup);
73    const fileList = this.rollup.getModuleIds();
74    const sourceMapGenerator: SourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
75
76    for (const filePath of fileList) {
77      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS)) {
78        const sourceMap: Map<string, string[]> = new Map<string, string[]>();
79        const relativeSourceFilePath =
80          toUnixPath(filePath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
81        sourceMap[FILE] = path.basename(relativeSourceFilePath);
82        sourceMap[SOURCE] = [relativeSourceFilePath];
83        sourceMapGenerator.updateSourceMap(filePath, sourceMap);
84      }
85    }
86    const fileListArray: Array<string> = [
87      path.join(this.rollup.share.projectConfig.modulePath, ENTRYABILITY_TS_PATH_DEFAULT),
88      path.join(this.rollup.share.projectConfig.modulePath, INDEX_ETS_PATH_DEFAULT)
89    ];
90    moduleMode.updateSourceMapFromFileList(fileListArray);
91    const sourceMapFilePath: string = path.join(this.rollup.share.projectConfig.patchAbcPath, SOURCEMAPS);
92    if (sourceMapFilePath && fs.existsSync(sourceMapFilePath)) {
93      const testObject = fs.readFileSync(sourceMapFilePath).toString();
94      expect(testObject.indexOf(ENTRYABILITY_TS_PATH_DEFAULT) > 0 ||
95        testObject.indexOf(ENTRYABILITY_JS_PATH_DEFAULT) > 0 ||
96        testObject.indexOf(INDEX_ETS_PATH_DEFAULT) > 0).to.be.true;
97    }
98
99    let newSourceMaps = sourceMapGenerator.getSourceMaps();
100    for (const key of Object.keys(newSourceMaps)) {
101      delete newSourceMaps[key];
102    }
103    SourceMapGenerator.cleanSourceMapObject();
104  });
105
106  mocha.it('2-1: test addHotReloadArgs under hot reload debug', function () {
107    this.rollup.hotReload();
108    this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS;
109    const moduleMode = new ModuleHotreloadMode(this.rollup);
110    moduleMode.addHotReloadArgs();
111    expect(moduleMode.cmdArgs[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
112    expect(moduleMode.cmdArgs[1] === DEBUG_INFO).to.be.true;
113    expect(moduleMode.cmdArgs[2] === SIMBOL_TABLE).to.be.true;
114    expect(moduleMode.cmdArgs[3].indexOf(SYMBOLMAP_MAP) > 0).to.be.true;
115  });
116
117  mocha.it('3-1: test the error message of the ModuleHotreloadMode constructor', function () {
118    this.rollup.hotReload();
119    this.rollup.share.projectConfig.oldMapFilePath = '';
120    const errInfo: LogData = LogDataFactory.newInstance(
121      ErrorCode.ETS2BUNDLE_INTERNAL_HOT_RELOAD_FAILED_INCORRECT_SYMBOL_MAP_CONFIG,
122      ArkTSInternalErrorDescription,
123      'Hot Reload failed, symbolMap file is not correctly configured.'
124    );
125    const stub = sinon.stub(CommonLogger.getInstance(this.rollup).getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit');
126    try {
127      new ModuleHotreloadMode(this.rollup);
128    } catch (e) {
129    }
130    expect(stub.calledWith(errInfo)).to.be.true;
131    stub.restore();
132  });
133
134  mocha.it('3-2: test the error message of the ModuleHotreloadMode constructor ' +
135    'without getHvigorConsoleLogger', function () {
136    this.rollup.hotReload();
137    this.rollup.share.projectConfig.oldMapFilePath = '';
138    const errInfo: LogData = LogDataFactory.newInstance(
139      ErrorCode.ETS2BUNDLE_INTERNAL_HOT_RELOAD_FAILED_INCORRECT_SYMBOL_MAP_CONFIG,
140      ArkTSInternalErrorDescription,
141      'Hot Reload failed, symbolMap file is not correctly configured.'
142    );
143    CommonLogger.destroyInstance();
144    const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger;
145    this.rollup.share.getHvigorConsoleLogger = undefined;
146    const stub = sinon.stub(CommonLogger.getInstance(this.rollup), 'throwArkTsCompilerError');
147    try {
148      new ModuleHotreloadMode(this.rollup);
149    } catch (e) {
150    }
151    expect(stub.calledWith(errInfo.toString())).to.be.true;
152    CommonLogger.destroyInstance();
153    this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger;
154    stub.restore();
155  });
156});