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 fs from "fs";
19import path from "path";
20import MagicString from 'magic-string';
21import sinon from 'sinon';
22
23import {
24  needAotCompiler,
25  shouldETSOrTSFileTransformToJS,
26  changeFileExtension,
27  isAotMode,
28  isDebug,
29  isCommonJsPluginVirtualFile,
30  isCurrentProjectFiles,
31  isSpecifiedExt,
32  isTsOrEtsSourceFile,
33  isJsSourceFile,
34  isJsonSourceFile,
35  utUtils,
36  updateSourceMap,
37  hasTsNoCheckOrTsIgnoreFiles,
38  compilingEtsOrTsFiles,
39  cleanUpFilesList
40} from '../../../lib/fast_build/ark_compiler/utils';
41import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock';
42import { ModuleInfo } from '../mock/rollup_mock/module_info';
43import {
44  AOT_FULL,
45  AOT_PARTIAL,
46  AOT_TYPE
47} from '../../../lib/pre_define';
48import {
49  ESMODULE,
50  JSBUNDLE,
51  EXTNAME_JS,
52  EXTNAME_TS,
53  EXTNAME_ETS,
54  EXTNAME_JSON,
55  RELEASE,
56  DEBUG,
57  GEN_ABC_PLUGIN_NAME
58} from '../../../lib/fast_build/ark_compiler/common/ark_define';
59import ModuleSourceFileMock from '../mock/class_mock/module_source_files_mock';
60import {
61  genTemporaryPath,
62  toUnixPath,
63  getProjectRootPath,
64  getBelongModuleInfo
65} from '../../../lib/utils';
66import projectConfig from '../utils/processProjectConfig';
67import {
68  TEST_TS,
69  TEST_JS,
70  TEST_ETS,
71  TEST_JSON,
72  PROJECT_ROOT,
73  DEFAULT_PROJECT
74} from '../mock/rollup_mock/path_config';
75import { scanFiles } from "../utils/utils";
76import { SourceMapGenerator } from '../../../lib/fast_build/ark_compiler/generate_sourcemap';
77import { ModuleSourceFile } from '../../../lib/fast_build/ark_compiler/module/module_source_file';
78import {
79  ENTRY_PACKAGE_INFO,
80  FILE,
81  SOURCE,
82  DYNAMICIMPORT_ETS,
83  UPDATESOURCEMAP
84} from '../mock/rollup_mock/common';
85import {
86  moduleResolutionHostTest
87} from '../../../lib/ets_checker';
88import {
89  getRollupCache,
90  setRollupCache
91} from '../../../lib/utils';
92import {
93  ArkObfuscator
94} from 'arkguard';
95
96mocha.describe('test utils file api', function () {
97  mocha.before(function () {
98    this.rollup = new RollUpPluginMock();
99  });
100
101  mocha.after(() => {
102    delete this.rollup;
103  });
104
105  mocha.it('1-1: test needAotCompiler under build debug', function () {
106    this.rollup.build();
107    const returnInfo = needAotCompiler(this.rollup.share.projectConfig);
108    expect(returnInfo).to.be.false;
109  });
110
111  mocha.it('1-2: test needAotCompiler under build debug and anBuildMode is AOT_PARTIAL', function () {
112    this.rollup.build();
113    this.rollup.share.projectConfig.compileMode = ESMODULE;
114    this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL;
115    const returnInfo = needAotCompiler(this.rollup.share.projectConfig);
116    expect(returnInfo).to.be.true;
117  });
118
119  mocha.it('1-3: test needAotCompiler under build release', function () {
120    this.rollup.build(RELEASE);
121    this.rollup.share.projectConfig.compileMode = ESMODULE;
122    this.rollup.share.projectConfig.anBuildMode = AOT_FULL;
123    const returnInfo = needAotCompiler(this.rollup.share.projectConfig);
124    expect(returnInfo).to.be.true;
125  });
126
127  mocha.it('1-4: test needAotCompiler under build release and compileMode is JSBUNDLE', function () {
128    this.rollup.build(RELEASE);
129    this.rollup.share.projectConfig.compileMode = JSBUNDLE;
130    this.rollup.share.projectConfig.anBuildMode = AOT_FULL;
131    const returnInfo = needAotCompiler(this.rollup.share.projectConfig);
132    expect(returnInfo).to.be.false;
133  });
134
135  mocha.it('1-5: test needAotCompiler under preview debug', function () {
136    this.rollup.preview();
137    this.rollup.share.projectConfig.compileMode = JSBUNDLE;
138    this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL;
139    const buildModeIsAotFull = needAotCompiler(this.rollup.share.projectConfig);
140    expect(buildModeIsAotFull === false).to.be.true;
141  });
142
143  mocha.it('1-6: test needAotCompiler under preview debug and anBuildMode is AOT_TYPE', function () {
144    this.rollup.preview();
145    this.rollup.share.projectConfig.compileMode = JSBUNDLE;
146    this.rollup.share.projectConfig.anBuildMode = AOT_TYPE;
147    const buildModeIsAotFull = needAotCompiler(this.rollup.share.projectConfig);
148    expect(buildModeIsAotFull === false).to.be.true;
149  });
150
151  mocha.it('1-7: test needAotCompiler under hot reload debug', function () {
152    this.rollup.hotReload();
153    this.rollup.share.projectConfig.compileMode = ESMODULE;
154    this.rollup.share.projectConfig.anBuildMode = AOT_TYPE;
155    const buildModeIsAotType = needAotCompiler(this.rollup.share.projectConfig);
156    expect(buildModeIsAotType === false).to.be.true;
157  });
158
159  mocha.it('2-1-1: test shouldETSOrTSFileTransformToJS under build debug: arkProjectConfig.processTs is false', function () {
160    this.rollup.build();
161    this.mockfileList = this.rollup.getModuleIds();
162    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
163    for (const filePath of this.mockfileList) {
164      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
165        expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true;
166      }
167    }
168  });
169
170  mocha.it('2-1-2: test shouldETSOrTSFileTransformToJS under build debug: arkProjectConfig.processTs is true', function () {
171    this.rollup.build();
172    this.mockfileList = this.rollup.getModuleIds();
173    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
174    this.rollup.share.arkProjectConfig.processTs = true;
175    for (const filePath of this.mockfileList) {
176      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
177        compilingEtsOrTsFiles.push(filePath);
178        hasTsNoCheckOrTsIgnoreFiles.push(filePath);
179        expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true;
180      }
181    }
182  });
183
184  mocha.it('2-2: test shouldETSOrTSFileTransformToJS under build release', function () {
185    this.rollup.build(RELEASE);
186    this.mockfileList = this.rollup.getModuleIds();
187    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
188    for (const filePath of this.mockfileList) {
189      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
190        expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true;
191      }
192    }
193  });
194
195  mocha.it('2-3: test shouldETSOrTSFileTransformToJS under preview debug', function () {
196    this.rollup.preview();
197    this.mockfileList = this.rollup.getModuleIds();
198    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
199    for (const filePath of this.mockfileList) {
200      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
201        expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true;
202      }
203    }
204  });
205
206  mocha.it('2-4: test shouldETSOrTSFileTransformToJS under hot reload debug', function () {
207    this.rollup.hotReload();
208    this.mockfileList = this.rollup.getModuleIds();
209    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
210    for (const filePath of this.mockfileList) {
211      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
212        expect(shouldETSOrTSFileTransformToJS(filePath, this.rollup.share.arkProjectConfig) === true).to.be.true;
213      }
214    }
215  });
216
217  mocha.it('2-5: test shouldETSOrTSFileTransformToJS under build release and enable obuscate file name', function () {
218    this.rollup.build(RELEASE);
219    this.mockfileList = this.rollup.getModuleIds();
220    this.rollup.share.arkProjectConfig.cachePath = this.rollup.share.projectConfig.cachePath;
221
222    this.rollup.share.projectConfig = {
223      buildMode: 'Release'
224    }
225    this.rollup.share.projectConfig.obfuscationMergedObConfig = {
226      options: {
227        enableFileNameObfuscation: true
228      }
229    };
230    this.rollup.share.arkProjectConfig.processTs = true;
231    const projectConfig: Object = Object.assign(this.rollup.share.arkProjectConfig, this.rollup.share.projectConfig);
232
233    const currentDir = path.dirname(__dirname);
234    const testMainDir = path.join(currentDir, "../../test/ark_compiler_ut/testdata/testcase_def/entry/build/entry/src/main");
235    let reservedFileNamesDirectories = testMainDir.split("/").filter(directory => directory !== "");
236    reservedFileNamesDirectories = reservedFileNamesDirectories.concat(["pages", "entryability"]);
237
238    const arkguardConfig = {
239      mRenameFileName: {
240        mEnable: true,
241        mNameGeneratorType: 1,
242        mReservedFileNames: reservedFileNamesDirectories
243      },
244      mPerformancePrinter: []
245    }
246
247    let arkObfuscator: ArkObfuscator = new ArkObfuscator();
248    arkObfuscator.init(arkguardConfig);
249
250    let index = 0
251    for (const filePath of this.mockfileList) {
252      cleanUpFilesList();
253      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
254        if (index == 0) {
255          const directory = path.dirname(filePath);
256          const tempPath = path.join(directory, "../../../build/entry/src/main/entryability")
257          const bFilePath = path.join(tempPath, 'b.js');
258          fs.writeFileSync(bFilePath, 'console.log("b.js created");');
259          expect(bFilePath !== filePath).to.be.true;
260          expect(shouldETSOrTSFileTransformToJS(filePath, projectConfig) === true).to.be.true;
261          fs.unlinkSync(bFilePath);
262        }
263      }
264      index++;
265    }
266  });
267
268  mocha.it('3-1: test writeFileContent under build debug', function () {
269    this.rollup.build();
270    const mockFileList: object = this.rollup.getModuleIds();
271    for (const moduleId of mockFileList) {
272      if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) {
273        const code: string = fs.readFileSync(moduleId, 'utf-8');
274        const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta;
275        const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo);
276        moduleSource.initPluginEnvMock(this.rollup);
277        const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath,
278          moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo);
279        utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source,
280          moduleSource.projectConfig, moduleSource.logger);
281        const newFilePath = changeFileExtension(filePath, EXTNAME_JS);
282        const readFilecontent = fs.readFileSync(newFilePath, 'utf-8');
283        expect(readFilecontent === moduleSource.source).to.be.true;
284      }
285    }
286  });
287
288  mocha.it('3-2: test writeFileContent under build release', function () {
289    this.rollup.build(RELEASE);
290    SourceMapGenerator.initInstance(this.rollup);
291    const mockFileList: object = this.rollup.getModuleIds();
292    for (const moduleId of mockFileList) {
293      if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) {
294        const code: string = fs.readFileSync(moduleId, 'utf-8');
295        const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta;
296        const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo);
297        moduleSource.initPluginEnvMock(this.rollup);
298        const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath,
299          moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo);
300        utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source,
301          moduleSource.projectConfig, moduleSource.logger);
302        const newFilePath = changeFileExtension(filePath, EXTNAME_JS);
303        const readFilecontent = fs.readFileSync(newFilePath, 'utf-8');
304        expect(readFilecontent === moduleSource.source).to.be.true;
305      }
306    }
307    SourceMapGenerator.cleanSourceMapObject();
308  });
309
310  mocha.it('3-3: test writeFileContent under preview debug', function () {
311    this.rollup.preview();
312    const mockFileList: object = this.rollup.getModuleIds();
313    for (const moduleId of mockFileList) {
314      if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) {
315        const code: string = fs.readFileSync(moduleId, 'utf-8');
316        const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta;
317        const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo);
318        moduleSource.initPluginEnvMock(this.rollup);
319        const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath,
320          moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo);
321        utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source,
322          moduleSource.projectConfig, moduleSource.logger);
323        const newFilePath = changeFileExtension(filePath, EXTNAME_JS);
324        const readFilecontent = fs.readFileSync(newFilePath, 'utf-8');
325        expect(readFilecontent === moduleSource.source).to.be.true;
326      }
327    }
328  });
329
330  mocha.it('3-4: test writeFileContent under hot reload debug', function () {
331    this.rollup.hotReload();
332    const mockFileList: object = this.rollup.getModuleIds();
333    for (const moduleId of mockFileList) {
334      if (moduleId.endsWith(EXTNAME_TS) || moduleId.endsWith(EXTNAME_ETS) || moduleId.endsWith(EXTNAME_JS)) {
335        const code: string = fs.readFileSync(moduleId, 'utf-8');
336        const metaInfo: Object = this.rollup.getModuleInfo(moduleId).meta;
337        const moduleSource = new ModuleSourceFileMock(moduleId, code, metaInfo);
338        moduleSource.initPluginEnvMock(this.rollup);
339        const filePath = genTemporaryPath(moduleSource.moduleId, moduleSource.projectConfig.projectPath,
340          moduleSource.projectConfig.cachePath, moduleSource.projectConfig, moduleSource.metaInfo);
341        utUtils.writeFileContent(moduleSource.moduleId, filePath, moduleSource.source,
342          moduleSource.projectConfig, moduleSource.logger);
343        const newFilePath = changeFileExtension(filePath, EXTNAME_JS);
344        const readFilecontent = fs.readFileSync(newFilePath, 'utf-8');
345        expect(readFilecontent === moduleSource.source).to.be.true;
346      }
347    }
348  });
349
350  mocha.it('4-1-1: test updateSourceMap under build debug: originMap is null', async function () {
351    this.rollup.build();
352    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
353    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
354      this.rollup.share.projectConfig.entryModuleName,
355      this.rollup.share.projectConfig.modulePath)
356    );
357    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
358
359    const relativeSourceFilePath =
360      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
361    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
362    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
363    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
364    const updatedMap: object = sourceCode.generateMap({
365      source: relativeSourceFilePath,
366      file: `${path.basename(moduleSource.moduleId)}`,
367      includeContent: false,
368      hires: true
369    });
370
371    updatedMap[SOURCE] = [relativeSourceFilePath];
372    updatedMap[FILE] = path.basename(relativeSourceFilePath);
373    updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0';
374
375    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap));
376    expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true;
377    // pop dynamicImportpath moduleInfo
378    this.rollup.moduleInfos.pop();
379    SourceMapGenerator.cleanSourceMapObject();
380  });
381
382  mocha.it('4-1-2: test updateSourceMap under build debug: newMap is null', async function () {
383    this.rollup.build();
384    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
385    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
386      this.rollup.share.projectConfig.entryModuleName,
387      this.rollup.share.projectConfig.modulePath)
388    );
389    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
390
391    const relativeSourceFilePath =
392      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
393    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
394    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
395    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
396    const updatedMap: object = sourceCode.generateMap({
397      source: relativeSourceFilePath,
398      file: `${path.basename(moduleSource.moduleId)}`,
399      includeContent: false,
400      hires: true
401    });
402
403    updatedMap[SOURCE] = [relativeSourceFilePath];
404    updatedMap[FILE] = path.basename(relativeSourceFilePath);
405    updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0';
406
407    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(updatedMap));
408    expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true;
409    // pop dynamicImportpath moduleInfo
410    this.rollup.moduleInfos.pop();
411    SourceMapGenerator.cleanSourceMapObject();
412  });
413
414  mocha.it('4-1-3: test updateSourceMap under build debug: originMap and newMap is not null', async function () {
415    this.rollup.build();
416    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
417    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
418      this.rollup.share.projectConfig.entryModuleName,
419      this.rollup.share.projectConfig.modulePath)
420    );
421    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
422
423    const relativeSourceFilePath =
424      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
425    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
426    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
427    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
428    const updatedMap: object = sourceCode.generateMap({
429      source: relativeSourceFilePath,
430      file: `${path.basename(moduleSource.moduleId)}`,
431      includeContent: false,
432      hires: true
433    });
434    const arraylist = Array.from(this.rollup.share.allFiles);
435    const syncCode: string = fs.readFileSync(arraylist[2].toString(), 'utf-8');
436    const dynamicModuleSource = new ModuleSourceFile(dynamicImportpath, syncCode);
437    const codeString: MagicString = new MagicString(<string>dynamicModuleSource.source);
438    const sourceMap: object = codeString.generateMap({
439      source: relativeSourceFilePath,
440      file: `${path.basename(dynamicModuleSource.moduleId)}`,
441      includeContent: false,
442      hires: true
443    });
444
445    delete sourceMap.sourcesContent;
446    updatedMap[SOURCE] = [relativeSourceFilePath];
447    updatedMap[FILE] = path.basename(relativeSourceFilePath);
448
449    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(sourceMap, updatedMap));
450    const readSourceMap =
451      JSON.parse(fs.readFileSync(`${this.rollup.share.projectConfig.projectTopDir}/${UPDATESOURCEMAP}`, 'utf-8'));
452    expect(sourceMapGenerator.getSourceMap(dynamicImportpath).file === DYNAMICIMPORT_ETS).to.be.true;
453    expect(sourceMapGenerator.getSourceMap(dynamicImportpath).mappings === readSourceMap.mappings).to.be.true;
454    // pop dynamicImportpath moduleInfo
455    this.rollup.moduleInfos.pop();
456    SourceMapGenerator.cleanSourceMapObject();
457  });
458
459  mocha.it('4-2: test updateSourceMap under build release', async function () {
460    this.rollup.build(RELEASE);
461    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
462    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
463      this.rollup.share.projectConfig.entryModuleName,
464      this.rollup.share.projectConfig.modulePath)
465    );
466    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
467
468    const relativeSourceFilePath =
469      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
470    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
471    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
472    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
473    const updatedMap: object = sourceCode.generateMap({
474      source: relativeSourceFilePath,
475      file: `${path.basename(moduleSource.moduleId)}`,
476      includeContent: false,
477      hires: true
478    });
479
480    updatedMap[SOURCE] = [relativeSourceFilePath];
481    updatedMap[FILE] = path.basename(relativeSourceFilePath);
482    updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0';
483
484    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap));
485    expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true;
486    // pop dynamicImportpath moduleInfo
487    this.rollup.moduleInfos.pop();
488    SourceMapGenerator.cleanSourceMapObject();
489  });
490
491  mocha.it('4-3: test updateSourceMap under preview debug', async function () {
492    this.rollup.preview();
493    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
494    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
495      this.rollup.share.projectConfig.entryModuleName,
496      this.rollup.share.projectConfig.modulePath)
497    );
498    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
499
500    const relativeSourceFilePath =
501      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
502    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
503    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
504    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
505    const updatedMap: object = sourceCode.generateMap({
506      source: relativeSourceFilePath,
507      file: `${path.basename(moduleSource.moduleId)}`,
508      includeContent: false,
509      hires: true
510    });
511
512    updatedMap[SOURCE] = [relativeSourceFilePath];
513    updatedMap[FILE] = path.basename(relativeSourceFilePath);
514    updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0';
515
516    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap));
517    expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true;
518    // pop dynamicImportpath moduleInfo
519    this.rollup.moduleInfos.pop();
520    SourceMapGenerator.cleanSourceMapObject();
521  });
522
523  mocha.it('4-4: test updateSourceMap under hot reload debug', async function () {
524    this.rollup.hotReload();
525    const dynamicImportpath = this.rollup.share.projectConfig.DynamicImportpath;
526    this.rollup.moduleInfos.push(new ModuleInfo(dynamicImportpath,
527      this.rollup.share.projectConfig.entryModuleName,
528      this.rollup.share.projectConfig.modulePath)
529    );
530    const sourceMapGenerator = SourceMapGenerator.initInstance(this.rollup);
531
532    const relativeSourceFilePath =
533      toUnixPath(dynamicImportpath.replace(this.rollup.share.projectConfig.projectTopDir + path.sep, ''));
534    const code: string = fs.readFileSync(dynamicImportpath, 'utf-8');
535    const moduleSource = new ModuleSourceFile(dynamicImportpath, code);
536    const sourceCode: MagicString = new MagicString(<string>moduleSource.source);
537    const updatedMap: object = sourceCode.generateMap({
538      source: relativeSourceFilePath,
539      file: `${path.basename(moduleSource.moduleId)}`,
540      includeContent: false,
541      hires: true
542    });
543
544    updatedMap[SOURCE] = [relativeSourceFilePath];
545    updatedMap[FILE] = path.basename(relativeSourceFilePath);
546    updatedMap[ENTRY_PACKAGE_INFO] = 'entry|1.0.0';
547
548    sourceMapGenerator.updateSourceMap(dynamicImportpath, await updateSourceMap(undefined, updatedMap));
549    expect(sourceMapGenerator.getSourceMap(dynamicImportpath) === updatedMap).to.be.true;
550    // pop dynamicImportpath moduleInfo
551    this.rollup.moduleInfos.pop();
552    SourceMapGenerator.cleanSourceMapObject();
553  });
554
555  mocha.it('5-1: test isAotMode under build debug', function () {
556    this.rollup.build();
557    const returnInfo = isAotMode(this.rollup.share.projectConfig);
558    expect(returnInfo).to.be.false;
559  });
560
561  mocha.it('5-2: test isAotMode under build release', function () {
562    this.rollup.build(RELEASE);
563    this.rollup.share.projectConfig.compileMode = ESMODULE;
564    this.rollup.share.projectConfig.anBuildMode = AOT_FULL;
565    const returnInfo = isAotMode(this.rollup.share.projectConfig);
566    expect(returnInfo).to.be.true;
567  });
568
569  mocha.it('5-3: test isAotMode under preview debug', function () {
570    this.rollup.preview();
571    this.rollup.share.projectConfig.compileMode = JSBUNDLE;
572    this.rollup.share.projectConfig.anBuildMode = AOT_PARTIAL;
573    const buildModeIsAotType = isAotMode(this.rollup.share.projectConfig);
574    expect(buildModeIsAotType).to.be.false;
575  });
576
577  mocha.it('5-4: test isAotMode under hot reload debug', function () {
578    this.rollup.hotReload();
579    this.rollup.share.projectConfig.compileMode = ESMODULE;
580    this.rollup.share.projectConfig.anBuildMode = AOT_TYPE;
581    const buildModeIsAotType = isAotMode(this.rollup.share.projectConfig);
582    expect(buildModeIsAotType).to.be.true;
583  });
584
585  mocha.it('5-5: test isAotMode under hot fix debug', function () {
586    projectConfig.buildMode = DEBUG;
587    projectConfig.compileMode = JSBUNDLE;
588    projectConfig.anBuildMode = AOT_TYPE;
589    const buildModeIsAotType = isAotMode(projectConfig);
590    expect(buildModeIsAotType).to.be.false;
591  });
592
593  mocha.it('5-6: test isAotMode under hot fix release', function () {
594    projectConfig.buildMode = RELEASE;
595    projectConfig.compileMode = ESMODULE;
596    projectConfig.anBuildMode = AOT_PARTIAL;
597    const buildModeIsAotType = isAotMode(projectConfig);
598    expect(buildModeIsAotType).to.be.true;
599  });
600
601  mocha.it('6-1: test isDebug under build debug', function () {
602    this.rollup.build();
603    const returnInfo = isDebug(this.rollup.share.projectConfig);
604    expect(returnInfo).to.be.true;
605  });
606
607  mocha.it('6-2: test isDebug under build release', function () {
608    this.rollup.build(RELEASE);
609    const returnInfo = isDebug(this.rollup.share.projectConfig);
610    expect(returnInfo).to.be.false;
611  });
612
613  mocha.it('6-3: test isDebug under preview debug', function () {
614    this.rollup.preview();
615    const returnInfo = isDebug(this.rollup.share.projectConfig);
616    expect(returnInfo).to.be.true;
617  });
618
619  mocha.it('6-4: test isDebug under hot reload debug', function () {
620    this.rollup.hotReload();
621    const returnInfo = isDebug(this.rollup.share.projectConfig);
622    expect(returnInfo).to.be.true;
623  });
624
625  mocha.it('6-5: test isDebug under hot fix debug', function () {
626    projectConfig.buildMode = DEBUG;
627    const returnInfo = isDebug(projectConfig);
628    expect(returnInfo).to.be.true;
629  });
630
631  mocha.it('6-6: test isDebug under hot fix release', function () {
632    projectConfig.buildMode = RELEASE;
633    const returnInfo = isDebug(projectConfig);
634    expect(returnInfo).to.be.false;
635  });
636
637  mocha.it('7-1: test changeFileExtension under build debug', function () {
638    this.rollup.build();
639    const targetExt = EXTNAME_TS;
640    const originExt = '';
641    const allFiles = new Set<string>();
642    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
643    this.mockfileList = allFiles.values();
644    for (const file of this.mockfileList) {
645      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
646        const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
647        const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
648        const returnInfo = changeFileExtension(file, targetExt, originExt);
649        expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
650      }
651    }
652  });
653
654  mocha.it('7-2: test changeFileExtension under build release', function () {
655    this.rollup.build(RELEASE);
656    const targetExt = EXTNAME_TS;
657    const originExt = '';
658    const allFiles = new Set<string>();
659    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
660    this.mockfileList = allFiles.values();
661    for (const file of this.mockfileList) {
662      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
663        const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
664        const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
665        const returnInfo = changeFileExtension(file, targetExt, originExt);
666        expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
667      }
668    }
669  });
670
671  mocha.it('7-3: test changeFileExtension under preview debug', function () {
672    this.rollup.preview();
673    const targetExt = EXTNAME_TS;
674    const originExt = '';
675    const allFiles = new Set<string>();
676    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
677    this.mockfileList = allFiles.values();
678    for (const file of this.mockfileList) {
679      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
680        const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
681        const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
682        const returnInfo = changeFileExtension(file, targetExt, originExt);
683        expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
684      }
685    }
686  });
687
688  mocha.it('7-4: test changeFileExtension under hot reload debug', function () {
689    this.rollup.hotReload();
690    const targetExt = EXTNAME_TS;
691    const originExt = '';
692    const allFiles = new Set<string>();
693    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
694    this.mockfileList = allFiles.values();
695    for (const file of this.mockfileList) {
696      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
697        const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
698        const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
699        const returnInfo = changeFileExtension(file, targetExt, originExt);
700        expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
701      }
702    }
703  });
704
705  mocha.it('7-5: test changeFileExtension under hot fix debug', function () {
706    projectConfig.buildMode = DEBUG;
707    const file = TEST_TS;
708    const targetExt = EXTNAME_TS;
709    const originExt = '';
710    const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
711    const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
712    const returnInfo = changeFileExtension(file, targetExt, originExt);
713    expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
714  });
715
716  mocha.it('7-6: test changeFileExtension under hot fix release', function () {
717    projectConfig.buildMode = RELEASE;
718    const file = TEST_TS;
719    const targetExt = EXTNAME_TS;
720    const originExt = '';
721    const currentExt = originExt.length === 0 ? path.extname(file) : originExt;
722    const fileWithoutExt = file.substring(0, file.lastIndexOf(currentExt));
723    const returnInfo = changeFileExtension(file, targetExt, originExt);
724    expect(returnInfo === fileWithoutExt + targetExt).to.be.true;
725  });
726
727  mocha.it('8-1: test isCommonJsPluginVirtualFile under build debug', function () {
728    this.rollup.build();
729    const allFiles = new Set<string>();
730    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
731    this.mockfileList = allFiles.values();
732    for (const filePath of this.mockfileList) {
733      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
734        const returnInfo = isCommonJsPluginVirtualFile(filePath);
735        expect(returnInfo).to.be.false;
736      }
737    }
738  });
739
740  mocha.it('8-2: test isCommonJsPluginVirtualFile under build release', function () {
741    this.rollup.build(RELEASE);
742    const allFiles = new Set<string>();
743    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
744    this.mockfileList = allFiles.values();
745    for (const filePath of this.mockfileList) {
746      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
747        const returnInfo = isCommonJsPluginVirtualFile(filePath);
748        expect(returnInfo).to.be.false;
749      }
750    }
751  });
752
753  mocha.it('8-3: test isCommonJsPluginVirtualFile under preview debug', function () {
754    this.rollup.preview();
755    const allFiles = new Set<string>();
756    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
757    this.mockfileList = allFiles.values();
758    for (const filePath of this.mockfileList) {
759      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
760        const returnInfo = isCommonJsPluginVirtualFile(filePath);
761        expect(returnInfo).to.be.false;
762      }
763    }
764  });
765
766  mocha.it('8-4: test isCommonJsPluginVirtualFile under hot reload debug', function () {
767    this.rollup.hotReload();
768    const allFiles = new Set<string>();
769    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
770    this.mockfileList = allFiles.values();
771    for (const filePath of this.mockfileList) {
772      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
773        const returnInfo = isCommonJsPluginVirtualFile(filePath);
774        expect(returnInfo).to.be.false;
775      }
776    }
777  });
778
779  mocha.it('8-5: test isCommonJsPluginVirtualFile under hot fix debug', function () {
780    projectConfig.buildMode = DEBUG;
781    const filePath = TEST_TS;
782    const returnInfo = isCommonJsPluginVirtualFile(filePath);
783    expect(returnInfo).to.be.false;
784  });
785
786  mocha.it('8-6: test isCommonJsPluginVirtualFile under hot fix release', function () {
787    projectConfig.buildMode = RELEASE;
788    const filePath = TEST_TS;
789    const returnInfo = isCommonJsPluginVirtualFile(filePath);
790    expect(returnInfo).to.be.false;
791  });
792
793  mocha.it('9-1: test isCurrentProjectFiles under build debug', function () {
794    this.rollup.build();
795    const allFiles = new Set<string>();
796    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
797    this.mockfileList = allFiles.values();
798    for (const filePath of this.mockfileList) {
799      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
800        const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig);
801        expect(returnInfo).to.be.true;
802      }
803    }
804  });
805
806  mocha.it('9-2: test isCurrentProjectFiles under build release', function () {
807    this.rollup.build(RELEASE);
808    const allFiles = new Set<string>();
809    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
810    this.mockfileList = allFiles.values();
811    for (const filePath of this.mockfileList) {
812      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
813        const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig);
814        expect(returnInfo).to.be.true;
815      }
816    }
817  });
818
819  mocha.it('9-3: test isCurrentProjectFiles under preview debug', function () {
820    this.rollup.preview();
821    const allFiles = new Set<string>();
822    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
823    this.mockfileList = allFiles.values();
824    for (const filePath of this.mockfileList) {
825      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
826        const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig);
827        expect(returnInfo).to.be.true;
828      }
829    }
830  });
831
832  mocha.it('9-4: test isCurrentProjectFiles under hot reload debug', function () {
833    this.rollup.hotReload();
834    const allFiles = new Set<string>();
835    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
836    this.mockfileList = allFiles.values();
837    for (const filePath of this.mockfileList) {
838      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
839        const returnInfo = isCurrentProjectFiles(filePath, this.rollup.share.projectConfig);
840        expect(returnInfo).to.be.true;
841      }
842    }
843  });
844
845  mocha.it('9-5: test isCurrentProjectFiles under hot fix debug', function () {
846    projectConfig.buildMode = DEBUG;
847    const filePath = TEST_TS;
848    const returnInfo = isCurrentProjectFiles(filePath, projectConfig);
849    expect(returnInfo).to.be.false;
850  });
851
852  mocha.it('9-6: test isCurrentProjectFiles under hot fix release', function () {
853    projectConfig.buildMode = RELEASE;
854    const filePath = TEST_TS;
855    const returnInfo = isCurrentProjectFiles(filePath, projectConfig);
856    expect(returnInfo).to.be.false;
857  });
858
859  mocha.it('10-1: test isSpecifiedExt under build debug', function () {
860    this.rollup.build();
861    const fileExtendName = EXTNAME_ETS;
862    const allFiles = new Set<string>();
863    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
864    this.mockfileList = allFiles.values();
865    for (const filePath of this.mockfileList) {
866      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
867        if (filePath.endsWith(EXTNAME_ETS)) {
868          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true;
869        } else {
870          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false;
871        }
872      }
873    }
874  });
875
876  mocha.it('10-2: test isSpecifiedExt under build release', function () {
877    this.rollup.build(RELEASE);
878    const fileExtendName = EXTNAME_JS;
879    const allFiles = new Set<string>();
880    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
881    this.mockfileList = allFiles.values();
882    for (const filePath of this.mockfileList) {
883      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
884        if (filePath.endsWith(EXTNAME_JS)) {
885          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true;
886        } else {
887          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false;
888        }
889      }
890    }
891  });
892
893  mocha.it('10-3: test isSpecifiedExt under preview debug', function () {
894    this.rollup.preview();
895    const fileExtendName = EXTNAME_TS;
896    const allFiles = new Set<string>();
897    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
898    this.mockfileList = allFiles.values();
899    for (const filePath of this.mockfileList) {
900      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
901        if (filePath.endsWith(EXTNAME_TS)) {
902          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true;
903        } else {
904          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false;
905        }
906      }
907    }
908  });
909
910  mocha.it('10-4: test isSpecifiedExt under hot reload debug', function () {
911    this.rollup.hotReload();
912    const fileExtendName = EXTNAME_JS;
913    const allFiles = new Set<string>();
914    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
915    this.mockfileList = allFiles.values();
916    for (const filePath of this.mockfileList) {
917      if (filePath.endsWith(EXTNAME_TS) || filePath.endsWith(EXTNAME_ETS) || filePath.endsWith(EXTNAME_JS)) {
918        if (filePath.endsWith(EXTNAME_JS)) {
919          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.true;
920        } else {
921          expect(isSpecifiedExt(filePath, fileExtendName)).to.be.false;
922        }
923      }
924    }
925  });
926
927  mocha.it('10-5: test isSpecifiedExt under hot fix debug', function () {
928    projectConfig.buildMode = DEBUG;
929    const fileExtendName = EXTNAME_JS;
930    const filePath = TEST_JS;
931    const returnInfo = isSpecifiedExt(filePath, fileExtendName);
932    expect(returnInfo).to.be.true;
933  });
934
935  mocha.it('10-6: test isSpecifiedExt under hot fix release', function () {
936    projectConfig.buildMode = RELEASE;
937    const fileExtendName = EXTNAME_JS;
938    const filePath = TEST_TS;
939    const returnInfo = isSpecifiedExt(filePath, fileExtendName);
940    expect(returnInfo).to.be.false;
941  });
942
943  mocha.it('11-1: test isTsOrEtsSourceFile under build debug', function () {
944    this.rollup.build();
945    const allFiles = new Set<string>();
946    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
947    this.mockfileList = allFiles.values();
948    for (const file of this.mockfileList) {
949      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
950        const returnInfo = isTsOrEtsSourceFile(file);
951        expect(returnInfo).to.be.true;
952      }
953      if (file.endsWith(EXTNAME_JS)) {
954        const returnInfoJs = isTsOrEtsSourceFile(file);
955        expect(returnInfoJs).to.be.false;
956      }
957    }
958  });
959
960  mocha.it('11-2: test isTsOrEtsSourceFile under build release', function () {
961    this.rollup.build(RELEASE);
962    const allFiles = new Set<string>();
963    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
964    this.mockfileList = allFiles.values();
965    for (const file of this.mockfileList) {
966      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
967        const returnInfo = isTsOrEtsSourceFile(file);
968        expect(returnInfo).to.be.true;
969      }
970      if (file.endsWith(EXTNAME_JS)) {
971        const returnInfoJs = isTsOrEtsSourceFile(file);
972        expect(returnInfoJs).to.be.false;
973      }
974    }
975  });
976
977  mocha.it('11-3: test isTsOrEtsSourceFile under preview debug', function () {
978    this.rollup.preview();
979    const allFiles = new Set<string>();
980    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
981    this.mockfileList = allFiles.values();
982    for (const file of this.mockfileList) {
983      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
984        const returnInfo = isTsOrEtsSourceFile(file);
985        expect(returnInfo).to.be.true;
986      }
987      if (file.endsWith(EXTNAME_JS)) {
988        const returnInfoJs = isTsOrEtsSourceFile(file);
989        expect(returnInfoJs).to.be.false;
990      }
991    }
992  });
993
994  mocha.it('11-4: test isTsOrEtsSourceFile under hot reload debug', function () {
995    this.rollup.hotReload();
996    const allFiles = new Set<string>();
997    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
998    this.mockfileList = allFiles.values();
999    for (const file of this.mockfileList) {
1000      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
1001        const returnInfo = isTsOrEtsSourceFile(file);
1002        expect(returnInfo).to.be.true;
1003      }
1004      if (file.endsWith(EXTNAME_JS)) {
1005        const returnInfoJs = isTsOrEtsSourceFile(file);
1006        expect(returnInfoJs).to.be.false;
1007      }
1008    }
1009  });
1010
1011  mocha.it('11-5: test isTsOrEtsSourceFile under hot fix debug', function () {
1012    projectConfig.buildMode = DEBUG;
1013    const file = TEST_TS;
1014    const returnInfo = isTsOrEtsSourceFile(file);
1015    expect(returnInfo).to.be.true;
1016    const fileEts = TEST_ETS;
1017    const returnInfoEts = isTsOrEtsSourceFile(fileEts);
1018    expect(returnInfoEts).to.be.true;
1019
1020  });
1021
1022  mocha.it('11-6: test isTsOrEtsSourceFile under hot fix release', function () {
1023    projectConfig.buildMode = RELEASE;
1024    const file = TEST_TS;
1025    const returnInfo = isTsOrEtsSourceFile(file);
1026    expect(returnInfo).to.be.true;
1027    const fileJs = TEST_JS;
1028    const returnInfoJs = isTsOrEtsSourceFile(fileJs);
1029    expect(returnInfoJs).to.be.false;
1030  });
1031
1032  mocha.it('12-1: test isJsSourceFile under build debug', function () {
1033    this.rollup.build();
1034    const allFiles = new Set<string>();
1035    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1036    this.mockfileList = allFiles.values();
1037    for (const file of this.mockfileList) {
1038      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
1039        const returnInfo = isJsSourceFile(file);
1040        expect(returnInfo).to.be.false;
1041      }
1042      if (file.endsWith(EXTNAME_JS)) {
1043        const returnInfoJs = isJsSourceFile(file);
1044        expect(returnInfoJs).to.be.true;
1045      }
1046    }
1047  });
1048
1049  mocha.it('12-2: test isJsSourceFile under build release', function () {
1050    this.rollup.build(RELEASE);
1051    const allFiles = new Set<string>();
1052    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1053    this.mockfileList = allFiles.values();
1054    for (const file of this.mockfileList) {
1055      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
1056        const returnInfo = isJsSourceFile(file);
1057        expect(returnInfo).to.be.false;
1058      }
1059      if (file.endsWith(EXTNAME_JS)) {
1060        const returnInfoJs = isJsSourceFile(file);
1061        expect(returnInfoJs).to.be.true;
1062      }
1063    }
1064  });
1065
1066  mocha.it('12-3: test isJsSourceFile under preview debug', function () {
1067    this.rollup.preview();
1068    const allFiles = new Set<string>();
1069    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1070    this.mockfileList = allFiles.values();
1071    for (const file of this.mockfileList) {
1072      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
1073        const returnInfo = isJsSourceFile(file);
1074        expect(returnInfo).to.be.false;
1075      }
1076      if (file.endsWith(EXTNAME_JS)) {
1077        const returnInfoJs = isJsSourceFile(file);
1078        expect(returnInfoJs).to.be.true;
1079      }
1080    }
1081  });
1082
1083  mocha.it('12-4: test isJsSourceFile under hot reload debug', function () {
1084    this.rollup.hotReload();
1085    const allFiles = new Set<string>();
1086    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1087    this.mockfileList = allFiles.values();
1088    for (const file of this.mockfileList) {
1089      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS)) {
1090        const returnInfo = isJsSourceFile(file);
1091        expect(returnInfo).to.be.false;
1092      }
1093      if (file.endsWith(EXTNAME_JS)) {
1094        const returnInfoJs = isJsSourceFile(file);
1095        expect(returnInfoJs).to.be.true;
1096      }
1097    }
1098  });
1099
1100  mocha.it('12-5: test isJsSourceFile under hot fix debug', function () {
1101    projectConfig.buildMode = DEBUG;
1102    const file = TEST_JS;
1103    const returnInfo = isJsSourceFile(file);
1104    expect(returnInfo).to.be.true;
1105    const fileTs = TEST_TS;
1106    const returnInfoTs = isJsSourceFile(fileTs);
1107    expect(returnInfoTs).to.be.false;
1108  });
1109
1110  mocha.it('12-6: test isJsSourceFile under hot fix release', function () {
1111    projectConfig.buildMode = RELEASE;
1112    const file = TEST_JS;
1113    const returnInfo = isJsSourceFile(file);
1114    expect(returnInfo).to.be.true;
1115    const fileTs = TEST_TS;
1116    const returnInfoTs = isJsSourceFile(fileTs);
1117    expect(returnInfoTs).to.be.false;
1118  });
1119
1120  mocha.it('13-1: test isJsonSourceFile under build debug', function () {
1121    this.rollup.build();
1122    const allFiles = new Set<string>();
1123    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1124    this.mockfileList = allFiles.values(); for (const file of this.mockfileList) {
1125      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
1126        const returnInfo = isJsonSourceFile(file);
1127        expect(returnInfo).to.be.false;
1128      }
1129      if (file.endsWith(EXTNAME_JSON)) {
1130        const returnInfoJson = isJsonSourceFile(file);
1131        expect(returnInfoJson).to.be.true;
1132      }
1133    }
1134  });
1135
1136  mocha.it('13-2: test isJsonSourceFile under build release', function () {
1137    this.rollup.build(RELEASE);
1138    const allFiles = new Set<string>();
1139    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1140    this.mockfileList = allFiles.values();
1141    for (const file of this.mockfileList) {
1142      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
1143        const returnInfo = isJsonSourceFile(file);
1144        expect(returnInfo).to.be.false;
1145      }
1146      if (file.endsWith(EXTNAME_JSON)) {
1147        const returnInfoJson = isJsonSourceFile(file);
1148        expect(returnInfoJson).to.be.true;
1149      }
1150    }
1151  });
1152
1153  mocha.it('13-3: test isJsonSourceFile under preview debug', function () {
1154    this.rollup.preview();
1155    const allFiles = new Set<string>();
1156    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1157    this.mockfileList = allFiles.values();
1158    for (const file of this.mockfileList) {
1159      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
1160        const returnInfo = isJsonSourceFile(file);
1161        expect(returnInfo).to.be.false;
1162      }
1163      if (file.endsWith(EXTNAME_JSON)) {
1164        const returnInfoJson = isJsonSourceFile(file);
1165        expect(returnInfoJson).to.be.true;
1166      }
1167    }
1168  });
1169
1170  mocha.it('13-4: test isJsonSourceFile under hot reload debug', function () {
1171    this.rollup.hotReload();
1172    const allFiles = new Set<string>();
1173    scanFiles(this.rollup.share.projectConfig.modulePath, allFiles);
1174    this.mockfileList = allFiles.values();
1175    for (const file of this.mockfileList) {
1176      if (file.endsWith(EXTNAME_TS) || file.endsWith(EXTNAME_ETS) || file.endsWith(EXTNAME_JS)) {
1177        const returnInfo = isJsonSourceFile(file);
1178        expect(returnInfo).to.be.false;
1179      }
1180      if (file.endsWith(EXTNAME_JSON)) {
1181        const returnInfoJson = isJsonSourceFile(file);
1182        expect(returnInfoJson).to.be.true;
1183      }
1184    }
1185  });
1186
1187  mocha.it('13-5: test isJsonSourceFile under hot fix debug', function () {
1188    projectConfig.buildMode = DEBUG;
1189    const file = TEST_TS;
1190    const returnInfo = isJsonSourceFile(file);
1191    expect(returnInfo).to.be.false;
1192    const fileJson = TEST_JSON;
1193    const returnInfoJson = isJsonSourceFile(fileJson);
1194    expect(returnInfoJson).to.be.true;
1195  });
1196
1197  mocha.it('13-6: test isJsonSourceFile under hot fix release', function () {
1198    projectConfig.buildMode = RELEASE;
1199    const file = TEST_TS;
1200    const returnInfo = isJsonSourceFile(file);
1201    expect(returnInfo).to.be.false;
1202    const fileJson = TEST_JS;
1203    const returnInfoJson = isJsonSourceFile(fileJson);
1204    expect(returnInfoJson).to.be.false;
1205  });
1206
1207  mocha.it('14-1: test moduleResolutionHost', function () {
1208    const dirExists = moduleResolutionHostTest.directoryExists(path.resolve(__dirname));
1209    const dirNotExists = moduleResolutionHostTest.directoryExists(path.resolve('./dirNotExists'));
1210    expect(dirExists).to.be.true;
1211    expect(dirNotExists).to.be.false;
1212    const fileExists = moduleResolutionHostTest.fileExists(path.resolve(__filename));
1213    const fileNotExists = moduleResolutionHostTest.fileExists(path.resolve('./fileNotExists'));
1214    expect(fileExists).to.be.true;
1215    expect(fileNotExists).to.be.false;
1216    const dirExistsCache = moduleResolutionHostTest.directoryExists(path.resolve(__dirname));
1217    const dirNotExistsCache = moduleResolutionHostTest.directoryExists(path.resolve('./dirNotExists'));
1218    expect(dirExistsCache).to.be.true;
1219    expect(dirNotExistsCache).to.be.false;
1220    const fileExistsCache = moduleResolutionHostTest.fileExists(path.resolve(__filename));
1221    const fileNotExistsCache = moduleResolutionHostTest.fileExists(path.resolve('./fileNotExists'));
1222    expect(fileExistsCache).to.be.true;
1223    expect(fileNotExistsCache).to.be.false;
1224  });
1225
1226  mocha.it('15-1: test get/setRollupCache with hvigor provided cache interface', function () {
1227    this.rollup.build();
1228    let cacheKey: string = "cache1";
1229    let cacheKeyNotExist: string = "cacheNotExist";
1230    let cacheValue: object = {
1231      value: "value1"
1232    };
1233    this.rollup.share.initWithCache();
1234    setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue);
1235    expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(cacheValue);
1236    expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined);
1237  });
1238
1239  mocha.it('15-2: test get/setRollupCache with hvigor provided cacheStoreManager interface', function () {
1240    this.rollup.build();
1241    let cacheKey: string = "cache1";
1242    let cacheKeyNotExist: string = "cacheNotExist";
1243    let cacheValue: object = {
1244      value: "value1"
1245    };
1246    this.rollup.share.initWithCacheStoreManager();
1247    setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue);
1248    expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(cacheValue);
1249    expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined);
1250  });
1251
1252  mocha.it('15-3: test get/setRollupCache without hvigor cache interface provided', function () {
1253    this.rollup.build();
1254    let cacheKey: string = "cache1";
1255    let cacheKeyNotExist: string = "cacheNotExist";
1256    let cacheValue: object = {
1257      value: "value1"
1258    };
1259    this.rollup.share.initWithoutCache();
1260    setRollupCache(this.rollup.share, projectConfig, cacheKey, cacheValue);
1261    expect(getRollupCache(this.rollup.share, projectConfig, cacheKey)).to.be.equal(undefined);
1262    expect(getRollupCache(this.rollup.share, projectConfig, cacheKeyNotExist)).to.be.equal(undefined);
1263  });
1264
1265  mocha.it('16-1: test genTemporaryPath adapt external modules', function () {
1266    this.rollup.build();
1267    const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets';
1268    const moduleInfo = {
1269      id: filePath,
1270      meta: {
1271        isLocalDependency: true,
1272        moduleName: 'libhar',
1273        belongModulePath: '/testHar/har',
1274      }
1275    };
1276    this.rollup.moduleInfos.push(moduleInfo);
1277    const projectConfig = this.rollup.share.projectConfig;
1278    const metaInfo = this.rollup.getModuleInfo(filePath).meta;
1279    const cacheFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.cachePath,
1280      projectConfig, metaInfo);
1281    const expectCacheFilePath = `${projectConfig.cachePath}/libhar/src/main/ets/utils/Calc.ets`;
1282    expect(cacheFilePath === expectCacheFilePath).to.be.true;
1283  });
1284
1285  mocha.it('16-2: test genTemporaryPath to concatenate the paths under the PackageHar directory', function () {
1286    this.rollup.build();
1287    const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets';
1288    const moduleInfo = {
1289      id: filePath,
1290      meta: {
1291        isLocalDependency: true,
1292        moduleName: 'libhar',
1293        belongModulePath: '/testHar/har',
1294      }
1295    };
1296    this.rollup.moduleInfos.push(moduleInfo);
1297    const projectConfig = this.rollup.share.projectConfig;
1298    projectConfig.compileHar = true;
1299    const metaInfo = this.rollup.getModuleInfo(filePath).meta;
1300    const buildFilePath = genTemporaryPath(filePath, projectConfig.projectPath, projectConfig.buildPath,
1301      projectConfig, metaInfo, true);
1302    const expectBuildFilePath = `${projectConfig.buildPath}/src/main/ets/utils/Calc.ets`;
1303    expect(buildFilePath === expectBuildFilePath).to.be.true;
1304  });
1305
1306  mocha.it('17-1: test getProjectRootPath adapt external modules', function () {
1307    this.rollup.build();
1308    const filePath: string = '/testHar/har/src/main/ets/utils/Calc.ets';
1309    this.rollup.share.projectConfig.rootPathSet = ['/testHar', `${PROJECT_ROOT}/${DEFAULT_PROJECT}`];
1310    const projectConfig = this.rollup.share.projectConfig;
1311    const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true);
1312    const statSyncStub = sinon.stub(fs, 'statSync').returns({
1313      isFile: sinon.stub().returns(true)
1314    });
1315    const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet);
1316    const expectProjectConfig: string = '/testHar';
1317    expect(projectRootPath === expectProjectConfig).to.be.true;
1318    existsSyncStub.restore();
1319    statSyncStub.restore();
1320  });
1321
1322  mocha.it('17-2: test getProjectRootPath adapt external modules(multiple project names contain a relationship)',
1323    function () {
1324    this.rollup.build();
1325    const filePath: string = '/project/testA/har/src/main/ets/utils/Calc.ets';
1326    this.rollup.share.projectConfig.rootPathSet = ['/project/test', '/project/testA', '/project/testAB'];
1327    const projectConfig = this.rollup.share.projectConfig;
1328    const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true);
1329    const statSyncStub = sinon.stub(fs, 'statSync').returns({
1330      isFile: sinon.stub().returns(true)
1331    });
1332    const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet);
1333    const expectProjectConfig: string = '/project/testA';
1334    expect(projectRootPath === expectProjectConfig).to.be.true;
1335    existsSyncStub.restore();
1336    statSyncStub.restore();
1337  });
1338
1339  mocha.it('17-3: test getProjectRootPath under build', function () {
1340    this.rollup.build();
1341    const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har/src/main/ets/utils/Calc.ets`;
1342    const projectConfig = this.rollup.share.projectConfig;
1343    const projectRootPath: string = getProjectRootPath(filePath, projectConfig, projectConfig.rootPathSet);
1344    expect(projectRootPath === projectConfig.projectRootPath).to.be.true;
1345  });
1346
1347  mocha.it('18-1: test getBelongModuleInfo under build file is local dependency', function () {
1348    this.rollup.build();
1349    const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har/src/main/ets/utils/Calc.ets`;
1350    const projectRootPath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}`;
1351    const modulePathMap: Object = {
1352        'enrty': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/entry`,
1353        'libhar': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har`
1354    };
1355    const expectBelongModuleInfo: Object = {
1356      isLocalDependency: true,
1357      moduleName: 'libhar',
1358      belongModulePath: `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har`
1359    };
1360    const belongModuleInfo: Object = getBelongModuleInfo(filePath, modulePathMap, projectRootPath);
1361    Object.keys(belongModuleInfo).forEach(item => {
1362      expect(belongModuleInfo[item] === expectBelongModuleInfo[item]).to.be.true;
1363    });
1364  });
1365
1366  mocha.it('18-2: test getBelongModuleInfo under build file is not local dependency', function () {
1367    this.rollup.build();
1368    const filePath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}/oh_modules/.ohpm/json5/index.js`;
1369    const projectRootPath: string = `${PROJECT_ROOT}/${DEFAULT_PROJECT}`;
1370    const modulePathMap: Object = {
1371        'enrty': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/entry`,
1372        'libhar': `${PROJECT_ROOT}/${DEFAULT_PROJECT}/har`
1373    };
1374    const expectBelongModuleInfo: Object = {
1375      isLocalDependency: false,
1376      moduleName: '',
1377      belongModulePath: `${PROJECT_ROOT}/${DEFAULT_PROJECT}`
1378    };
1379    const belongModuleInfo: Object = getBelongModuleInfo(filePath, modulePathMap, projectRootPath);
1380    Object.keys(belongModuleInfo).forEach(item => {
1381      expect(belongModuleInfo[item] === expectBelongModuleInfo[item]).to.be.true;
1382    });
1383  });
1384});