import { Stats } from 'fs' import { assert } from 'chai' import { join } from 'path' import { crawl } from '../src' import * as fse from 'fs-extra' import * as temp from 'temp' temp.track() const makeTempDir = (prefix: string): Promise => { return new Promise((resolve, reject) => { temp.mkdir(prefix, (err: Error, dir: string) => { err ? reject(err) : resolve(dir) }) }) } const file = (path: string): Node => ({ dir: false, path }) const dir = (path: string): Node => ({ dir: true, path }) const preds = { onlyFiles: (_path: string, stats: Stats): boolean => { return stats.isFile() }, } const getMapKeys = (m: Map): Array => [...m.keys()] // fromRelative('a/b/c.txt') -> '/Users/user/code/node/recur-readdir/test/dummy/a/b/c.txt' function expandPath(root: string, relativePath: string) { return join(root, relativePath) } function expandPaths( root: string, relativePaths: Array ): Array { return relativePaths.map(path => expandPath(root, path)) } type Node = { dir: boolean; path: string } async function createDir(nodes: Array): Promise { const tempDir = await makeTempDir('recur-readdir-') for (const node of nodes) { if (node.dir) { await fse.ensureDir(join(tempDir, node.path)) } else { await fse.ensureFile(join(tempDir, node.path)) } } return tempDir } describe('missing entry dir', () => { it('fails', async () => { return crawl('./not-found').catch(err => assert.equal('ENOENT', err.code) ) }) }) describe('entry dir is a file', () => { it('returns single path', async () => { const paths = [...(await crawl(__filename)).keys()] assert.deepEqual(paths, [__filename]) }) }) describe('nested folder tree without files', () => { describe('no filter', () => { it('returns only dirs', async () => { const root = await createDir([dir('a/b/c')]) const paths = await crawl(root).then(getMapKeys) assert.sameMembers(paths, expandPaths(root, ['a', 'a/b', 'a/b/c'])) }) }) describe('files-only filter', () => { it('returns empty', async () => { const root = await createDir([dir('a/b/c')]) const paths = await crawl(root, { filter: preds.onlyFiles, }).then(getMapKeys) assert.sameMembers(paths, []) }) }) }) describe('depth', () => { describe('of 0', () => { it('returns empty results', async () => { const root = await createDir([ file('a.txt'), file('a/b.txt'), file('a/b/c.txt'), ]) const paths = await crawl(root, { depth: 0 }).then(getMapKeys) assert.sameMembers(paths, []) }) }) describe('of 1', () => { it('returns first level', async () => { const root = await createDir([ file('a.txt'), file('b.txt'), file('a/b.txt'), file('a/b/c.txt'), ]) const paths = await crawl(root, { depth: 1 }).then(getMapKeys) assert.sameMembers( paths, expandPaths(root, ['a', 'a.txt', 'b.txt']) ) }) }) describe('of 2', () => { it('returns two levels', async () => { const root = await createDir([ file('a.txt'), file('b.txt'), file('a/a.txt'), file('a/b.txt'), file('a/b/a.txt'), file('a/b/b.txt'), ]) const paths = await crawl(root, { depth: 2 }).then(getMapKeys) assert.sameMembers( paths, expandPaths(root, [ // dirs 'a', 'a/b', // files 'a.txt', 'b.txt', 'a/a.txt', 'a/b.txt', ]) ) }) }) }) describe('filter', () => { it('does not stop recursion', async () => { const root = await createDir([ file('file'), file('a1/a2/file'), file('b1/file'), file('b1/b2/file'), ]) const filter = (_path: string, stats: Stats) => { return stats.isFile() } const paths = await crawl(root, { filter }).then(getMapKeys) assert.sameMembers( paths, expandPaths(root, ['file', 'a1/a2/file', 'b1/file', 'b1/b2/file']) ) }) it('verify readme example', async () => { const root = await createDir([ file('a.txt'), file('b.txt'), file('nest1/nest2/nested.txt'), file('.secret/password.txt'), file('.secret/folder/hello.txt'), ]) const filter = (path: string, stats: Stats) => { return !path.includes('/.secret/') && stats.isFile() } const paths = await crawl(root, { filter }).then(getMapKeys) assert.sameMembers( paths, expandPaths(root, ['a.txt', 'b.txt', 'nest1/nest2/nested.txt']) ) }) })