Skip to content

Commit

Permalink
Add withCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Apr 25, 2018
1 parent 2f48fa6 commit d8923db
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Object {
"unsafe": [Function],
},
"snapSpawn": Object {
"createSpawnFunc": [Function],
"default": [Function],
"snap": [Function],
"spawn": [Function],
Expand Down
11 changes: 10 additions & 1 deletion packages/typescript/extra-jest/lib/snap-spawn/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {SpawnSyncOptions} from 'child_process'
import * as xsnap from '../snap'
import spawn, {SpawnFunc} from './lib/spawn'
import spawn, {SpawnFunc, createSpawnFunc} from './lib/spawn'

export * from './lib/spawn'

Expand All @@ -13,4 +13,13 @@ export function snap (
return snap(spawn(fn, argv, options))
}

export namespace snap {
export const withCommand = (
command: string,
argv?: string[],
options?: SpawnSyncOptions,
fsnap = xsnap.safe
) => snap(createSpawnFunc(command), argv, options, fsnap)
}

export default snap
13 changes: 12 additions & 1 deletion packages/typescript/extra-jest/lib/snap-spawn/lib/spawn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {SpawnSyncOptions, SpawnSyncReturns} from 'child_process'
import {SpawnSyncOptions, SpawnSyncReturns, spawnSync} from 'child_process'
import ramda from 'ramda'

export type IOData = Buffer | string
export type OptionalIOData = IOData | null | undefined
Expand All @@ -22,6 +23,8 @@ export interface SpawnReturns {

export type SpawnFunc = (argv: string[], options: SpawnSyncOptions) => SpawnSyncReturns<IOData>

export const createSpawnFunc = (command: string): SpawnFunc => ramda.partial(spawnSync, [command])

export function spawn (
fn: SpawnFunc,
argv: string[] = [],
Expand All @@ -41,4 +44,12 @@ export function spawn (
}
}

export namespace spawn {
export const withCommand = (
command: string,
argv?: string[],
options?: SpawnSyncOptions
) => spawn(createSpawnFunc(command), argv, options)
}

export default spawn
2 changes: 2 additions & 0 deletions packages/typescript/extra-jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"version": "0.0.0",
"main": "index.js",
"dependencies": {
"ramda": "^0.25.0",
"fs-extra": "^5.0.0",
"fs-tree-utils": "^0.0.0",
"unique-temp-path": "^0.0.0",
"@types/ramda": "^0.25.24",
"@types/fs-extra": "^5.0.1",
"@types/node": "^9.6.4"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`executes echo 1`] = `
exports[`passing command executes echo 1`] = `
"
error: null
signal: null
status: 0
stderr: ((EMPTY))
stdout: |+
Hello, World!!
"
`;

exports[`passing command executes node 1`] = `
"
error: null
signal: null
status: 0
stderr: ((EMPTY))
stdout: |+
{ abc: 123, def: 456 }
"
`;

exports[`passing functions executes echo 1`] = `
Object {
"arguments": Object {
"argv": Array [
Expand All @@ -13,7 +41,7 @@ Object {
}
`;

exports[`executes echo 2`] = `
exports[`passing functions executes echo 2`] = `
"
error: null
signal: null
Expand All @@ -27,7 +55,7 @@ stdout: |+
"
`;

exports[`executes node 1`] = `
exports[`passing functions executes node 1`] = `
Object {
"arguments": Object {
"argv": Array [],
Expand All @@ -38,7 +66,7 @@ Object {
}
`;

exports[`executes node 2`] = `
exports[`passing functions executes node 2`] = `
"
error: null
signal: null
Expand Down
58 changes: 36 additions & 22 deletions packages/typescript/extra-jest/test/snap-spawn.test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import {spawnSync} from 'child_process'
import snapSpawn from '../../lib/snap-spawn'

it('executes echo', () => snapSpawn(
(argv, options) => {
expect({
arguments: {argv, options}
}).toMatchSnapshot()
describe('passing functions', () => {
it('executes echo', () => snapSpawn(
(argv, options) => {
expect({
arguments: {argv, options}
}).toMatchSnapshot()

return spawnSync('echo', argv, options)
},
['Hello, World!!'],
{encoding: 'utf8'}
)())
return spawnSync('echo', argv, options)
},
['Hello, World!!'],
{encoding: 'utf8'}
)())

it('executes node', () => snapSpawn(
(argv, options) => {
expect({
arguments: {argv, options}
}).toMatchSnapshot()
it('executes node', () => snapSpawn(
(argv, options) => {
expect({
arguments: {argv, options}
}).toMatchSnapshot()

return spawnSync('node', argv, options)
},
[],
{
input: 'console.log({abc: 123, def: 456})'
}
)())
return spawnSync('node', argv, options)
},
[],
{
input: 'console.log({abc: 123, def: 456})'
}
)())
})

describe('passing command', () => {
it(
'executes echo',
snapSpawn.withCommand('echo', ['Hello, World!!'])
)

it(
'executes node',
snapSpawn.withCommand('node', [], {input: 'console.log({abc: 123, def: 456})'})
)
})

0 comments on commit d8923db

Please sign in to comment.