• 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 this 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 */
15import path from 'path';
16import fs from 'fs';
17import mocha from 'mocha';
18import sinon from 'sinon';
19import { expect } from 'chai';
20
21import {
22  	BUILD_ON
23} from '../../lib/pre_define';
24import {
25	resetComponentCollection,
26  	componentCollection
27} from '../../lib/validate_ui_syntax';
28import {
29	transformLog
30} from '../../lib/process_ui_syntax';
31import {
32  	componentInfo,
33  	resetUtils,
34	storedFileInfo
35} from '../../lib/utils';
36import {
37	partialUpdateConfig,
38	projectConfig,
39	resetGlobalProgram,
40	resetMain,
41	resources
42} from '../../main';
43import {
44	etsChecker
45} from '../../lib/fast_build/ets_ui/rollup-plugin-ets-checker';
46import {
47	etsTransform
48} from '../../lib/fast_build/ets_ui/rollup-plugin-ets-typescript';
49import processStructComponentV2 from '../../lib/process_struct_componentV2';
50import {
51	RollUpPluginMock
52} from './helpers/mockRollupContext';
53import {
54	PartialUpdateConfig,
55	ProjectConfig
56} from './helpers/projectConfig';
57import {
58	CACHE_PATH,
59	UT_PARTIAL_UPFATE_PAGES
60} from './helpers/pathConfig';
61import {
62	parseCode,
63	sourceReplace
64} from './helpers/parser';
65import { resolveWithExtension } from './ut.test';
66const PROJECT_ROOT: string = path.resolve(__dirname, '../../test/transform_ut');
67const DEFAULT_PROJECT: string = 'application';
68const TEST_CASES_PATH: string = path.resolve(PROJECT_ROOT, DEFAULT_PROJECT, 'entry/src/main/ets/pages');
69const OUTPUTS_PATH: string = path.resolve(PROJECT_ROOT, DEFAULT_PROJECT, 'entry/build', CACHE_PATH, 'entry/src/main/ets/pages');
70const MAIN_PAGES: string[] = UT_PARTIAL_UPFATE_PAGES.map((p) => `pages/utForPartialUpdate/${p}`);
71
72mocha.describe('test UT for partial update testcases [non-preview mode]', function () {
73	this.timeout(10000);
74
75	mocha.before(function () {
76		resetUtils();
77		resetGlobalProgram();
78		resetMain();
79		this.rollup = new RollUpPluginMock();
80		this.rollup.build(PROJECT_ROOT, DEFAULT_PROJECT, MAIN_PAGES);
81
82		this.globalProjectConfig = new ProjectConfig();
83		this.globalProjectConfig.setPreview(false);
84		this.globalProjectConfig.setIgnoreWarning(true);
85		this.globalProjectConfig.scan(PROJECT_ROOT, DEFAULT_PROJECT, MAIN_PAGES);
86		this.globalProjectConfig.mockCompileContextInfo(`${PROJECT_ROOT}/${DEFAULT_PROJECT}`, MAIN_PAGES);
87		this.globalProjectConfig.concat(RollUpPluginMock.mockArkProjectConfig(PROJECT_ROOT, DEFAULT_PROJECT, false));
88
89		this.rollup.share.projectConfig.concat(this.globalProjectConfig);
90		Object.assign(projectConfig, this.globalProjectConfig);
91
92		this.globalPartialUpdateConfig = new PartialUpdateConfig();
93		this.globalPartialUpdateConfig.setPartialUpdateMode(true);
94		this.globalPartialUpdateConfig.mockDisableArkTSLinter();
95
96		Object.assign(partialUpdateConfig, this.globalPartialUpdateConfig);
97
98		this.etsCheckerPlugin = etsChecker();
99		this.etsTransformPlugin = etsTransform();
100
101		// disable writing to local files
102		sinon.stub(fs, 'writeSync');
103
104		// run etsChecker once
105		const buildStart = this.etsCheckerPlugin.buildStart.bind(this.rollup);
106		buildStart();
107	});
108
109	mocha.after(() => {
110		this.rollup?.share?.flushLogger();
111		delete this.rollup;
112		delete this.globalProjectConfig;
113		delete this.globalPartialUpdateConfig;
114		delete this.etsCheckerPlugin;
115		delete this.etsTransformPlugin;
116
117		resetUtils();
118		resetGlobalProgram();
119		resetMain();
120		sinon.restore();
121	});
122
123	mocha.beforeEach(function () {
124		resources.app["media"] = {icon:16777222};
125		resources.app["font"] = {song:16777223};
126
127		process.env.rawFileResource = './';
128		process.env.compileMode = 'moduleJson';
129		process.env.compiler = BUILD_ON;
130		process.env.compileTool = 'rollup';
131
132		transformLog.errors = [];
133		componentInfo.id = 0;
134		componentCollection.customComponents.clear();
135		resetComponentCollection();
136		storedFileInfo.setCurrentArkTsFile();
137	});
138
139	mocha.afterEach(function () {
140		processStructComponentV2.resetStructMapInEts();
141	});
142
143	UT_PARTIAL_UPFATE_PAGES.forEach((utPage, index) => {
144		mocha.it(`1-${index + 1}: test ${utPage}`, function (done) {
145			const sourceFilePath: string = path.resolve(TEST_CASES_PATH, `utForPartialUpdate/${utPage}.ets`);
146			const sourceCode: string = fs.readFileSync(sourceFilePath, 'utf-8');
147			const targetFilePath: string = resolveWithExtension(OUTPUTS_PATH, 'utForPartialUpdate', utPage);
148			const targetCode: string = fs.readFileSync(targetFilePath, 'utf-8');
149
150			storedFileInfo.addFileCacheInfo(sourceFilePath);
151
152			const transform = this.etsTransformPlugin.transform.bind(this.rollup);
153
154			transform(sourceReplace(sourceCode), sourceFilePath)
155				.then(res => {
156					expect(parseCode(res.code)).eql(parseCode(targetCode));
157					done();
158				})
159				.catch(err => done(err));
160		});
161	});
162});
163