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 { expect } from 'chai'; 17import mocha from 'mocha'; 18import sinon from 'sinon'; 19import fs from 'fs'; 20import childProcess from 'child_process'; 21import cluster from 'cluster'; 22import path from 'path'; 23 24import RollUpPluginMock from '../mock/rollup_mock/rollup_plugin_mock'; 25import { BundleMode } from '../../../lib/fast_build/ark_compiler/bundle/bundle_mode'; 26import { changeFileExtension } from '../../../lib/fast_build/ark_compiler/utils'; 27import { 28 DEBUG, 29 ES2ABC, 30 RELEASE, 31 TEMPORARY, 32 TS2ABC 33} from '../../../lib/fast_build/ark_compiler/common/ark_define'; 34import { toUnixPath } from '../../../lib/utils'; 35import { sleep } from "../utils/utils"; 36import { 37 ArkTSErrorDescription, 38 ArkTSInternalErrorDescription, 39 ErrorCode 40} from '../../../lib/fast_build/ark_compiler/error_code'; 41import { 42 CommonLogger, 43 LogData, 44 LogDataFactory 45} from '../../../lib/fast_build/ark_compiler/logger'; 46 47mocha.describe('test bundle_mode file api', function () { 48 mocha.before(function () { 49 this.rollup = new RollUpPluginMock(); 50 }); 51 52 mocha.after(() => { 53 delete this.rollup; 54 }); 55 56 mocha.it('1-1: test error message of executeArkCompiler', function () { 57 this.rollup.build(); 58 const rollupBundleFileSet: Object = { 59 'test.js': { 60 'type': 'asset', 61 'source': 'test' 62 } 63 }; 64 const errInfo: LogData = LogDataFactory.newInstance( 65 ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE, 66 ArkTSInternalErrorDescription, 67 'Invalid compilation mode. ' + 68 `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.` 69 ); 70 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 71 bundleMode.projectConfig.pandaMode = 'invalid value'; 72 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 73 bundleMode.executeArkCompiler(); 74 expect(stub.calledWith(errInfo)).to.be.true; 75 stub.restore(); 76 }); 77 78 mocha.it('1-2: test error message of executeArkCompiler without getHvigorConsoleLogger', function () { 79 this.rollup.build(); 80 const rollupBundleFileSet: Object = { 81 'test.js': { 82 'type': 'asset', 83 'source': 'test' 84 } 85 }; 86 const errInfo: LogData = LogDataFactory.newInstance( 87 ErrorCode.ETS2BUNDLE_INTERNAL_INVALID_COMPILE_MODE, 88 ArkTSInternalErrorDescription, 89 'Invalid compilation mode. ' + 90 `ProjectConfig.pandaMode should be either ${TS2ABC} or ${ES2ABC}.` 91 ); 92 CommonLogger.destroyInstance(); 93 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 94 this.rollup.share.getHvigorConsoleLogger = undefined; 95 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 96 bundleMode.projectConfig.pandaMode = 'invalid value'; 97 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 98 bundleMode.executeArkCompiler(); 99 expect(stub.calledWith(errInfo.toString())).to.be.true; 100 CommonLogger.destroyInstance(); 101 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 102 stub.restore(); 103 }); 104 105 mocha.it('2-1: test the error message of executeEs2AbcCmd handler error', async function () { 106 this.rollup.build(); 107 const rollupBundleFileSet: Object = { 108 'test.js': { 109 'type': 'asset', 110 'source': 'test' 111 } 112 }; 113 const errInfo: LogData = LogDataFactory.newInstance( 114 ErrorCode.ETS2BUNDLE_INTERNAL_EXECUTE_ES2ABC_WITH_ASYNC_HANDLER_FAILED, 115 ArkTSInternalErrorDescription, 116 'Failed to execute es2abc with async handler. Error: Execution failed' 117 ); 118 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 119 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').throws(new Error('Execution failed')); 120 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 121 try { 122 bundleMode.executeEs2AbcCmd(); 123 } catch (e) { 124 } 125 expect(stub.calledWith(errInfo)).to.be.true; 126 triggerAsyncStub.restore(); 127 stub.restore(); 128 }); 129 130 mocha.it('2-2: test the error message of executeEs2AbcCmd throw error on failed code', async function () { 131 this.rollup.build(); 132 const rollupBundleFileSet: Object = { 133 'test.js': { 134 'type': 'asset', 135 'source': 'test' 136 } 137 }; 138 const errInfo: LogData = LogDataFactory.newInstance( 139 ErrorCode.ETS2BUNDLE_EXTERNAL_ES2ABC_EXECUTION_FAILED, 140 ArkTSErrorDescription, 141 'Failed to execute es2abc.', 142 '', 143 ["Please refer to es2abc's error codes."] 144 ); 145 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 146 const child = childProcess.exec('false', { windowsHide: true }); 147 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').returns(child); 148 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 149 const afterCompilationProcessStub = sinon.stub(bundleMode, 'afterCompilationProcess'); 150 bundleMode.executeEs2AbcCmd(); 151 await sleep(1000); 152 expect(stub.calledWith(errInfo)).to.be.true; 153 triggerAsyncStub.restore(); 154 stub.restore(); 155 afterCompilationProcessStub.restore(); 156 }); 157 158 mocha.it('2-3: test the error message of executeEs2AbcCmd throw error(Failed to startup es2abc)', async function () { 159 this.rollup.build(); 160 const rollupBundleFileSet: Object = { 161 'test.js': { 162 'type': 'asset', 163 'source': 'test' 164 } 165 }; 166 const errInfo: LogData = LogDataFactory.newInstance( 167 ErrorCode.ETS2BUNDLE_INTERNAL_ES2ABC_SUBPROCESS_START_FAILED, 168 ArkTSInternalErrorDescription, 169 'Failed to initialize or launch the es2abc process. Error: test error' 170 ); 171 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 172 const child = { 173 on: sinon.stub(), 174 stderr: { 175 on: sinon.stub(), 176 }, 177 }; 178 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').callsFake((callback) => { 179 callback(); 180 return child; 181 }); 182 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 183 let errorEventCallback; 184 child.on.callsFake((event, callback) => { 185 if (event === 'error') { 186 errorEventCallback = callback; 187 } 188 }); 189 bundleMode.executeEs2AbcCmd(); 190 if (errorEventCallback) { 191 errorEventCallback(new Error('test error')); 192 } 193 await sleep(100); 194 expect(stub.calledWith(errInfo)).to.be.true; 195 triggerAsyncStub.restore(); 196 stub.restore(); 197 }); 198 199 mocha.it('2-4: test the error message of executeEs2AbcCmd handler error ' + 200 'without getHvigorConsoleLogger', async function () { 201 this.rollup.build(); 202 const rollupBundleFileSet: Object = { 203 'test.js': { 204 'type': 'asset', 205 'source': 'test' 206 } 207 }; 208 CommonLogger.destroyInstance(); 209 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 210 this.rollup.share.getHvigorConsoleLogger = undefined; 211 const errInfo: LogData = LogDataFactory.newInstance( 212 ErrorCode.ETS2BUNDLE_INTERNAL_EXECUTE_ES2ABC_WITH_ASYNC_HANDLER_FAILED, 213 ArkTSInternalErrorDescription, 214 'Failed to execute es2abc with async handler. Error: Execution failed' 215 ); 216 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 217 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').throws(new Error('Execution failed')); 218 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 219 try { 220 bundleMode.executeEs2AbcCmd(); 221 } catch (e) { 222 } 223 expect(stub.calledWith(errInfo.toString())).to.be.true; 224 CommonLogger.destroyInstance(); 225 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 226 triggerAsyncStub.restore(); 227 stub.restore(); 228 }); 229 230 mocha.it('2-5: test the error message of executeEs2AbcCmd throw error on failed code ' + 231 'without getHvigorConsoleLogger', async function () { 232 this.rollup.build(); 233 const rollupBundleFileSet: Object = { 234 'test.js': { 235 'type': 'asset', 236 'source': 'test' 237 } 238 }; 239 const errInfo: LogData = LogDataFactory.newInstance( 240 ErrorCode.ETS2BUNDLE_EXTERNAL_ES2ABC_EXECUTION_FAILED, 241 ArkTSErrorDescription, 242 'Failed to execute es2abc.', 243 '', 244 ["Please refer to es2abc's error codes."] 245 ); 246 CommonLogger.destroyInstance(); 247 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 248 this.rollup.share.getHvigorConsoleLogger = undefined; 249 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 250 const child = childProcess.exec('false', { windowsHide: true }); 251 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').returns(child); 252 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 253 const afterCompilationProcessStub = sinon.stub(bundleMode, 'afterCompilationProcess'); 254 bundleMode.executeEs2AbcCmd(); 255 await sleep(1000); 256 expect(stub.calledWith(errInfo.toString())).to.be.true; 257 CommonLogger.destroyInstance(); 258 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 259 triggerAsyncStub.restore(); 260 stub.restore(); 261 afterCompilationProcessStub.restore(); 262 }); 263 264 mocha.it('2-6: test the error message of executeEs2AbcCmd throw error(Failed to startup es2abc) ' + 265 'without getHvigorConsoleLogger', async function () { 266 this.rollup.build(); 267 const rollupBundleFileSet: Object = { 268 'test.js': { 269 'type': 'asset', 270 'source': 'test' 271 } 272 }; 273 const errInfo: LogData = LogDataFactory.newInstance( 274 ErrorCode.ETS2BUNDLE_INTERNAL_ES2ABC_SUBPROCESS_START_FAILED, 275 ArkTSInternalErrorDescription, 276 'Failed to initialize or launch the es2abc process. Error: test error' 277 ); 278 CommonLogger.destroyInstance(); 279 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 280 this.rollup.share.getHvigorConsoleLogger = undefined; 281 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 282 const child = { 283 on: sinon.stub(), 284 stderr: { 285 on: sinon.stub(), 286 }, 287 }; 288 const triggerAsyncStub = sinon.stub(bundleMode, 'triggerAsync').callsFake((callback) => { 289 callback(); 290 return child; 291 }); 292 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 293 let errorEventCallback; 294 child.on.callsFake((event, callback) => { 295 if (event === 'error') { 296 errorEventCallback = callback; 297 } 298 }); 299 bundleMode.executeEs2AbcCmd(); 300 if (errorEventCallback) { 301 errorEventCallback(new Error('test error')); 302 } 303 await sleep(100); 304 expect(stub.calledWith(errInfo.toString())).to.be.true; 305 CommonLogger.destroyInstance(); 306 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 307 triggerAsyncStub.restore(); 308 stub.restore(); 309 }); 310 311 mocha.it('3-1: test the error message of collectBundleFileList(file.type is invalid value)', function () { 312 this.rollup.build(); 313 const rollupBundleFileSet: Object = { 314 'test.js': { 315 'type': '' 316 } 317 }; 318 const errInfo: LogData = LogDataFactory.newInstance( 319 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_RETRIEVE_SOURCE_CODE_FROM_SUMMARY, 320 ArkTSInternalErrorDescription, 321 'Failed to retrieve source code for test.js from rollup file set.' 322 ); 323 const logger: CommonLogger = CommonLogger.getInstance(this.rollup); 324 const stub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 325 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 326 expect(stub.calledWith(errInfo)).to.be.true; 327 stub.restore(); 328 }); 329 330 mocha.it('3-1-1: test the error message of collectBundleFileList(file.type is invalid value)' + 331 ' without getHvigorConsoleLogger', function () { 332 this.rollup.build(); 333 const rollupBundleFileSet: Object = { 334 'test.js': { 335 'type': '' 336 } 337 }; 338 const errInfo: LogData = LogDataFactory.newInstance( 339 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_RETRIEVE_SOURCE_CODE_FROM_SUMMARY, 340 ArkTSInternalErrorDescription, 341 'Failed to retrieve source code for test.js from rollup file set.' 342 ); 343 CommonLogger.destroyInstance(); 344 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 345 this.rollup.share.getHvigorConsoleLogger = undefined; 346 const logger: CommonLogger = CommonLogger.getInstance(this.rollup); 347 const stub = sinon.stub(logger, 'throwArkTsCompilerError'); 348 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 349 expect(stub.calledWith(errInfo.toString())).to.be.true; 350 CommonLogger.destroyInstance(); 351 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 352 stub.restore(); 353 }); 354 355 mocha.it('3-2: test the error message of collectBundleFileList', function () { 356 this.rollup.build(); 357 const rollupBundleFileSet: Object = { 358 'test.js': { 359 'type': 'asset', 360 'source': 'test' 361 } 362 }; 363 const existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((path) => { 364 const pattern = /test\.temp\.js$/; 365 if (pattern.test(path)) { 366 return false; 367 } 368 return true; 369 }); 370 const errInfo: LogData = LogDataFactory.newInstance( 371 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GENERATE_CACHE_SOURCE_FILE, 372 ArkTSInternalErrorDescription, 373 'Failed to generate cached source file: test.js.' 374 ); 375 const logger: CommonLogger = CommonLogger.getInstance(this.rollup); 376 const stub = sinon.stub(logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 377 try { 378 new BundleMode(this.rollup, rollupBundleFileSet); 379 } catch (e) { 380 } 381 expect(stub.calledWith(errInfo)).to.be.true; 382 existsSyncStub.restore(); 383 stub.restore(); 384 }); 385 386 mocha.it('3-2-1: test the error message of collectBundleFileList without getHvigorConsoleLogger', function () { 387 this.rollup.build(); 388 const rollupBundleFileSet: Object = { 389 'test.js': { 390 'type': 'asset', 391 'source': 'test' 392 } 393 }; 394 const existsSyncStub = sinon.stub(fs, 'existsSync').callsFake((path) => { 395 const pattern = /test\.temp\.js$/; 396 if (pattern.test(path)) { 397 return false; 398 } 399 return true; 400 }); 401 const errInfo: LogData = LogDataFactory.newInstance( 402 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_GENERATE_CACHE_SOURCE_FILE, 403 ArkTSInternalErrorDescription, 404 'Failed to generate cached source file: test.js.' 405 ); 406 CommonLogger.destroyInstance(); 407 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 408 this.rollup.share.getHvigorConsoleLogger = undefined; 409 const logger: CommonLogger = CommonLogger.getInstance(this.rollup); 410 const stub = sinon.stub(logger, 'throwArkTsCompilerError'); 411 try { 412 new BundleMode(this.rollup, rollupBundleFileSet); 413 } catch (e) { 414 } 415 expect(stub.calledWith(errInfo.toString())).to.be.true; 416 CommonLogger.destroyInstance(); 417 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 418 existsSyncStub.restore(); 419 stub.restore(); 420 }); 421 422 mocha.it('4-1: test the error message of filterBundleFileListWithHashJson', function () { 423 this.rollup.build(); 424 const rollupBundleFileSet: Object = { 425 'test.js': { 426 'type': 'asset', 427 'source': 'test' 428 } 429 }; 430 const errInfo: LogData = LogDataFactory.newInstance( 431 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_RETRIEVE_PACKAGE_CACHE_IN_INCREMENTAL_BUILD, 432 ArkTSInternalErrorDescription, 433 sinon.match.any 434 ); 435 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 436 const jsonData = JSON.stringify(rollupBundleFileSet, null, 2); 437 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 438 for (const value of bundleMode.intermediateJsBundle.values()) { 439 fs.unlinkSync(value.cacheFilePath); 440 } 441 fs.writeFileSync(bundleMode.hashJsonFilePath, jsonData); 442 bundleMode.filterBundleFileListWithHashJson(); 443 expect(stub.calledWith(errInfo)).to.be.true; 444 stub.restore(); 445 }); 446 447 mocha.it('4-1-1: test the error message of filterBundleFileListWithHashJson without getHvigorConsoleLogger', function () { 448 this.rollup.build(); 449 const rollupBundleFileSet: Object = { 450 'test.js': { 451 'type': 'asset', 452 'source': 'test' 453 } 454 }; 455 const errInfo: LogData = LogDataFactory.newInstance( 456 ErrorCode.ETS2BUNDLE_INTERNAL_UNABLE_TO_RETRIEVE_PACKAGE_CACHE_IN_INCREMENTAL_BUILD, 457 ArkTSInternalErrorDescription 458 ); 459 CommonLogger.destroyInstance(); 460 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 461 this.rollup.share.getHvigorConsoleLogger = undefined; 462 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 463 const jsonData = JSON.stringify(rollupBundleFileSet, null, 2); 464 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 465 for (const value of bundleMode.intermediateJsBundle.values()) { 466 fs.unlinkSync(value.cacheFilePath); 467 } 468 fs.writeFileSync(bundleMode.hashJsonFilePath, jsonData); 469 bundleMode.filterBundleFileListWithHashJson(); 470 expect(stub.calledWithMatch(errInfo.toString())).to.be.true; 471 CommonLogger.destroyInstance(); 472 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 473 stub.restore(); 474 }); 475 476 mocha.it('5-1: test the error message of invokeTs2AbcWorkersToGenAbc(worker error)', function () { 477 this.rollup.build(); 478 const rollupBundleFileSet: Object = { 479 'test.js': { 480 'type': 'asset', 481 'source': 'test' 482 } 483 }; 484 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 485 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 486 const clusterStub = sinon.stub(cluster, 'fork'); 487 const fakeWorker = { 488 on: sinon.stub() 489 }; 490 clusterStub.returns(fakeWorker); 491 const splittedBundles = bundleMode.getSplittedBundles() 492 try { 493 fakeWorker.on.withArgs('message').callsFake((event, callback) => { 494 callback({ data: 'error' }); 495 }); 496 bundleMode.invokeTs2AbcWorkersToGenAbc(splittedBundles) 497 } catch (e) { 498 } 499 expect(stub.calledWith('ArkTS:ERROR Failed to execute ts2abc')).to.be.true; 500 clusterStub.restore(); 501 stub.restore(); 502 }); 503 504 mocha.it('6-1: test the error message of writeHashJson', function () { 505 this.rollup.build(); 506 const rollupBundleFileSet: Object = { 507 'test.js': { 508 'type': 'asset', 509 'source': 'test' 510 } 511 }; 512 const errInfo: LogData = LogDataFactory.newInstance( 513 ErrorCode.ETS2BUNDLE_INTERNAL_HASH_JSON_FILE_GENERATION_MISSING_PATHS, 514 ArkTSInternalErrorDescription, 515 sinon.match.any 516 ); 517 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 518 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 519 try { 520 bundleMode.writeHashJson(); 521 } catch (e) { 522 } 523 expect(stub.calledWith(errInfo)).to.be.true; 524 stub.restore(); 525 }); 526 527 mocha.it('6-2: test the error message of writeHashJson without getHvigorConsoleLogger', function () { 528 this.rollup.build(); 529 const rollupBundleFileSet: Object = { 530 'test.js': { 531 'type': 'asset', 532 'source': 'test' 533 } 534 }; 535 const errInfo: LogData = LogDataFactory.newInstance( 536 ErrorCode.ETS2BUNDLE_INTERNAL_HASH_JSON_FILE_GENERATION_MISSING_PATHS, 537 ArkTSInternalErrorDescription 538 ); 539 CommonLogger.destroyInstance(); 540 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 541 this.rollup.share.getHvigorConsoleLogger = undefined; 542 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 543 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 544 try { 545 bundleMode.writeHashJson(); 546 } catch (e) { 547 } 548 expect(stub.calledWithMatch(errInfo.toString())).to.be.true; 549 CommonLogger.destroyInstance(); 550 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 551 stub.restore(); 552 }); 553 554 mocha.it('7-1: test the error message of copyFileFromCachePathToOutputPath', function () { 555 this.rollup.build(); 556 const rollupBundleFileSet: Object = { 557 'test.js': { 558 'type': 'asset', 559 'source': 'test' 560 } 561 }; 562 const errInfo: LogData = LogDataFactory.newInstance( 563 ErrorCode.ETS2BUNDLE_INTERNAL_INCREMENTAL_BUILD_MISSING_CACHE_ABC_FILE_PATH, 564 ArkTSInternalErrorDescription, 565 sinon.match.any 566 ); 567 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 568 const stub = sinon.stub(bundleMode.logger.getLoggerFromErrorCode(errInfo.code), 'printErrorAndExit'); 569 try { 570 bundleMode.copyFileFromCachePathToOutputPath(); 571 } catch (e) { 572 } 573 expect(stub.calledWith(errInfo)).to.be.true; 574 stub.restore(); 575 }); 576 577 mocha.it('7-2: test the error message of copyFileFromCachePathToOutputPath ' + 578 'without getHvigorConsoleLogger', function () { 579 this.rollup.build(); 580 const rollupBundleFileSet: Object = { 581 'test.js': { 582 'type': 'asset', 583 'source': 'test' 584 } 585 }; 586 const errInfo: LogData = LogDataFactory.newInstance( 587 ErrorCode.ETS2BUNDLE_INTERNAL_INCREMENTAL_BUILD_MISSING_CACHE_ABC_FILE_PATH, 588 ArkTSInternalErrorDescription, 589 ); 590 CommonLogger.destroyInstance(); 591 const getHvigorConsoleLogger = this.rollup.share.getHvigorConsoleLogger; 592 this.rollup.share.getHvigorConsoleLogger = undefined; 593 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 594 const stub = sinon.stub(bundleMode.logger, 'throwArkTsCompilerError'); 595 try { 596 bundleMode.copyFileFromCachePathToOutputPath(); 597 } catch (e) { 598 } 599 expect(stub.calledWithMatch(errInfo.toString())).to.be.true; 600 CommonLogger.destroyInstance(); 601 this.rollup.share.getHvigorConsoleLogger = getHvigorConsoleLogger; 602 stub.restore(); 603 }); 604 605 mocha.it('8-1: test sourceFile field of bundle mode in release', function () { 606 this.rollup.build(); 607 this.rollup.share.projectConfig.buildMode = RELEASE 608 const rollupBundleFileSet: Object = { 609 'test.js': { 610 'type': 'asset', 611 'source': 'test' 612 } 613 }; 614 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 615 const filesinfo: string[] = fs.readFileSync(bundleMode.generateFileInfoOfBundle()).toString().split(";"); 616 // sourceFile is the 4th field in filesInfo 617 const sourceFile: string = filesinfo[3]; 618 const relativeCachePath: string = toUnixPath(bundleMode.projectConfig.cachePath.replace( 619 bundleMode.projectConfig.projectRootPath + path.sep, '')); 620 const buildDirArr: string[] = bundleMode.projectConfig.aceModuleBuild.split(path.sep); 621 const abilityDir: string = buildDirArr[buildDirArr.length - 1]; 622 623 expect(sourceFile === path.join(relativeCachePath, TEMPORARY, abilityDir, 'test.temp.js')).to.be.true; 624 }); 625 626 mocha.it('9-1: test sourceFile field of bundle mode in debug', function () { 627 this.rollup.build(); 628 this.rollup.share.projectConfig.buildMode = DEBUG 629 const rollupBundleFileSet: Object = { 630 'test.js': { 631 'type': 'asset', 632 'source': 'test' 633 } 634 }; 635 const bundleMode = new BundleMode(this.rollup, rollupBundleFileSet); 636 const filesinfo: string[] = fs.readFileSync(bundleMode.generateFileInfoOfBundle()).toString().split(";"); 637 // sourceFile is the 4th field in filesInfo 638 const sourceFile: string = filesinfo[3]; 639 const relativePath: string = toUnixPath(bundleMode.projectConfig.aceModuleBuild.replace( 640 bundleMode.projectConfig.projectRootPath + path.sep, '')); 641 642 expect(sourceFile === path.join(relativePath, 'test.js')).to.be.true; 643 }); 644});