|
| 1 | +import fs from 'fs' |
| 2 | +import os from 'os' |
| 3 | +import path from 'path' |
| 4 | +import { getFilesRecursive, filterReadable } from '../src/fs-utils.js' |
| 5 | + |
| 6 | +describe('fs-utils', () => { |
| 7 | + const tmpRoot = path.join(os.tmpdir(), `cuda-toolkit-test-${Date.now()}`) |
| 8 | + |
| 9 | + beforeAll(async () => { |
| 10 | + await fs.promises.mkdir(tmpRoot, { recursive: true }) |
| 11 | + // create files and nested directories |
| 12 | + await fs.promises.mkdir(path.join(tmpRoot, 'subdir')) |
| 13 | + await fs.promises.writeFile(path.join(tmpRoot, 'a.txt'), 'a') |
| 14 | + await fs.promises.writeFile(path.join(tmpRoot, 'subdir', 'b.txt'), 'b') |
| 15 | + |
| 16 | + // create a non-readable file (if platform supports chmod) |
| 17 | + try { |
| 18 | + const p = path.join(tmpRoot, 'noaccess.txt') |
| 19 | + await fs.promises.writeFile(p, 'x') |
| 20 | + await fs.promises.chmod(p, 0o000) |
| 21 | + } catch { |
| 22 | + // ignore chmod failures on platforms that don't support it |
| 23 | + } |
| 24 | + }) |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + try { |
| 28 | + const p = path.join(tmpRoot, 'noaccess.txt') |
| 29 | + await fs.promises.chmod(p, 0o644) |
| 30 | + } catch { |
| 31 | + // ignore |
| 32 | + } |
| 33 | + await fs.promises.rm(tmpRoot, { recursive: true, force: true }) |
| 34 | + }) |
| 35 | + |
| 36 | + test('getFilesRecursive returns all files under directory', async () => { |
| 37 | + const files = await getFilesRecursive(tmpRoot) |
| 38 | + |
| 39 | + const normalizedNames = files.map((f) => path.relative(tmpRoot, f)).sort() |
| 40 | + |
| 41 | + expect(normalizedNames).toEqual( |
| 42 | + ['a.txt', 'noaccess.txt', path.join('subdir', 'b.txt')].sort() |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + test('filterReadable filters out non-readable or missing files', async () => { |
| 47 | + const a = path.join(tmpRoot, 'a.txt') |
| 48 | + const nosuch = path.join(tmpRoot, 'does-not-exist.txt') |
| 49 | + const noaccess = path.join(tmpRoot, 'noaccess.txt') |
| 50 | + |
| 51 | + const result = await filterReadable([a, nosuch, noaccess]) |
| 52 | + |
| 53 | + expect(result).toContain(a) |
| 54 | + expect(result).not.toContain(nosuch) |
| 55 | + }) |
| 56 | +}) |
0 commit comments