You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current inline-fixtures-files API does not support the creation of binary files. Also, there is no way to modify file metadata such as mode or mtime.
This proposal provides a flexible API for creating various variations of files.
Design
Provides a programmable file creation interface to meet diverse needs
e.g. await createIFF({ 'file.txt': (path) => writeFile(path, Buffer.from([0x00, 0x01])) })
Do not automatically create parent directories.
To allow users to control how parent directories are created as well, parent directories are not automatically created.
Example
import{defineIFFCreator}from'@mizdra/inline-fixture-files';import{writeFile,utimes,cp,symlink,mkdir}from'node:fs/promises';import{constants}from'node:fs';import{dirname,join}from'node:path';constfixtureBaseDir=join(tmpdir(),'your-app-name',process.env['VITEST_POOL_ID']!);constcreateIFF=defineIFFCreator({generateRootDir: ()=>join(fixtureBaseDir,randomUUID()),});// Example: Fileconstiff1=awaitcreateIFF({'buffer.txt': (path)=>writeFile(path,Buffer.from([0x00,0x01])),'encoding.txt': (path)=>writeFile(path,'text',{encoding: 'utf16le'}),'mode.txt': (path)=>writeFile(path,'text',{mode: 0o600}),'flag.txt': (path)=>writeFile(path,'text',{flag: 'wx'}),'utime.txt': async(path)=>{awaitwriteFile(path,'text');awaitutimes(0,0);},'cp.txt': (path)=>cp('./cp.txt',path,{mode: constants.COPYFILE_FICLONE}),'symlink.txt': (path)=>symlink('./symlink.txt',path),// NOTE: The flexible file creation API does not automatically create parent directories.// Therefore, you must manually create the parent directories in order to create nested files.'nested/file.txt': async(path)=>{awaitmkdir(dirname(path));awaitwriteFile(path,'text',{mode: 0o600});},});expectType<{'buffer.txt'': string;'encoding.txt': string;'mode.txt': string;'flag.txt': string;'utime.txt': string;'cp.txt': string;'symlink.txt': string;'nested': string;'nested/file.txt': string;}>(iff1.paths);// Example: Directoryconstiff2=awaitcreateIFF({'mode': (path)=>mkdir(path,{mode: 0o600}),'cp': (path)=>cp('./cp',path,{mode: constants.COPYFILE_FICLONE}),'symlink': (path)=>symlink('./symlink',path),// NOTE: The flexible file creation API does not automatically create parent directories.// Therefore, the recursive option is required to create nested directories.'nested/directory': (path)=>mkdir(path,{mode: 0x600,recursive: true}),}).addFixtures({// Use the add function to add files to the directory created by the flexible file creation API.'mode': {'file1.txt': 'file1',},// If you want to include the paths to the files in the copied directory in `iff2.paths`,// you can use the following hack:'cp': {'file1.txt': ()=>{},// noop},});expectType<{'mode': string;'mode/file1.txt': string;'cp': string;'cp/file1.txt': string;'symlink': string;'nested': string;'nested/directory': string;}>(iff2.paths);
Drawbacks
A runtime error will occur if the user forgets to create the parent directory of the nested file/directory.
constiff1=awaitcreateIFF({// A runtime error occurs that there are no nested directories.'nested/file.txt': (path)=>writeFile(path,'text',{mode: 0o600}),});
This can be confusing to users.
I think it would be desirable to provide the ability to wrap errors and display better warnings to the user.
constiff1=awaitcreateIFF({// IFFParentDirectoryNotFoundError: There is no parent directory (`nested`). Did you forget to create the parent directory?// The flexible file creation API does not automatically create the parent directory, you have to create it manually.'nested/file.txt': (path)=>writeFile(path,'text',{mode: 0o600}),});
The text was updated successfully, but these errors were encountered:
Motiavtion
The current inline-fixtures-files API does not support the creation of binary files. Also, there is no way to modify file metadata such as mode or mtime.
This proposal provides a flexible API for creating various variations of files.
Design
await createIFF({ 'file.txt': (path) => writeFile(path, Buffer.from([0x00, 0x01])) })
Example
Drawbacks
The text was updated successfully, but these errors were encountered: