<commit_before>/* @flow */

import Path from 'path'
import invariant from 'assert'
import { it } from 'jasmine-fix'
import { exec, execNode } from '../src'

const PATH_NODE = Path.join(__dirname, 'fixtures', 'node.js')
const PATH_WAIT = Path.join(__dirname, 'fixtures', 'wait.js')
const PATH_ENV = Path.join(__dirname, 'fixtures', 'env.js')

describe('exec', function() {
  it('works with stdout', async function() {
    const result = await exec(process.execPath, [PATH_NODE])
    expect(result).toBe('STDOUT')
  })

  it('throws if we are expecting on stderr and get output on stdout', async function() {
    try {
      await exec(process.execPath, [PATH_NODE, 'error'])
      expect(false).toBe(true)
    } catch (_) {
      expect(_.message).toBe('STDERR')
    }
  })

  it("doesn't mind when we are expecting on stderr though", async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { stream: 'stderr' })
    expect(result).toBe('STDERR')
  })

  it('also supports giving out both streams at once', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { stream: 'both' })
    invariant(typeof result === 'object' && result)
    expect(result.stderr).toBe('STDERR')
    expect(result.stdout).toBe('STDOUT')
  })

  it('passes on stdin properly', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'input'], { stdin: 'hello dolly' })
    expect(result).toBe('STDOUThello dolly')
  })

  it('passes on stdin Buffers properly', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'input'], { stdin: new Buffer('Wakey Wakey') })
    expect(result).toBe('STDOUTWakey Wakey')
  })

  it('ignores stderr if throwOnStderr is specified', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { throwOnStderr: false })
    expect(result).toBe('STDOUT')
  })

  it('acts like a good boy if script terminates before timeout', async function() {
    const result = await exec(process.execPath, [PATH_WAIT, '1000'], { timeout: 1500 })
    expect(result).toBe('PASSED')
  })

  it('terminates the script and errors if the process times out', async function() {
    try {
      await exec(process.execPath, [PATH_WAIT, '2000'], { timeout: 1000 })
      expect(false).toBe(true)<commit_msg>:new: Use the new buffer creation method<commit_after>/* @flow */

import Path from 'path'
import invariant from 'assert'
import { it } from 'jasmine-fix'
import { exec, execNode } from '../src'

const PATH_NODE = Path.join(__dirname, 'fixtures', 'node.js')
const PATH_WAIT = Path.join(__dirname, 'fixtures', 'wait.js')
const PATH_ENV = Path.join(__dirname, 'fixtures', 'env.js')

describe('exec', function() {
  it('works with stdout', async function() {
    const result = await exec(process.execPath, [PATH_NODE])
    expect(result).toBe('STDOUT')
  })

  it('throws if we are expecting on stderr and get output on stdout', async function() {
    try {
      await exec(process.execPath, [PATH_NODE, 'error'])
      expect(false).toBe(true)
    } catch (_) {
      expect(_.message).toBe('STDERR')
    }
  })

  it("doesn't mind when we are expecting on stderr though", async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { stream: 'stderr' })
    expect(result).toBe('STDERR')
  })

  it('also supports giving out both streams at once', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { stream: 'both' })
    invariant(typeof result === 'object' && result)
    expect(result.stderr).toBe('STDERR')
    expect(result.stdout).toBe('STDOUT')
  })

  it('passes on stdin properly', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'input'], { stdin: 'hello dolly' })
    expect(result).toBe('STDOUThello dolly')
  })

  it('passes on stdin Buffers properly', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'input'], { stdin: Buffer.from('Wakey Wakey') })
    expect(result).toBe('STDOUTWakey Wakey')
  })

  it('ignores stderr if throwOnStderr is specified', async function() {
    const result = await exec(process.execPath, [PATH_NODE, 'error'], { throwOnStderr: false })
    expect(result).toBe('STDOUT')
  })

  it('acts like a good boy if script terminates before timeout', async function() {
    const result = await exec(process.execPath, [PATH_WAIT, '1000'], { timeout: 1500 })
    expect(result).toBe('PASSED')
  })

  it('terminates the script and errors if the process times out', async function() {
    try {
      await exec(process.execPath, [PATH_WAIT, '2000'], { timeout: 1000 })
      expect(false).toBe(true)
{'fraction_non_alphanumeric': 0.15369897959183673, 'fraction_numerical': 0.006802721088435374, 'mean_word_length': 4.176017601760176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4771010', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1437, 'n_words': 418}