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