|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { join } from 'node:path'; |
| 9 | +import { mkdir, writeFile, rm } from 'node:fs/promises'; |
| 10 | +import { expect } from 'chai'; |
| 11 | +import { traverseForFiles } from '../src/flags.js'; |
| 12 | + |
| 13 | +describe('traverseForFiles', () => { |
| 14 | + const testDir = join(process.cwd(), 'test-temp'); |
| 15 | + const testFiles = [ |
| 16 | + 'file1.yml', |
| 17 | + 'file2.yaml', |
| 18 | + join('subdir', 'file3.yml'), |
| 19 | + join('subdir', 'file4.yaml'), |
| 20 | + join('node_modules', 'file5.yml'), |
| 21 | + join('excluded', 'file6.yaml'), |
| 22 | + ] as const; |
| 23 | + |
| 24 | + before(async () => { |
| 25 | + // Create directory structure and test files |
| 26 | + await mkdir(testDir, { recursive: true }); |
| 27 | + await mkdir(join(testDir, 'subdir'), { recursive: true }); |
| 28 | + await mkdir(join(testDir, 'node_modules'), { recursive: true }); |
| 29 | + await mkdir(join(testDir, 'excluded'), { recursive: true }); |
| 30 | + |
| 31 | + // Create test files |
| 32 | + await Promise.all(testFiles.map((file) => writeFile(join(testDir, file), 'test content'))); |
| 33 | + }); |
| 34 | + |
| 35 | + after(async () => { |
| 36 | + // Clean up test files |
| 37 | + await rm(testDir, { recursive: true, force: true }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should find all yaml files when no excludeDirs is provided', async () => { |
| 41 | + const results = await traverseForFiles(testDir, ['.yml', '.yaml']); |
| 42 | + expect(results).to.have.lengthOf(6); |
| 43 | + expect(results).to.include(join(testDir, 'file1.yml')); |
| 44 | + expect(results).to.include(join(testDir, 'file2.yaml')); |
| 45 | + expect(results).to.include(join(testDir, 'subdir', 'file3.yml')); |
| 46 | + expect(results).to.include(join(testDir, 'subdir', 'file4.yaml')); |
| 47 | + expect(results).to.include(join(testDir, 'node_modules', 'file5.yml')); |
| 48 | + expect(results).to.include(join(testDir, 'excluded', 'file6.yaml')); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should exclude specified directories', async () => { |
| 52 | + const results = await traverseForFiles(testDir, ['.yml', '.yaml'], ['node_modules', 'excluded']); |
| 53 | + expect(results).to.have.lengthOf(4); |
| 54 | + expect(results).to.include(join(testDir, 'file1.yml')); |
| 55 | + expect(results).to.include(join(testDir, 'file2.yaml')); |
| 56 | + expect(results).to.include(join(testDir, 'subdir', 'file3.yml')); |
| 57 | + expect(results).to.include(join(testDir, 'subdir', 'file4.yaml')); |
| 58 | + expect(results).to.not.include(join(testDir, 'node_modules', 'file5.yml')); |
| 59 | + expect(results).to.not.include(join(testDir, 'excluded', 'file6.yaml')); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle empty excludeDirs array', async () => { |
| 63 | + const results = await traverseForFiles(testDir, ['.yml', '.yaml'], []); |
| 64 | + expect(results).to.have.lengthOf(6); |
| 65 | + }); |
| 66 | +}); |
0 commit comments