|
1 | 1 | import assert from 'node:assert'; |
2 | | -import { join, sep } from 'node:path'; |
3 | | -import { beforeEach, describe, it } from 'node:test'; |
| 2 | +import { readFileSync, unlinkSync } from 'node:fs'; |
| 3 | +import { homedir } from 'node:os'; |
| 4 | +import { join, resolve, sep } from 'node:path'; |
| 5 | +import { afterEach, beforeEach, describe, it } from 'node:test'; |
4 | 6 | import main from '../main.js'; |
5 | 7 | import TestDifferenceFormatter from './TestDifferenceFormatter.js'; |
6 | 8 | import TestLogger from './TestLogger.js'; |
@@ -393,5 +395,99 @@ describe('compare', () => { |
393 | 395 | ]); |
394 | 396 | }); |
395 | 397 | }); |
| 398 | + |
| 399 | + describe('when "--log-csv" argument is passed', () => { |
| 400 | + const mockedDate = new Date('2025-10-05 12:34:56.789'); |
| 401 | + let originalDateNow: typeof Date.now; |
| 402 | + |
| 403 | + const expectedCsvFilePath = join(homedir(), 'dir_diff_export_20251005_123456789.csv'); |
| 404 | + |
| 405 | + beforeEach(async () => { |
| 406 | + originalDateNow = Date.now; |
| 407 | + Date.now = () => mockedDate.getTime(); |
| 408 | + |
| 409 | + process.argv = [ |
| 410 | + 'node', |
| 411 | + '.', |
| 412 | + 'compare', |
| 413 | + '--source', |
| 414 | + sourceDirPath, |
| 415 | + '--target', |
| 416 | + targetDirPath, |
| 417 | + '--log-csv', |
| 418 | + ]; |
| 419 | + |
| 420 | + await main({ logger, differenceFormatter }); |
| 421 | + }); |
| 422 | + |
| 423 | + afterEach(() => { |
| 424 | + Date.now = originalDateNow; |
| 425 | + |
| 426 | + unlinkSync(expectedCsvFilePath); |
| 427 | + }); |
| 428 | + |
| 429 | + it('outputs the expected log messages', async () => { |
| 430 | + assert.deepStrictEqual(logger.getMessages(), [ |
| 431 | + `Path to the source directory: "${sourceDirPath}"`, |
| 432 | + `Path to the target directory: "${targetDirPath}"`, |
| 433 | + '! Directory difference is logged into CSV file (--log-csv)', |
| 434 | + '', |
| 435 | + '', |
| 436 | + '', |
| 437 | + `<green>source-only</green> | | .dot_file_added`, |
| 438 | + `<green>source-only</green> | | file1_added.txt`, |
| 439 | + `<green>source-only</green> | | file2_added.txt`, |
| 440 | + `<green>source-only</green> | dir | subdir1_added`, |
| 441 | + `<green>source-only</green> | | subdir1_added${sep}file11_added.txt`, |
| 442 | + `<green>source-only</green> | | subdir2${sep}file22_added.txt`, |
| 443 | + `<green>source-only</green> | | subdir2${sep}subdir21${sep}file212_added.txt`, |
| 444 | + `<green>source-only</green> | dir | subdir2${sep}subdir22_added`, |
| 445 | + `<green>source-only</green> | | subdir2${sep}subdir22_added${sep}file221_added.txt`, |
| 446 | + `<yellow>different </yellow> | | file5_modified_content.txt`, |
| 447 | + `<yellow>different </yellow> | | subdir2${sep}file23_modified_size.txt`, |
| 448 | + `<yellow>different </yellow> | | subdir2${sep}subdir21${sep}file213_modified_content.txt`, |
| 449 | + `<red>target-only</red> | | file6_removed.txt`, |
| 450 | + `<red>target-only</red> | | subdir2${sep}file24_removed.txt`, |
| 451 | + `<red>target-only</red> | dir | subdir3_removed`, |
| 452 | + `<red>target-only</red> | | subdir3_removed${sep}file31_removed.txt`, |
| 453 | + '', |
| 454 | + `Directory difference is exported to: "${expectedCsvFilePath}"`, |
| 455 | + ]); |
| 456 | + }); |
| 457 | + |
| 458 | + it('outputs the expected single line messages', async () => { |
| 459 | + assert.deepStrictEqual(logger.getSingleLineMessages(), [ |
| 460 | + 'Comparison is finished.\n' + |
| 461 | + 'Processed: 24 files, 7 directories.\n' + |
| 462 | + `Found: <green>7</green> source-only files, <green>2</green> source-only dirs, <yellow>3</yellow> different files, <red>3</red> target-only files, <red>1</red> target-only dirs.`, |
| 463 | + ]); |
| 464 | + }); |
| 465 | + |
| 466 | + it('creates the expected CSV file', async () => { |
| 467 | + const root = resolve(import.meta.dirname, '../../test/resources/common'); |
| 468 | + const csvFileContent = readFileSync(expectedCsvFilePath, 'utf-8'); |
| 469 | + |
| 470 | + assert.deepStrictEqual( |
| 471 | + csvFileContent, |
| 472 | + '"Difference type","Entry type","Relative entry path","Absolute entry path"\n' + |
| 473 | + `"source-only","file",".dot_file_added","${root}${sep}source${sep}.dot_file_added"\n` + |
| 474 | + `"source-only","file","file1_added.txt","${root}${sep}source${sep}file1_added.txt"\n` + |
| 475 | + `"source-only","file","file2_added.txt","${root}${sep}source${sep}file2_added.txt"\n` + |
| 476 | + `"source-only","dir","subdir1_added","${root}${sep}source${sep}subdir1_added"\n` + |
| 477 | + `"source-only","file","subdir1_added\\file11_added.txt","${root}${sep}source${sep}subdir1_added${sep}file11_added.txt"\n` + |
| 478 | + `"source-only","file","subdir2\\file22_added.txt","${root}${sep}source${sep}subdir2${sep}file22_added.txt"\n` + |
| 479 | + `"source-only","file","subdir2\\subdir21\\file212_added.txt","${root}${sep}source${sep}subdir2${sep}subdir21${sep}file212_added.txt"\n` + |
| 480 | + `"source-only","dir","subdir2\\subdir22_added","${root}${sep}source${sep}subdir2${sep}subdir22_added"\n` + |
| 481 | + `"source-only","file","subdir2\\subdir22_added\\file221_added.txt","${root}${sep}source${sep}subdir2${sep}subdir22_added${sep}file221_added.txt"\n` + |
| 482 | + `"different","file","file5_modified_content.txt","${root}${sep}source${sep}file5_modified_content.txt"\n` + |
| 483 | + `"different","file","subdir2\\file23_modified_size.txt","${root}${sep}source${sep}subdir2${sep}file23_modified_size.txt"\n` + |
| 484 | + `"different","file","subdir2\\subdir21\\file213_modified_content.txt","${root}${sep}source${sep}subdir2${sep}subdir21${sep}file213_modified_content.txt"\n` + |
| 485 | + `"target-only","file","file6_removed.txt","${root}${sep}target${sep}file6_removed.txt"\n` + |
| 486 | + `"target-only","file","subdir2\\file24_removed.txt","${root}${sep}target${sep}subdir2${sep}file24_removed.txt"\n` + |
| 487 | + `"target-only","dir","subdir3_removed","${root}${sep}target${sep}subdir3_removed"\n` + |
| 488 | + `"target-only","file","subdir3_removed\\file31_removed.txt","${root}${sep}target${sep}subdir3_removed${sep}file31_removed.txt"\n`, |
| 489 | + ); |
| 490 | + }); |
| 491 | + }); |
396 | 492 | }); |
397 | 493 | }); |
0 commit comments