Skip to content

Commit 601df73

Browse files
iclantonclaude
andcommitted
Add unit tests for FileSystem.createReadStream/createWriteStream
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 67c5bb8 commit 601df73

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

libraries/node-core-library/src/test/FileSystem.test.ts

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22
// See LICENSE in the project root for license information.
33

4+
import * as nodeJsOs from 'node:os';
45
import fs from 'node:fs';
56

67
import { FileSystem } from '../FileSystem';
@@ -51,4 +52,141 @@ describe(FileSystem.name, () => {
5152
}
5253
});
5354
});
55+
56+
describe(FileSystem.createReadStream.name, () => {
57+
let tempDir: string;
58+
59+
beforeEach(() => {
60+
tempDir = fs.mkdtempSync(`${nodeJsOs.tmpdir()}/filesystem-test-`);
61+
});
62+
63+
afterEach(() => {
64+
fs.rmSync(tempDir, { recursive: true, force: true });
65+
});
66+
67+
test('returns a readable stream for an existing file', (done) => {
68+
const filePath: string = `${tempDir}/test.txt`;
69+
fs.writeFileSync(filePath, 'hello world');
70+
71+
const stream: fs.ReadStream = FileSystem.createReadStream(filePath);
72+
const chunks: Buffer[] = [];
73+
74+
stream.on('data', (chunk: string | Buffer) => {
75+
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
76+
});
77+
78+
stream.on('end', () => {
79+
const result: string = Buffer.concat(chunks).toString();
80+
expect(result).toBe('hello world');
81+
done();
82+
});
83+
84+
stream.on('error', done);
85+
});
86+
87+
test('stream emits an error for a nonexistent file', (done) => {
88+
const filePath: string = `${tempDir}/nonexistent.txt`;
89+
const stream: fs.ReadStream = FileSystem.createReadStream(filePath);
90+
91+
stream.on('error', (error: NodeJS.ErrnoException) => {
92+
expect(error.code).toBe('ENOENT');
93+
done();
94+
});
95+
});
96+
});
97+
98+
describe(FileSystem.createWriteStream.name, () => {
99+
let tempDir: string;
100+
101+
beforeEach(() => {
102+
tempDir = fs.mkdtempSync(`${nodeJsOs.tmpdir()}/filesystem-test-`);
103+
});
104+
105+
afterEach(() => {
106+
fs.rmSync(tempDir, { recursive: true, force: true });
107+
});
108+
109+
test('creates a writable stream that writes data to a file', (done) => {
110+
const filePath: string = `${tempDir}/output.txt`;
111+
const stream: fs.WriteStream = FileSystem.createWriteStream(filePath);
112+
113+
stream.write('hello ');
114+
stream.write('world');
115+
stream.end(() => {
116+
const result: string = fs.readFileSync(filePath, 'utf-8');
117+
expect(result).toBe('hello world');
118+
done();
119+
});
120+
121+
stream.on('error', done);
122+
});
123+
124+
test('emits an error when the parent folder does not exist and ensureFolderExists is not set', (done) => {
125+
const filePath: string = `${tempDir}/nonexistent-folder/output.txt`;
126+
const stream: fs.WriteStream = FileSystem.createWriteStream(filePath);
127+
128+
stream.on('error', (error: NodeJS.ErrnoException) => {
129+
expect(error.code).toBe('ENOENT');
130+
done();
131+
});
132+
});
133+
134+
test('creates the parent folder when ensureFolderExists is true', (done) => {
135+
const filePath: string = `${tempDir}/new-folder/output.txt`;
136+
const stream: fs.WriteStream = FileSystem.createWriteStream(filePath, {
137+
ensureFolderExists: true
138+
});
139+
140+
stream.write('test data');
141+
stream.end(() => {
142+
const result: string = fs.readFileSync(filePath, 'utf-8');
143+
expect(result).toBe('test data');
144+
done();
145+
});
146+
147+
stream.on('error', done);
148+
});
149+
});
150+
151+
describe('createWriteStreamAsync', () => {
152+
let tempDir: string;
153+
154+
beforeEach(() => {
155+
tempDir = fs.mkdtempSync(`${nodeJsOs.tmpdir()}/filesystem-test-`);
156+
});
157+
158+
afterEach(() => {
159+
fs.rmSync(tempDir, { recursive: true, force: true });
160+
});
161+
162+
test('creates a writable stream that writes data to a file', async () => {
163+
const filePath: string = `${tempDir}/output.txt`;
164+
const stream: fs.WriteStream = await FileSystem.createWriteStreamAsync(filePath);
165+
166+
await new Promise<void>((resolve, reject) => {
167+
stream.on('error', reject);
168+
stream.write('async hello');
169+
stream.end(() => resolve());
170+
});
171+
172+
const result: string = fs.readFileSync(filePath, 'utf-8');
173+
expect(result).toBe('async hello');
174+
});
175+
176+
test('creates the parent folder when ensureFolderExists is true', async () => {
177+
const filePath: string = `${tempDir}/new-folder/output.txt`;
178+
const stream: fs.WriteStream = await FileSystem.createWriteStreamAsync(filePath, {
179+
ensureFolderExists: true
180+
});
181+
182+
await new Promise<void>((resolve, reject) => {
183+
stream.on('error', reject);
184+
stream.write('async test data');
185+
stream.end(() => resolve());
186+
});
187+
188+
const result: string = fs.readFileSync(filePath, 'utf-8');
189+
expect(result).toBe('async test data');
190+
});
191+
});
54192
});

0 commit comments

Comments
 (0)