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 16import mocha from 'mocha'; 17import fs from 'fs'; 18import path from 'path'; 19import { expect } from 'chai'; 20 21import { EXPECT_INDEX_ETS } from './mock/rollup_mock/path_config'; 22import RollUpPluginMock from './mock/rollup_mock/rollup_plugin_mock'; 23import { 24 addLocalPackageSet, 25 compilerOptions, 26 localPackageSet, 27 needReCheckForChangedDepUsers, 28 resetEtsCheck, 29 serviceChecker, 30} from '../../lib/ets_checker'; 31import { TS_BUILD_INFO_SUFFIX } from '../../lib/pre_define' 32import { 33 globalProgram, 34 projectConfig 35} from '../../main'; 36 37mocha.describe('test ets_checker file api', function () { 38 mocha.before(function () { 39 this.rollup = new RollUpPluginMock(); 40 }); 41 42 mocha.after(() => { 43 delete this.rollup; 44 const cacheFile: string = path.resolve(projectConfig.cachePath, '../.ts_checker_cache'); 45 if (fs.existsSync(cacheFile)) { 46 fs.unlinkSync(cacheFile); 47 } 48 const tsBuildInfoFilePath: string = path.resolve(projectConfig.cachePath, '..', TS_BUILD_INFO_SUFFIX); 49 if (fs.existsSync(tsBuildInfoFilePath)) { 50 fs.unlinkSync(tsBuildInfoFilePath); 51 } 52 const tsBuildInfoLinterFilePath: string = tsBuildInfoFilePath + '.linter'; 53 if (fs.existsSync(tsBuildInfoLinterFilePath)) { 54 fs.unlinkSync(tsBuildInfoLinterFilePath); 55 } 56 }); 57 58 mocha.it('1-1: test addLocalPackageSet for original ohmurl', function () { 59 this.rollup.build(); 60 const rollupObject = this.rollup; 61 const rollupFileList = rollupObject.getModuleIds(); 62 projectConfig.useNormalizedOHMUrl = false; 63 for (const moduleId of rollupFileList) { 64 if (fs.existsSync(moduleId)) { 65 addLocalPackageSet(moduleId, rollupObject); 66 } 67 } 68 expect(localPackageSet.has('entry')).to.be.true; 69 localPackageSet.clear(); 70 }); 71 72 mocha.it('1-2: test addLocalPackageSet for normalized ohmurl', function () { 73 this.rollup.build(); 74 const rollupObject = this.rollup; 75 const rollupFileList = rollupObject.getModuleIds(); 76 projectConfig.useNormalizedOHMUrl = true; 77 for (const moduleId of rollupFileList) { 78 const moduleInfo: Object = rollupObject.getModuleInfo(moduleId); 79 if (moduleInfo) { 80 const metaInfo: Object = moduleInfo.meta; 81 metaInfo.pkgName = 'pkgname'; 82 } 83 if (fs.existsSync(moduleId)) { 84 addLocalPackageSet(moduleId, rollupObject); 85 } 86 } 87 expect(localPackageSet.has('pkgname')).to.be.true; 88 }); 89 90 mocha.it('1-3: test getOrCreateLanguageService when dependency in oh-package.json change', function () { 91 this.timeout(10000); 92 this.rollup.build(); 93 let rollupObject = this.rollup; 94 expect(needReCheckForChangedDepUsers).to.be.false; 95 96 interface MockCacheStore { 97 service: Object | undefined; 98 pkgJsonFileHash: string; 99 targetESVersion: number; 100 } 101 const mockServiceCache = { 102 service: undefined, 103 pkgJsonFileHash: '9f07917d395682c73a90af8f5796a2c6', 104 targetESVersion: 8 105 } 106 let mockCache = new Map<string, MockCacheStore>(); 107 mockCache.set('service', mockServiceCache); 108 rollupObject.share.cache = mockCache; 109 rollupObject.share.depInfo = {enableIncre: true}; 110 rollupObject.share.projectConfig.pkgJsonFileHash = '26bde0f30dda53b0afcbf39428ec9851'; 111 Object.assign(projectConfig, rollupObject.share.projectConfig); 112 113 // The current hash and the hash in cache of the dependency differs, should recheck 114 serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share); 115 expect(needReCheckForChangedDepUsers).to.be.true; 116 expect(globalProgram.program != null).to.be.true; 117 118 resetEtsCheck(); 119 expect(needReCheckForChangedDepUsers).to.be.false; 120 expect(globalProgram.program == null).to.be.true; 121 122 // The current hash and the hash in cache of the dependency are the same, no need to recheck 123 serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share); 124 expect(needReCheckForChangedDepUsers).to.be.false; 125 expect(globalProgram.program != null).to.be.true; 126 127 resetEtsCheck(); 128 expect(needReCheckForChangedDepUsers).to.be.false; 129 expect(globalProgram.program == null).to.be.true; 130 }); 131 132 mocha.it('1-4: test getOrCreateLanguageService when paths in compilerOptions change', function () { 133 this.timeout(10000); 134 this.rollup.build(); 135 let rollupObject = this.rollup; 136 process.env.compileTool = 'rollup'; 137 138 Object.assign(projectConfig, rollupObject.share.projectConfig); 139 serviceChecker([EXPECT_INDEX_ETS], null, null, null, rollupObject.share); 140 expect(JSON.stringify(compilerOptions.paths) === '{"*":["*","../../../../*","../*"]}').to.be.true; 141 expect(needReCheckForChangedDepUsers).to.be.false; 142 expect(globalProgram.program != null).to.be.true; 143 expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true; 144 145 resetEtsCheck(); 146 expect(needReCheckForChangedDepUsers).to.be.false; 147 expect(globalProgram.program == null).to.be.true; 148 expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true; 149 150 interface MockCacheStore { 151 service: Object | undefined; 152 pkgJsonFileHash: string; 153 targetESVersion: number; 154 } 155 const mockServiceCache = { 156 service: undefined, 157 pkgJsonFileHash: '9f07917d395682c73a90af8f5796a2c6', 158 targetESVersion: 8 159 } 160 let mockCache = new Map<string, MockCacheStore>(); 161 mockCache.set('service', mockServiceCache); 162 rollupObject.share.cache = mockCache; 163 rollupObject.share.depInfo = {enableIncre: true}; 164 rollupObject.share.projectConfig.pkgJsonFileHash = '26bde0f30dda53b0afcbf39428ec9851'; 165 166 // The current hash of the dependency differs, and the paths in compilerOptions will change since resolveModulePaths change 167 const resolveModulePaths = ['../testdata/expect']; 168 serviceChecker([EXPECT_INDEX_ETS], null, resolveModulePaths, null, rollupObject.share); 169 expect(JSON.stringify(compilerOptions.paths) === '{"*":["*","../../../../../../../testdata/expect/*"]}').to.be.true; 170 expect(needReCheckForChangedDepUsers).to.be.true; 171 expect(globalProgram.program != null).to.be.true; 172 expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true; 173 174 resetEtsCheck(); 175 expect(needReCheckForChangedDepUsers).to.be.false; 176 expect(globalProgram.program == null).to.be.true; 177 expect(compilerOptions.skipPathsInKeyForCompilationSettings).to.be.true; 178 }); 179});