<commit_before>import '../src'
import bufferEqual from 'buffer-equal'
import convert from 'convert-source-map'
import gulp from 'gulp'
import timeout from 'timeout-then'
import rump from 'rump'
import {colors} from 'gulp-util'
import {readFile, writeFile} from 'mz/fs'
import {resolve, sep} from 'path'
import {spy} from 'sinon'

const {stripColor} = colors,
      protocol = process.platform === 'win32' ? 'file:///' : 'file://'

describe('tasks', function() {
  this.timeout(0)

  beforeEach(() => {
    rump.configure({
      environment: 'development',
      paths: {
        source: {root: 'test/fixtures', scripts: ''},
        destination: {root: 'tmp', scripts: ''},
      },
    })
  })

  it('are added and defined', () => {
    const callback = spy()
    rump.on('gulp:main', callback)
    rump.on('gulp:scripts', callback)
    rump.addGulpTasks({prefix: 'spec'})
    callback.should.be.calledTwice()
    gulp.tasks['spec:info:scripts'].should.be.ok()
    gulp.tasks['spec:build:scripts'].should.be.ok()
  })

  it('displays correct information in info task', () => {
    const logs = [],
          {log} = console

    console.log = (...args) => logs.push(stripColor(args.join(' ')))
    gulp.start('spec:info')
    console.log = log
    logs.slice(-6).should.eql([
      '',
      '--- Scripts v0.7.0',
      `Processed scripts from test${sep}fixtures are copied with source maps to tmp`,
      'Affected files:',
      'index.js',
      '',
    ])
  })

  describe('for building', () => {
    let originals

    before(async(done) => {
      originals = await Promise.all([
        readFile('test/fixtures/index.js'),
        readFile('test/fixtures/lib/index.js'),
      ])
      gulp.task('postbuild', ['spec:watch'], () => done())
      gulp.start('postbuild')
    })

    afterEach(async() => {
      await timeout(1000)
      await Promise.all([
        writeFile('test/fixtures/index.js', originals[0]),
        writeFile('test/fixtures/lib/index.js', originals[1]),
      ])
      await timeout(1000)
    })

    it('handles updates', async() => {
      const firstContent = await readFile('tmp/index.js')
      let secondContent
      await timeout(1000)
      await writeFile('test/fixtures/lib/index.js', 'module.exports = "";')
      await timeout(1000)
      secondContent = await readFile('tmp/index.js')
      bufferEqual(firstContent, secondContent).should.be.false()
    })

    it('handles source maps in development', async() => {
      const content = await readFile('tmp/index.js'),
            paths = convert
              .fromSource(content.toString())<commit_msg>Code cleanup<commit_after>import '../src'
import bufferEqual from 'buffer-equal'
import convert from 'convert-source-map'
import gulp from 'gulp'
import timeout from 'timeout-then'
import rump from 'rump'
import {colors} from 'gulp-util'
import {readFile, writeFile} from 'mz/fs'
import {resolve, sep} from 'path'
import {spy} from 'sinon'

const protocol = process.platform === 'win32' ? 'file:///' : 'file://',
      {stripColor} = colors

describe('tasks', function() {
  this.timeout(0)

  beforeEach(() => {
    rump.configure({
      environment: 'development',
      paths: {
        source: {root: 'test/fixtures', scripts: ''},
        destination: {root: 'tmp', scripts: ''},
      },
    })
  })

  it('are added and defined', () => {
    const callback = spy()
    rump.on('gulp:main', callback)
    rump.on('gulp:scripts', callback)
    rump.addGulpTasks({prefix: 'spec'})
    callback.should.be.calledTwice()
    gulp.tasks['spec:info:scripts'].should.be.ok()
    gulp.tasks['spec:build:scripts'].should.be.ok()
  })

  it('displays correct information in info task', () => {
    const logs = [],
          {log} = console
    console.log = (...args) => logs.push(stripColor(args.join(' ')))
    gulp.start('spec:info')
    console.log = log
    logs.slice(-6).should.eql([
      '',
      '--- Scripts v0.7.0',
      `Processed scripts from test${sep}fixtures are copied with source maps to tmp`,
      'Affected files:',
      'index.js',
      '',
    ])
  })

  describe('for building', () => {
    let originals

    before(async() => {
      originals = await Promise.all([
        readFile('test/fixtures/index.js'),
        readFile('test/fixtures/lib/index.js'),
      ])
      await new Promise(resolve => {
        gulp.task('postbuild', ['spec:watch'], resolve)
        gulp.start('postbuild')
      })
    })

    beforeEach(() => timeout(1000))

    afterEach(async() => {
      await timeout(1000)
      await Promise.all([
        writeFile('test/fixtures/index.js', originals[0]),
        writeFile('test/fixtures/lib/index.js', originals[1]),
      ])
    })

    it('handles updates', async() => {
      const firstContent = await readFile('tmp/index.js')
      await timeout(1000)
      await writeFile('test/fixtures/lib/index.js', 'module.exports = "";')
      await timeout(1000)
      bufferEqual(firstContent, await readFile('tmp/index.js')).should.be.false()
    })

    it('handles source maps in development', async() => {
      const content = await readFile('tmp/index.js'),
            paths = convert
              .fromSource(content.toString())
{'fraction_non_alphanumeric': 0.17346938775510204, 'fraction_numerical': 0.009626492106276472, 'mean_word_length': 2.848148148148148, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '5672518', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1639, 'n_words': 343}