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

handle case where cwd is set to dir created with mkdir #25

Merged
merged 1 commit into from
Feb 25, 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
2 changes: 1 addition & 1 deletion src/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class Batch {
function next(): void {
const latestFn = batch.shift();
if (!latestFn) return;
if (latestFn.length) {
if (latestFn.length && typeof latestFn === 'function') {
latestFn(next);
return;
}
Expand Down
10 changes: 6 additions & 4 deletions src/expectations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ function error(
if (actual) err.actual = actual;

// Use expect to show diffs between actual and expected values
try {
expect(actual).toEqual(expected);
} catch (e) {
console.log(e.message);
if (typeof expected === 'string') {
try {
expect(actual).toEqual(expected);
} catch (e) {
console.log(e.message);
}
}

// Show the stdout if the command errored
Expand Down
10 changes: 8 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import clone from 'clone';
import { spawn } from 'child_process';
import AssertionError from 'assertion-error';
Expand Down Expand Up @@ -210,8 +211,13 @@ export default class Runner {
* @returns instance for chaining
*/

public cwd(path: string): Runner {
this.environment.cwd = path;
public cwd(cwdPath: string): Runner {
this.batch.add(() => {
if (!fs.existsSync(cwdPath)) {
throw new Error(`The path "${cwdPath}" does not exist`);
}
this.environment.cwd = cwdPath;
});
return this;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/cwd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path';
import fs from 'fs';
import os from 'os';
import { Joker } from '.';

describe('Joker#cwd', () => {
it('should accept given path', async () => {
const tmpDir = path.join(
os.tmpdir(),
String(Math.floor(Math.random() * 100))
);
fs.mkdirSync(tmpDir);
await new Joker()
.cwd(tmpDir)
.run('echo foo')
.end();
});

it('should change allow changing cwd to created dir', async () => {
const tmpDir = path.join(
os.tmpdir(),
String(Math.floor(Math.random() * 100))
);
expect(fs.existsSync(tmpDir)).toBe(false);
await new Joker()
.mkdir(tmpDir)
.cwd(tmpDir)
.run('echo foo')
.rmdir(tmpDir)
.end();
expect(fs.existsSync(tmpDir)).toBe(false);
});

it('should fail if given path does not exist', async () => {
await expect(
new Joker()
.cwd('non_existent_path')
.run('echo foo')
.end()
).rejects.toThrow('The path "non_existent_path" does not exist');
});
});