|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import { join } from 'node:path'; |
| 7 | +import { after, before, describe, it } from 'node:test'; |
| 8 | +import { pathToFileURL } from 'node:url'; |
| 9 | + |
| 10 | +import { findFiles, globParent } from '../glob.mjs'; |
| 11 | + |
| 12 | +describe('findFiles', () => { |
| 13 | + let root; |
| 14 | + |
| 15 | + before(() => { |
| 16 | + root = mkdtempSync(join(tmpdir(), 'doc-kit-glob-')); |
| 17 | + mkdirSync(join(root, 'api', 'nested.md'), { recursive: true }); |
| 18 | + writeFileSync(join(root, 'api', 'fs.md'), ''); |
| 19 | + writeFileSync(join(root, 'api', 'path.md'), ''); |
| 20 | + writeFileSync(join(root, 'api', 'quic.md'), ''); |
| 21 | + writeFileSync(join(root, 'api', 'nested.md', 'deep.md'), ''); |
| 22 | + }); |
| 23 | + |
| 24 | + after(() => rmSync(root, { recursive: true, force: true })); |
| 25 | + |
| 26 | + it('should return matching files relative to cwd, without directories', () => { |
| 27 | + assert.deepStrictEqual(findFiles('api/*.md', { cwd: root }).sort(), [ |
| 28 | + 'api/fs.md', |
| 29 | + 'api/path.md', |
| 30 | + 'api/quic.md', |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should leave out ignored files', () => { |
| 35 | + assert.deepStrictEqual( |
| 36 | + findFiles('api/*.md', { cwd: root, ignore: ['api/quic.md'] }).sort(), |
| 37 | + ['api/fs.md', 'api/path.md'] |
| 38 | + ); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return absolute patterns relative to cwd', () => { |
| 42 | + assert.deepStrictEqual( |
| 43 | + findFiles(`${root.split('\\').join('/')}/api/fs.md`, { cwd: root }), |
| 44 | + ['api/fs.md'] |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should accept a URL as cwd', () => { |
| 49 | + assert.deepStrictEqual( |
| 50 | + findFiles('*.md', { cwd: pathToFileURL(join(root, 'api')) }).sort(), |
| 51 | + ['fs.md', 'path.md', 'quic.md'] |
| 52 | + ); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('globParent', () => { |
| 57 | + it('should return the directory before the first glob segment', () => { |
| 58 | + assert.strictEqual(globParent('doc/api/*.md'), 'doc/api'); |
| 59 | + assert.strictEqual(globParent('./doc/api/*.md'), './doc/api'); |
| 60 | + assert.strictEqual(globParent('/root/*.js'), '/root'); |
| 61 | + assert.strictEqual(globParent('lib/**/*.js'), 'lib'); |
| 62 | + assert.strictEqual(globParent('a/{b,c}/*.md'), 'a'); |
| 63 | + assert.strictEqual(globParent('a/b/[x-y]*.md'), 'a/b'); |
| 64 | + assert.strictEqual(globParent('a/+(b|c)/*.md'), 'a'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return the parent directory of a plain path', () => { |
| 68 | + assert.strictEqual(globParent('doc/api/fs.md'), 'doc/api'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should fall back to the current or root directory', () => { |
| 72 | + assert.strictEqual(globParent('*.md'), '.'); |
| 73 | + assert.strictEqual(globParent('/*.md'), '/'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments