|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { assert } from 'chai'; |
| 5 | +import * as fs from 'fs-extra'; |
| 6 | +import * as path from '../../../platform/vscode-path/path'; |
| 7 | +import * as os from 'os'; |
| 8 | +import { FileSystem } from './fileSystem.node'; |
| 9 | + |
| 10 | +suite('FileSystem - glob functionality', () => { |
| 11 | + let fileSystem: FileSystem; |
| 12 | + let tempDir: string; |
| 13 | + |
| 14 | + setup(async () => { |
| 15 | + fileSystem = new FileSystem(); |
| 16 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vscode-deepnote-test-')); |
| 17 | + }); |
| 18 | + |
| 19 | + teardown(async () => { |
| 20 | + await fs.remove(tempDir); |
| 21 | + }); |
| 22 | + |
| 23 | + test('searchLocal should find files matching pattern', async () => { |
| 24 | + // Create test files |
| 25 | + await fs.writeFile(path.join(tempDir, 'test1.ts'), 'content'); |
| 26 | + await fs.writeFile(path.join(tempDir, 'test2.ts'), 'content'); |
| 27 | + await fs.writeFile(path.join(tempDir, 'test.js'), 'content'); |
| 28 | + |
| 29 | + const results = await fileSystem.searchLocal('*.ts', tempDir); |
| 30 | + |
| 31 | + assert.strictEqual(results.length, 2); |
| 32 | + assert.ok(results.some((f) => f.endsWith('test1.ts'))); |
| 33 | + assert.ok(results.some((f) => f.endsWith('test2.ts'))); |
| 34 | + }); |
| 35 | + |
| 36 | + test('searchLocal should find files in subdirectories with ** pattern', async () => { |
| 37 | + // Create test files in subdirectories |
| 38 | + await fs.ensureDir(path.join(tempDir, 'sub1')); |
| 39 | + await fs.ensureDir(path.join(tempDir, 'sub2')); |
| 40 | + await fs.writeFile(path.join(tempDir, 'sub1', 'test1.ts'), 'content'); |
| 41 | + await fs.writeFile(path.join(tempDir, 'sub2', 'test2.ts'), 'content'); |
| 42 | + |
| 43 | + const results = await fileSystem.searchLocal('**/*.ts', tempDir); |
| 44 | + |
| 45 | + assert.strictEqual(results.length, 2); |
| 46 | + assert.ok(results.some((f) => f.includes('sub1') && f.endsWith('test1.ts'))); |
| 47 | + assert.ok(results.some((f) => f.includes('sub2') && f.endsWith('test2.ts'))); |
| 48 | + }); |
| 49 | + |
| 50 | + test('searchLocal should find hidden files when dot option is true', async () => { |
| 51 | + // Create hidden file |
| 52 | + await fs.writeFile(path.join(tempDir, '.hidden.ts'), 'content'); |
| 53 | + await fs.writeFile(path.join(tempDir, 'visible.ts'), 'content'); |
| 54 | + |
| 55 | + const results = await fileSystem.searchLocal('*.ts', tempDir, true); |
| 56 | + |
| 57 | + assert.ok(results.length >= 2); |
| 58 | + assert.ok(results.some((f) => f.endsWith('.hidden.ts'))); |
| 59 | + assert.ok(results.some((f) => f.endsWith('visible.ts'))); |
| 60 | + }); |
| 61 | + |
| 62 | + test('searchLocal should not find hidden files when dot option is false', async () => { |
| 63 | + // Create hidden file |
| 64 | + await fs.writeFile(path.join(tempDir, '.hidden.ts'), 'content'); |
| 65 | + await fs.writeFile(path.join(tempDir, 'visible.ts'), 'content'); |
| 66 | + |
| 67 | + const results = await fileSystem.searchLocal('*.ts', tempDir, false); |
| 68 | + |
| 69 | + assert.strictEqual(results.length, 1); |
| 70 | + assert.ok(results.some((f) => f.endsWith('visible.ts'))); |
| 71 | + assert.ok(!results.some((f) => f.endsWith('.hidden.ts'))); |
| 72 | + }); |
| 73 | + |
| 74 | + test('searchLocal should return empty array when no files match', async () => { |
| 75 | + const results = await fileSystem.searchLocal('*.nonexistent', tempDir); |
| 76 | + |
| 77 | + assert.strictEqual(results.length, 0); |
| 78 | + }); |
| 79 | + |
| 80 | + test('searchLocal should handle patterns with multiple extensions', async () => { |
| 81 | + await fs.writeFile(path.join(tempDir, 'test.ts'), 'content'); |
| 82 | + await fs.writeFile(path.join(tempDir, 'test.js'), 'content'); |
| 83 | + await fs.writeFile(path.join(tempDir, 'test.py'), 'content'); |
| 84 | + |
| 85 | + const results = await fileSystem.searchLocal('*.{ts,js}', tempDir); |
| 86 | + |
| 87 | + assert.strictEqual(results.length, 2); |
| 88 | + assert.ok(results.some((f) => f.endsWith('.ts'))); |
| 89 | + assert.ok(results.some((f) => f.endsWith('.js'))); |
| 90 | + assert.ok(!results.some((f) => f.endsWith('.py'))); |
| 91 | + }); |
| 92 | +}); |
0 commit comments