Skip to content

Commit 59ca3a0

Browse files
Hage Yaapahacksparrow
authored andcommitted
feat: implement TestSandbox.writeTextFile
Add `writeTextFile` method to `TestSandbox`
1 parent ce1f4b7 commit 59ca3a0

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/testlab/src/__tests__/integration/test-sandbox.integration.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ describe('TestSandbox integration tests', () => {
7373
expect(content).to.equal('{\n "key": "value"\n}\n');
7474
});
7575

76+
it('creates a text file in the sandbox', async () => {
77+
await sandbox.writeTextFile('data.txt', 'Hello');
78+
const fullPath = resolve(path, 'data.txt');
79+
expect(await pathExists(fullPath)).to.be.True();
80+
const content = await readFile(fullPath, 'utf-8');
81+
expect(content).to.equal('Hello');
82+
});
83+
7684
it('resets the sandbox', async () => {
7785
const file = 'test.js';
7886
const resolvedFile = resolve(__dirname, '../fixtures/test.js');

packages/testlab/src/test-sandbox.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ensureDirSync,
1212
pathExists,
1313
remove,
14+
writeFile,
1415
writeJson,
1516
} from 'fs-extra';
1617
import {parse, resolve} from 'path';
@@ -119,4 +120,17 @@ export class TestSandbox {
119120
await ensureDir(destDir);
120121
return writeJson(dest, data, {spaces: 2});
121122
}
123+
124+
/**
125+
* Creates a new file and writes the given data as a UTF-8-encoded text.
126+
*
127+
* @param dest - Destination filename, optionally including a relative path.
128+
* @param data - The text to write.
129+
*/
130+
async writeTextFile(dest: string, data: string): Promise<void> {
131+
dest = resolve(this.path, dest);
132+
const destDir = parse(dest).dir;
133+
await ensureDir(destDir);
134+
return writeFile(dest, data, {encoding: 'utf-8'});
135+
}
122136
}

0 commit comments

Comments
 (0)