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

feat: implement TestSandbox.writeTextFile #3824

Merged
merged 1 commit into from
Sep 27, 2019
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
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'});
}
}