Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311/** Tests for the empty family through the built `./node` entry.
Every case runs inside a disposable temporary directory; nothing touches the checkout.
@module */
import { mkdir, mkdtemp, readdir, readFile, rm, stat, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, } from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { emptyDir, emptyFile, emptyPath, removeEmptyFilesInDir,} from '@monochromatic-dev/module-fs-path/node';
//region Fixture helpers
/** Disposable temporary directory. */type TempDir = { /** Absolute path of the directory. */ readonly path: string;
/** Removes the directory tree after the test. */ readonly [Symbol.asyncDispose]: () => Promise<void>;};
/** Creates a disposable temporary directory.
@returns directory that removes itself on dispose
@example ```ts await using temp = await createTempDir(); ``` */async function createTempDir(): Promise<TempDir> { /** Fresh directory under the system temporary root. */ const path = await mkdtemp(join( tmpdir(), 'fs-path-empty-', ),); return { path, [Symbol.asyncDispose](): Promise<void> { return rm( path, { force: true, recursive: true, }, ); }, };}
/** Populates a directory with a file, a nested directory holding a file, and an empty nested directory.
@param dir - directory to populate
@example ```ts await populate(temp.path); ``` */async function populate(dir: string,): Promise<void> { await mkdir( join( dir, 'nested', ), { recursive: true, }, ); await mkdir( join( dir, 'hollow', ), ); await writeFile( join( dir, 'top.txt', ), 'top', ); await writeFile( join( dir, 'nested', 'inner.txt', ), 'inner', );}
//endregion Fixture helpers
await describe({ name: '', children: [ describe({ name: emptyDir.name, children: [ it({ name: 'removes every entry, nested directories included, and keeps the directory', fn: async () => { await using temp = await createTempDir(); await populate(temp.path,); expect(await emptyDir(temp.path,),).toBe(temp.path,); expect(await readdir(temp.path,),).toEqual([],); expect((await stat(temp.path,)).isDirectory(),).toBe(true,); }, },), it({ name: 'leaves an already empty directory in place', fn: async () => { await using temp = await createTempDir(); expect(await emptyDir(temp.path,),).toBe(temp.path,); expect(await readdir(temp.path,),).toEqual([],); }, },), ], },),
describe({ name: emptyFile.name, children: [ it({ name: 'truncates the file to zero bytes', fn: async () => { await using temp = await createTempDir(); /** File holding content before the call. */ const target = join( temp.path, 'bundle.js', ); await writeFile( target, 'console.log(1)', ); expect(await emptyFile(target,),).toBe(target,); expect(await readFile( target, 'utf8', ),).toBe('',); }, },), it({ name: 'strips a query suffix before writing and returns the original spelling', fn: async () => { await using temp = await createTempDir(); /** On-disk file the query-bearing spelling refers to. */ const target = join( temp.path, 'asset.css', ); await writeFile( target, 'body{}', ); expect(await emptyFile(`${target}?raw`,),).toBe(`${target}?raw`,); expect(await readFile( target, 'utf8', ),).toBe('',); expect(await readdir(temp.path,),).toEqual(['asset.css',],); }, },), ], },),
describe({ name: emptyPath.name, children: [ it({ name: 'empties a path with an extension as a file', fn: async () => { await using temp = await createTempDir(); /** Extension-bearing path treated as a file. */ const target = join( temp.path, 'out.txt', ); await writeFile( target, 'text', ); expect(await emptyPath(target,),).toBe(target,); expect(await readFile( target, 'utf8', ),).toBe('',); }, },), it({ name: 'empties a path without an extension as a directory', fn: async () => { await using temp = await createTempDir(); /** Extension-less path treated as a directory. */ const target = join( temp.path, 'dist', ); await mkdir(target,); await populate(target,); expect(await emptyPath(target,),).toBe(target,); expect(await readdir(target,),).toEqual([],); }, },), it({ name: 'ignores a query suffix when deciding between file and directory', fn: async () => { await using temp = await createTempDir(); /** Directory whose query-bearing spelling must not read as a file. */ const target = join( temp.path, 'assets', ); await mkdir(target,); await populate(target,); expect(await emptyPath(`${target}?raw`,),).toBe(target,); expect(await readdir(target,),).toEqual([],); }, },), ], },),
describe({ name: removeEmptyFilesInDir.name, children: [ it({ name: 'removes zero-byte and whitespace-only files, keeping content and directories', fn: async () => { await using temp = await createTempDir(); await mkdir(join( temp.path, 'sub', ),); await writeFile( join( temp.path, 'blank.txt', ), '', ); await writeFile( join( temp.path, 'spaces.txt', ), ' \n\t\n', ); await writeFile( join( temp.path, 'full.txt', ), 'content', ); expect(await removeEmptyFilesInDir(temp.path,),).toBe(temp.path,); expect((await readdir(temp.path,)).toSorted(),).toEqual([ 'full.txt', 'sub', ],); }, },), ], },), ],},);