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

Update system-extension.test.ts #789

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
126 changes: 62 additions & 64 deletions src/core-extensions/system-extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
import * as expect from 'expect'
import { platform } from 'os'
import { Toolbox } from '../domain/toolbox'
import create from './system-extension'

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))

const toolbox = new Toolbox()
create(toolbox)
const system = toolbox.system

test('survives the factory function', () => {
expect(system).toBeTruthy()
expect(typeof system.run).toBe('function')
})

test('captures stdout', async () => {
const stdout = await system.run(`${platform() === 'win32' ? 'dir /S /B' : 'ls'} ${__filename}`)
expect(stdout).toContain(__filename)
})

test('captures stderr', async () => {
expect.assertions(1)
try {
await system.run(`omgdontrunlol ${__filename}`)
} catch (e) {
expect(/not (found|recognized)/.test(e.stderr)).toBe(true)
}
})

test('knows about which', () => {
const npm = system.which('npm')
expect(npm).toBeTruthy()
})

test('can spawn and capture results', async () => {
const good = await system.spawn('echo hello')
expect(good.status).toBe(0)
expect(good.stdout.toString()).toEqual(expect.stringMatching(/"?hello"?\w*/))
})

test('spawn deals with missing programs', async () => {
const crap = await system.spawn('dfsjkajfkldasjklfajsd')
expect(crap.error).toBeTruthy()
expect(crap.output).toBeFalsy()
expect(crap.status).toBe(null)
})

test('spawn deals exit codes', async () => {
const crap = await system.spawn('npm')
expect(crap.error).toBeFalsy()
expect(crap.status).toBe(1)
})

test('start timer returns the number of milliseconds', async () => {
const WAIT = 10

const elapsed = system.startTimer() // start a timer
await delay(WAIT) // simulate a delay
const duration = elapsed() // how long was that?

// due to rounding this can be before the timeout.
expect(duration >= WAIT - 1).toBe(true)
})
import * as expect from 'expect';
import { platform } from 'os';
import { Toolbox } from '../domain/toolbox';
import create from './system-extension';

const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const toolbox = new Toolbox();
create(toolbox);
const system = toolbox.system;

describe('System Operations', () => {
test('Factory Function', () => {
expect(system).toBeTruthy();
expect(system.run).toBeInstanceOf(Function);
});

test('Capturing stdout', async () => {
const stdout = await system.run(`${platform() === 'win32' ? 'dir /S /B' : 'ls'} ${__filename}`);
expect(stdout).toContain(__filename);
});

test('Capturing stderr', async () => {
expect.assertions(1);
try {
await system.run(`omgdontrunlol ${__filename}`);
} catch (e) {
expect(e.stderr).toMatch(/not (found|recognized)/);
}
});

test('Checking for program existence', () => {
const npm = system.which('npm');
expect(npm).toBeTruthy();
});

test('Spawning and capturing results', async () => {
const good = await system.spawn('echo hello');
expect(good.status).toBe(0);
expect(good.stdout.toString()).toMatch(/hello/);
});

test('Handling missing programs during spawn', async () => {
const crap = await system.spawn('dfsjkajfkldasjklfajsd').catch(err => err);
expect(crap.error).toBeTruthy();
expect(crap.output).toBeFalsy();
expect(crap.status).toBeNull();
});

test('Handling exit codes during spawn', async () => {
const crap = await system.spawn('npm');
expect(crap.status).toBe(1);
});

test('Testing start timer function', async () => {
const WAIT = 10;
const start = Date.now();
await delay(WAIT);
const duration = Date.now() - start;
expect(duration).toBeGreaterThanOrEqual(WAIT - 1);
});
});