Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(utils): extend fs utils with createCacheReadStream and statCach… #30600

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/util/fs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
cachePathExists,
cachePathIsFile,
chmodLocalFile,
createCacheReadStream,
createCacheWriteStream,
deleteLocalFile,
ensureCacheDir,
Expand All @@ -32,6 +33,7 @@ import {
readSystemFile,
renameLocalFile,
rmCache,
statCacheFile,
statLocalFile,
writeLocalFile,
writeSystemFile,
Expand Down Expand Up @@ -332,6 +334,29 @@ describe('util/fs/index', () => {
});
});

describe('createCacheReadStream', () => {
it('creates read stream', async () => {
const path = `${cacheDir}/file.txt`;
const fileContent = 'foo';
await fs.outputFile(path, fileContent);

const stream = createCacheReadStream('file.txt');
expect(stream).toBeInstanceOf(fs.ReadStream);

let data = '';
stream.on('data', (chunk) => {
data += chunk.toString();
});

await new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});

expect(data).toBe(fileContent);
});
});

describe('localPathIsFile', () => {
it('returns true for file', async () => {
const path = `${localDir}/file.txt`;
Expand Down Expand Up @@ -431,6 +456,17 @@ describe('util/fs/index', () => {
});
});

describe('statCacheFile', () => {
it('returns stat object', async () => {
expect(await statCacheFile('foo')).toBeNull();

await fs.outputFile(`${cacheDir}/foo`, 'foobar');
const stat = await statCacheFile('foo');
expect(stat).toBeTruthy();
expect(stat!.isFile()).toBeTrue();
});
});

describe('listCacheDir', () => {
it('lists directory', async () => {
await fs.outputFile(`${cacheDir}/foo/bar.txt`, 'foobar');
Expand Down
16 changes: 16 additions & 0 deletions lib/util/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ export function createCacheWriteStream(path: string): fs.WriteStream {
return fs.createWriteStream(fullPath);
}

export function createCacheReadStream(path: string): fs.ReadStream {
const fullPath = ensureCachePath(path);
return fs.createReadStream(fullPath);
}

export async function localPathIsFile(pathName: string): Promise<boolean> {
const path = ensureLocalPath(pathName);
try {
Expand Down Expand Up @@ -249,6 +254,17 @@ export async function statLocalFile(
}
}

export async function statCacheFile(
pathName: string,
): Promise<fs.Stats | null> {
const path = ensureCachePath(pathName);
try {
return await fs.stat(path);
} catch (_) {
return null;
}
}

export function listCacheDir(
path: string,
options: { recursive: boolean } = { recursive: false },
Expand Down