Skip to content

Commit

Permalink
handle case where cwd is set to dir created with mkdir (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
amilajack committed Feb 25, 2019
1 parent a1ec00a commit 1be7022
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
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
45 changes: 45 additions & 0 deletions tests/cwd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'path';
import fs from 'fs';
import os from 'os';
import { Joker } from '.';

function mkdirTmpDir() {
const tmpDir = path.join(
os.tmpdir(),
String(Math.floor(Math.random() * 100))
);
if (fs.existsSync(tmpDir)) {
fs.rmdirSync(tmpDir);
}
return tmpDir;
}

describe('Joker#cwd', () => {
it('should accept given path', async () => {
const tmpDir = mkdirTmpDir();
await new Joker()
.cwd(tmpDir)
.run('echo foo')
.end();
});

it('should change allow changing cwd to created dir', async () => {
const tmpDir = mkdirTmpDir();
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');
});
});

0 comments on commit 1be7022

Please sign in to comment.