Skip to content

Commit

Permalink
feat: implement TestSandbox.writeTextFile
Browse files Browse the repository at this point in the history
Add `writeTextFile` method to `TestSandbox`
  • Loading branch information
Hage Yaapa authored and hacksparrow committed Sep 27, 2019
1 parent ce1f4b7 commit 59ca3a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ describe('TestSandbox integration tests', () => {
expect(content).to.equal('{\n "key": "value"\n}\n');
});

it('creates a text file in the sandbox', async () => {
await sandbox.writeTextFile('data.txt', 'Hello');
const fullPath = resolve(path, 'data.txt');
expect(await pathExists(fullPath)).to.be.True();
const content = await readFile(fullPath, 'utf-8');
expect(content).to.equal('Hello');
});

it('resets the sandbox', async () => {
const file = 'test.js';
const resolvedFile = resolve(__dirname, '../fixtures/test.js');
Expand Down
14 changes: 14 additions & 0 deletions packages/testlab/src/test-sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ensureDirSync,
pathExists,
remove,
writeFile,
writeJson,
} from 'fs-extra';
import {parse, resolve} from 'path';
Expand Down Expand Up @@ -119,4 +120,17 @@ export class TestSandbox {
await ensureDir(destDir);
return writeJson(dest, data, {spaces: 2});
}

/**
* Creates a new file and writes the given data as a UTF-8-encoded text.
*
* @param dest - Destination filename, optionally including a relative path.
* @param data - The text to write.
*/
async writeTextFile(dest: string, data: string): Promise<void> {
dest = resolve(this.path, dest);
const destDir = parse(dest).dir;
await ensureDir(destDir);
return writeFile(dest, data, {encoding: 'utf-8'});
}
}

0 comments on commit 59ca3a0

Please sign in to comment.