Skip to content

Commit

Permalink
fix(test): begin covering child process utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Mar 31, 2020
1 parent cdf1acd commit f690cdc
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 46 deletions.
11 changes: 0 additions & 11 deletions docs/api/js-utils.childoptions.cwd.md

This file was deleted.

11 changes: 0 additions & 11 deletions docs/api/js-utils.childoptions.env.md

This file was deleted.

5 changes: 1 addition & 4 deletions docs/api/js-utils.childoptions.md
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export interface ChildOptions
export interface ChildOptions extends ChildProcessOptions
```
## Properties
Expand All @@ -16,7 +16,4 @@ export interface ChildOptions
| --- | --- | --- |
| [args](./js-utils.childoptions.args.md) | <code>Array&lt;string&gt;</code> | |
| [command](./js-utils.childoptions.command.md) | <code>string</code> | |
| [cwd](./js-utils.childoptions.cwd.md) | <code>string</code> | |
| [env](./js-utils.childoptions.env.md) | <code>Array&lt;NameValuePair&lt;string&gt;&gt;</code> | |
| [timeout](./js-utils.childoptions.timeout.md) | <code>number</code> | |
11 changes: 0 additions & 11 deletions docs/api/js-utils.childoptions.timeout.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/api/js-utils.childspawner.md
Expand Up @@ -7,5 +7,5 @@
<b>Signature:</b>

```typescript
export declare type ChildSpawner = typeof spawn;
export declare type ChildSpawner = (command: string, args: Array<string>, options: Partial<ChildProcessOptions>) => ChildStreams;
```
4 changes: 2 additions & 2 deletions docs/api/js-utils.waitforchild.md
Expand Up @@ -7,14 +7,14 @@
<b>Signature:</b>

```typescript
export declare function waitForChild(child: ChildProcessWithoutNullStreams): Promise<ChildResult>;
export declare function waitForChild(child: ChildStreams): Promise<ChildResult>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| child | <code>ChildProcessWithoutNullStreams</code> | |
| child | <code>ChildStreams</code> | |

<b>Returns:</b>

Expand Down
21 changes: 15 additions & 6 deletions src/utils/Child.ts
@@ -1,4 +1,4 @@
import { ChildProcessWithoutNullStreams, spawn } from 'child_process';
import { ChildProcessWithoutNullStreams, spawn, ChildProcess } from 'child_process';
import { BaseError } from 'noicejs';
import { Writable } from 'stream';

Expand All @@ -7,28 +7,37 @@ import { ChildProcessError } from '../error/ChildProcessError';
import { encode } from './Buffer';
import { NameValuePair } from './Map';

export interface ChildOptions {
args: Array<string>;
command: string;
export interface ChildProcessOptions {
cwd: string;
env: Array<NameValuePair<string>>;
timeout: number;
}

export interface ChildOptions extends ChildProcessOptions {
args: Array<string>;
command: string;
}

export interface ChildResult {
status: number;
stderr: string;
stdout: string;
}

export type ChildSpawner = typeof spawn;
export type ChildStreams = ChildProcessWithoutNullStreams;

export type ChildSpawner = (
command: string,
args: Array<string>,
options: Partial<ChildProcessOptions>
) => ChildStreams;

const CHILD_ENCODING = 'utf-8';
const CHILD_EVENT = 'child process emitted error event';
const CHILD_STATUS = 'child process exited with error status';
const CHILD_OUTPUT = 'child process emitted error output';

export function waitForChild(child: ChildProcessWithoutNullStreams): Promise<ChildResult> {
export function waitForChild(child: ChildStreams): Promise<ChildResult> {
return new Promise((res, rej) => {
const stderr: Array<Buffer> = [];
const stdout: Array<Buffer> = [];
Expand Down
56 changes: 56 additions & 0 deletions test/utils/TestChild.ts
@@ -0,0 +1,56 @@
import { expect } from 'chai';

import { mustExist, Optional } from '../../src/utils';
import { ChildStreams, waitForChild } from '../../src/utils/Child';
import { describeLeaks, itLeaks } from '../helpers/async';
import { ChildProcessError } from '../../src';

type Closer = (status: number) => Promise<void>;

function createChild(): ChildStreams & { closer: Optional<Closer> } {
return {
closer /* Optional<Closer> */ : undefined,
on(event: string, fn: Closer) {
if (event === 'close') {
this.closer = fn;
}
},
stderr: {
on() {
/* noop */
},
},
stdout: {
on() {
/* noop */
},
},
/* eslint-disable @typescript-eslint/no-explicit-any */
} as any;
}

describeLeaks('child process utils', async () => {
describeLeaks('wait for child helper', async () => {
itLeaks('should read stdout data', async () => {
const child = createChild();

const resultPromise = waitForChild(child);
await mustExist(child.closer)(0);

const result = await resultPromise;
expect(result.status).to.equal(0);
});

itLeaks('should read stderr data');
itLeaks('should resolve on success status');

itLeaks('should reject on failure status', async () => {
const child = createChild();

const resultPromise = waitForChild(child);
await mustExist(child.closer)(1);

return expect(resultPromise).to.eventually.be.rejectedWith(ChildProcessError);
});
});
});

0 comments on commit f690cdc

Please sign in to comment.