<commit_before>'use strict'

const spawn = require('cross-spawn')
const path = require('path')

function fixture (file) {
  return path.join(__dirname, 'fixtures', file)
}

function run (args, options) {
  const cli = spawn(path.join(__dirname, '../cli.js'), args, options)
  return new Promise(resolve => {
    let out = ''
    cli.stdout.on('data', data => {
      out += data.toString()
    })
    cli.stderr.on('data', data => {
      out += data.toString()
    })
    cli.on('close', code => {
      resolve({ code, out })
    })
  })
}

it('returns help', () => {
  return run(['--help']).then(result => {
    expect(result.out).toContain('Examples:')
    expect(result.code).toEqual(0)
  })
})

it('returns version', () => {
  return run(['--version']).then(result => {
    expect(result.out).toMatch(/\d+\.\d+\.\d+/)
    expect(result.code).toEqual(0)
  })
})

it('returns size', () => {
  return run(['test/fixtures/empty.js']).then(result => {
    expect(result.out).toEqual('\n' +
    '  Package size: 0 B\n' +
    '  With all dependencies, minified and gzipped\n' +
    '\n')
    expect(result.code).toEqual(0)
  })
})

it('uses different units', () => {
  return run(['test/fixtures/big.js']).then(result => {
    expect(result.out).toContain('2.38 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('supports absolute path', () => {
  return run([path.join(__dirname, 'fixtures/empty.js')]).then(result => {
    expect(result.out).toContain('0 B\n')
    expect(result.code).toEqual(0)
  })
})

it('reads package.json', () => {
  return run([], { cwd: fixture('main/dir') }).then(result => {
    expect(result.out).toContain('22 B\n')
    expect(result.code).toEqual(0)
  })
})

it('uses index.js by default', () => {
  return run([], { cwd: fixture('index/dir') }).then(result => {
    expect(result.out).toContain('20 B\n')
    expect(result.code).toEqual(0)
  })
})

it('shows resolve errors', () => {
  return run(['unknown.js']).then(result => {
    expect(result.out).toContain('unknown.js')
    expect(result.out).toMatch(/^Can't resolve/)
    expect(result.code).toEqual(1)
  })
})

it('shows package.json errors', () => {
  const cwd = path.dirname(path.dirname(__dirname))
  return run([], { cwd }).then(result => {
    expect(result.out).toEqual(
      'Specify project files or run in project dir with package.json\n')
    expect(result.code).toEqual(1)
  })
})

it('shows limit', () => {
  return run(['2KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('shows small K', () => {
  return run(['2kB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows space in limit', () => {
  return run(['2', 'KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows fractional in limit', () => {
  return run(['2.20KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2.2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows unitless limit', () => {
  return run(['2048', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows bytes', () => {
  return run(['2048B', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows space and bytes', () => {
  return run(['2048', 'B', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('checks limits', () => {
  return run(['2KB', 'test/fixtures/big.js']).then(result => {
    expect(result.out).toContain('exceeded the size limit')
    expect(result.code).toEqual(3)
  })
})

it('shows analyzer', () => {
  return run(['--why', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Webpack Bundle Analyzer')
    expect(result.code).toEqual(0)
  })
})

it('uses Babili', () => {
  return run(['--babili', 'test/fixtures/es2016.js']).then(result => {
    expect(result.out).toContain('39 B\n')
    expect(result.code).toEqual(0)
  })
})<commit_msg>Improve first CI detection tests<commit_after>'use strict'

const spawn = require('cross-spawn')
const path = require('path')

function fixture (file) {
  return path.join(__dirname, 'fixtures', file)
}

function run (args, options) {
  if (!options) options = { }
  if (!options.env) {
    options.env = {
      TRAVIS_JOB_NUMBER: '1.1',
      APPVEYOR_JOB_NUMBER: '1'
    }
  }
  const cli = spawn(path.join(__dirname, '../cli.js'), args, options)
  return new Promise(resolve => {
    let out = ''
    cli.stdout.on('data', data => {
      out += data.toString()
    })
    cli.stderr.on('data', data => {
      out += data.toString()
    })
    cli.on('close', code => {
      resolve({ code, out })
    })
  })
}

it('returns help', () => {
  return run(['--help']).then(result => {
    expect(result.out).toContain('Examples:')
    expect(result.code).toEqual(0)
  })
})

it('returns version', () => {
  return run(['--version']).then(result => {
    expect(result.out).toMatch(/\d+\.\d+\.\d+/)
    expect(result.code).toEqual(0)
  })
})

it('returns size', () => {
  return run(['test/fixtures/empty.js']).then(result => {
    expect(result.out).toEqual('\n' +
    '  Package size: 0 B\n' +
    '  With all dependencies, minified and gzipped\n' +
    '\n')
    expect(result.code).toEqual(0)
  })
})

it('uses different units', () => {
  return run(['test/fixtures/big.js']).then(result => {
    expect(result.out).toContain('2.38 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('supports absolute path', () => {
  return run([path.join(__dirname, 'fixtures/empty.js')]).then(result => {
    expect(result.out).toContain('0 B\n')
    expect(result.code).toEqual(0)
  })
})

it('reads package.json', () => {
  return run([], { cwd: fixture('main/dir') }).then(result => {
    expect(result.out).toContain('22 B\n')
    expect(result.code).toEqual(0)
  })
})

it('uses index.js by default', () => {
  return run([], { cwd: fixture('index/dir') }).then(result => {
    expect(result.out).toContain('20 B\n')
    expect(result.code).toEqual(0)
  })
})

it('shows resolve errors', () => {
  return run(['unknown.js']).then(result => {
    expect(result.out).toContain('unknown.js')
    expect(result.out).toMatch(/^Can't resolve/)
    expect(result.code).toEqual(1)
  })
})

it('shows package.json errors', () => {
  const cwd = path.dirname(path.dirname(__dirname))
  return run([], { cwd }).then(result => {
    expect(result.out).toEqual(
      'Specify project files or run in project dir with package.json\n')
    expect(result.code).toEqual(1)
  })
})

it('shows limit', () => {
  return run(['2KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('shows small K', () => {
  return run(['2kB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows space in limit', () => {
  return run(['2', 'KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows fractional in limit', () => {
  return run(['2.20KB', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2.2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows unitless limit', () => {
  return run(['2048', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows bytes', () => {
  return run(['2048B', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('allows space and bytes', () => {
  return run(['2048', 'B', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Size limit:   2 KB\n')
    expect(result.code).toEqual(0)
  })
})

it('checks limits', () => {
  return run(['2KB', 'test/fixtures/big.js']).then(result => {
    expect(result.out).toContain('exceeded the size limit')
    expect(result.code).toEqual(3)
  })
})

it('shows analyzer', () => {
  return run(['--why', 'test/fixtures/empty.js']).then(result => {
    expect(result.out).toContain('Webpack Bundle Analyzer')
    expect(result.code).toEqual(0)
  })
})

it('uses Babili', () => {
  return run(['--babili', 'test/fixtures/es2016.js']).then(result => {
    expect(result.out).toContain('39 B\n')
    expect(result.code).toEqual(0)
  })
})

it('runs only on first job', () => {
  const travis = { TRAVIS: '1', TRAVIS_JOB_NUMBER: '1.2' }
  const appveyor = { APPVEYOR: '1', APPVEYOR_JOB_NUMBER: '2' }
  return Promise.all([
    run([], { env: travis }),
    run([], { env: appveyor })
  ]).then(results => {
    for (const result of results) {
      expect(result.out).toContain('first CI job')
      expect(result.code).toEqual(0)
    }
  })
})
{'fraction_non_alphanumeric': 0.22446797133996363, 'fraction_numerical': 0.014009196877339323, 'mean_word_length': 3.526621490803485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, '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': '4777002', 'n_tokens_mistral': 3467, 'n_tokens_neox': 3414, 'n_words': 547}