|
| 1 | +import * as assert from 'assert' |
| 2 | +import { Writable } from 'stream' |
| 3 | +import { ListDirectory } from './listDirectory' |
| 4 | +import { testFolder } from '@aws/lsp-core' |
| 5 | +import * as path from 'path' |
| 6 | +import * as fs from 'fs/promises' |
| 7 | +import { TestFeatures } from '@aws/language-server-runtimes/testing' |
| 8 | +import { Features } from '@aws/language-server-runtimes/server-interface/server' |
| 9 | + |
| 10 | +describe('ListDirectory Tool', () => { |
| 11 | + let tempFolder: testFolder.TestFolder |
| 12 | + let testFeatures: TestFeatures |
| 13 | + |
| 14 | + before(async () => { |
| 15 | + testFeatures = new TestFeatures() |
| 16 | + // @ts-ignore does not require all fs operations to be implemented |
| 17 | + testFeatures.workspace.fs = { |
| 18 | + exists: path => |
| 19 | + fs |
| 20 | + .access(path) |
| 21 | + .then(() => true) |
| 22 | + .catch(() => false), |
| 23 | + readdir: path => fs.readdir(path, { withFileTypes: true }), |
| 24 | + } as Features['workspace']['fs'] |
| 25 | + tempFolder = await testFolder.TestFolder.create() |
| 26 | + }) |
| 27 | + |
| 28 | + after(async () => { |
| 29 | + await tempFolder.delete() |
| 30 | + }) |
| 31 | + |
| 32 | + it('lists directory contents', async () => { |
| 33 | + await tempFolder.nest('subfolder') |
| 34 | + await tempFolder.write('fileA.txt', 'fileA content') |
| 35 | + |
| 36 | + const listDirectory = new ListDirectory(testFeatures) |
| 37 | + const result = await listDirectory.invoke({ path: tempFolder.path, maxDepth: 0 }) |
| 38 | + |
| 39 | + assert.strictEqual(result.output.kind, 'text') |
| 40 | + const lines = result.output.content.split('\n') |
| 41 | + const hasFileA = lines.some((line: string | string[]) => line.includes('[FILE] ') && line.includes('fileA.txt')) |
| 42 | + const hasSubfolder = lines.some( |
| 43 | + (line: string | string[]) => line.includes('[DIR] ') && line.includes('subfolder') |
| 44 | + ) |
| 45 | + |
| 46 | + assert.ok(hasFileA, 'Should list fileA.txt in the directory output') |
| 47 | + assert.ok(hasSubfolder, 'Should list the subfolder in the directory output') |
| 48 | + }) |
| 49 | + |
| 50 | + it('lists directory contents recursively', async () => { |
| 51 | + await tempFolder.nest('subfolder') |
| 52 | + await tempFolder.write('fileA.txt', 'fileA content') |
| 53 | + await tempFolder.write(path.join('subfolder', 'fileB.md'), '# fileB') |
| 54 | + |
| 55 | + const listDirectory = new ListDirectory(testFeatures) |
| 56 | + const result = await listDirectory.invoke({ path: tempFolder.path }) |
| 57 | + |
| 58 | + assert.strictEqual(result.output.kind, 'text') |
| 59 | + const lines = result.output.content.split('\n') |
| 60 | + const hasFileA = lines.some((line: string | string[]) => line.includes('[FILE] ') && line.includes('fileA.txt')) |
| 61 | + const hasSubfolder = lines.some( |
| 62 | + (line: string | string[]) => line.includes('[DIR] ') && line.includes('subfolder') |
| 63 | + ) |
| 64 | + const hasFileB = lines.some((line: string | string[]) => line.includes('[FILE] ') && line.includes('fileB.md')) |
| 65 | + |
| 66 | + assert.ok(hasFileA, 'Should list fileA.txt in the directory output') |
| 67 | + assert.ok(hasSubfolder, 'Should list the subfolder in the directory output') |
| 68 | + assert.ok(hasFileB, 'Should list fileB.md in the subfolder in the directory output') |
| 69 | + }) |
| 70 | + |
| 71 | + it('throws error if path does not exist', async () => { |
| 72 | + const missingPath = path.join(tempFolder.path, 'no_such_file.txt') |
| 73 | + const listDirectory = new ListDirectory(testFeatures) |
| 74 | + |
| 75 | + await assert.rejects(listDirectory.invoke({ path: missingPath, maxDepth: 0 })) |
| 76 | + }) |
| 77 | + |
| 78 | + it('expands ~ path', async () => { |
| 79 | + const listDirectory = new ListDirectory(testFeatures) |
| 80 | + const result = await listDirectory.invoke({ path: '~', maxDepth: 0 }) |
| 81 | + |
| 82 | + assert.strictEqual(result.output.kind, 'text') |
| 83 | + assert.ok(result.output.content.length > 0) |
| 84 | + }) |
| 85 | +}) |
0 commit comments