Skip to content

No stream sent on second call #3

@reggi

Description

@reggi

No stream sent on second call

const mockCli = require('mock-cli')
const {Readable} = require('stream')
const util = require('util')
const bluebird = require('bluebird')

const createReadableStream = (input, callback) => {
  input = input || null
  function ReadableStream (options) {
    Readable.call(this, options)
  }
  util.inherits(ReadableStream, Readable)
  ReadableStream.prototype._read = function (size) {
    if (callback) callback(input)
    this.push(input)
    input = null
  }
  return new ReadableStream()
}

const mockCliWrapper = (argv, stdin) => {
  return mockCli(['node', 'index', ...argv], {
    stdin: createReadableStream(stdin),
    stdout: process.stdout,
    stderr: process.stderr
  }, (error, result) => {
    if (error) throw error
    console.log(result)
  })
}

const test = async (argv, stdin) => {
  var unMockCli = mockCliWrapper(argv, stdin)
  delete require.cache[require.resolve('./index')]
  require('./index')
  await bluebird.delay(0)
  unMockCli()
}

(async () => {
  await test(['World', 'Thomas'], 'Hello World!')
  await test(['America', 'Thomas'], 'Hello America!')
})()

Here's the file I am trying to test.

// #!/usr/bin/env node
const getStdin = require('get-stdin')
const args = process.argv.slice(2)

getStdin().then(str => {
  try {
    if (str === '') return process.exit(0)
    const pattern = new RegExp(args[0], 'gm')
    process.stdout.write(str.replace(pattern, args[1]))
    return process.exit(0)
  } catch (e) {
    return process.exit(1)
  }
}).catch(console.log)

This is logging the following

 ▼     Start of CLI capture        ▼
 Hello Thomas!▲      End of CLI capture           ▲
{ code: 0,
  stdin: 'Hello World!',
  stdout: 'Hello Thomas!',
  stderr: '' }
 ▼     Start of CLI capture        ▼
 ▲      End of CLI capture           ▲

The required module code is not getting the second stdin for some reason.

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