Skip to content

FEAT: add an optional stdin: string parameter to execute #4

Description

@vassudanagunta

A lot of CLI commands follow the Unix philosophy and read stdin for default input and write output to stdout by default.

cli-testing-library already makes it easy to test the latter case as the execute API provides access stdout and stderr. But to test a simple CLI command that reads stdin, one is forced to use the spawn API. This is ok, but it would very nice if an optional stdin: string parameter were supported as follows:

it('program runs successfully', async () => {
    const { execute, cleanup } = await prepareEnvironment()
    const { code, stdout, stderr } = await execute(
        'node',
        './cli.js',
        'Hello? (Hello, hello, hello) / Is there anybody in there?'
    )
    expect(stdout).toBe(['Just nod if you can hear me / Is there anyone home?'])
    await cleanup()
});

I know this would clash with the existing optional runFrom parameter. Since your are in early alpha now might be a good time to switch a different way for users to pass in optional params, e.g. an options object, e.g.:

const { code, stdout, stderr } = await execute(
    'node',
    './cli.js',
    {stdin: 'Hello? (Hello, hello, hello) / Is there anybody in there?',
     runFrom: './testContext'}
)

Another alternative:

const { code, stdin, stdout, stderr } = await execute(
    'node',
    './cli.js'
)

// writes to and immediately closes stdin:
stdin('Hello? (Hello, hello, hello) / Is there anybody in there?')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions