• 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";
21
22import {
23  ENTRYABILITY_TS_PATH_DEFAULT,
24  ENTRYABILITY_JS_PATH_DEFAULT,
25  INDEX_ETS_PATH_DEFAULT
26} from '../mock/rollup_mock/common';
27import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
28import { ModuleHotreloadMode } from '../../../lib/fast_build/ark_compiler/module/module_hotreload_mode';
29import { newSourceMaps } from '../../../lib/fast_build/ark_compiler/transform';
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';
49
50mocha.describe('test module_hotreload_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: test updateSourceMapFromFileList under hot reload debug', function () {
60    this.rollup.hotReload();
61    this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS;
62    const moduleMode = new ModuleHotreloadMode(this.rollup);
63    const fileList = this.rollup.getModuleIds();
64    for (const filePath of fileList) {
65      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS)) {
66        const sourceMap: Map<string, string[]> = new Map<string, string[]>();
67        const relativeSourceFilePath =
68          toUnixPath(filePath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
69        sourceMap[FILE] = path.basename(relativeSourceFilePath);
70        sourceMap[SOURCE] = [relativeSourceFilePath];
71        newSourceMaps[relativeSourceFilePath] = sourceMap;
72      }
73    }
74    const fileListArray: Array<string> = [ENTRYABILITY_TS_PATH, INDEX_ETS_PATH];
75    moduleMode.updateSourceMapFromFileList(fileListArray);
76    const sourceMapFilePath: string = path.join(this.rollup.share.projectConfig.patchAbcPath, SOURCEMAPS);
77    if (sourceMapFilePath && fs.existsSync(sourceMapFilePath)) {
78      const testObject = fs.readFileSync(sourceMapFilePath).toString();
79      expect(testObject.indexOf(ENTRYABILITY_TS_PATH_DEFAULT) > 0 ||
80        testObject.indexOf(ENTRYABILITY_JS_PATH_DEFAULT) > 0 ||
81        testObject.indexOf(INDEX_ETS_PATH_DEFAULT) > 0).to.be.true;
82    }
83
84    for (const key of Object.keys(newSourceMaps)) {
85      delete newSourceMaps[key];
86    }
87  });
88
89  mocha.it('2-1: test addHotReloadArgs under hot reload debug', function () {
90    this.rollup.hotReload();
91    this.rollup.share.projectConfig.oldMapFilePath = DEFAULT_ETS;
92    const moduleMode = new ModuleHotreloadMode(this.rollup);
93    moduleMode.addHotReloadArgs();
94    expect(moduleMode.cmdArgs[0].indexOf(ES2ABC_PATH) > 0).to.be.true;
95    expect(moduleMode.cmdArgs[1] === DEBUG_INFO).to.be.true;
96    expect(moduleMode.cmdArgs[2] === SIMBOL_TABLE).to.be.true;
97    expect(moduleMode.cmdArgs[3].indexOf(SYMBOLMAP_MAP) > 0).to.be.true;
98  });
99});