Skip to content

Commit

Permalink
test main in optional lines
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton committed Jul 9, 2024
1 parent 1d67c6d commit f1164cb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ export function parse (args: Iterable<string>): Flags {
export async function main ({

args = argv.slice(2),
input = stdin,
lines: optional_lines,
print = console.log,
quit = exit,

}: {

args?: Iterable<string>,
input?: NodeJS.ReadableStream,
lines?: AsyncIterable<string>,
// deno-lint-ignore no-explicit-any
print?: Fn<any, void>,
Expand All @@ -79,7 +81,7 @@ export async function main ({
}

const flags = parse(args);
const lines = optional_lines ?? createInterface({ input: stdin });
const lines = optional_lines ?? createInterface({ input });

let code = 0;

Expand Down
28 changes: 28 additions & 0 deletions tests/cli/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, it } from '@std/testing/bdd';
import * as asserts from '@std/assert';
import * as mock from '@std/testing/mock';

import { Readable } from 'node:stream';

import { make_lines } from '../utils.ts';

import {
Expand Down Expand Up @@ -39,6 +41,32 @@ describe('main', function () {

}

it('ok in optional lines with input of ReadableStream', async function () {

const args = [ '--extra', 'acorn-jsx' ];

const input = Readable.from(`
"version": "1.2.3",
"lockfileVersion": 2,
"node_modules/lodash.memoize": {
"node_modules/acorn-jsx": {
"node_modules/side-channel": {
`);

const print = mock.spy(() => {});

const quit = mock.spy(() => {});

await main({ args, input, print, quit });

mock.assertSpyCallArg(print, 0, 0, 'acorn-jsx');
mock.assertSpyCalls(print, 1);

mock.assertSpyCallArg(quit, 0, 0, 1);
mock.assertSpyCalls(quit, 1);

});

it('exit by 0 with no matches', async function () {

const args = [ '--extra', 'wat' ];
Expand Down

0 comments on commit f1164cb

Please sign in to comment.