<commit_before>fs = require 'fs'
path = require 'path'
request = require 'request'
Promise = require 'bluebird'
childProcess = require 'child_process'

executeTests = ({cmd, args}, grunt, done) ->
  testProc = childProcess.spawn(cmd, args)

  testOutput = ""
  testProc.stdout.pipe(process.stdout)
  testProc.stderr.pipe(process.stderr)
  testProc.stdout.on 'data', (data) -> testOutput += data.toString()
  testProc.stderr.on 'data', (data) -> testOutput += data.toString()

  testProc.on 'error', (err) -> grunt.log.error("Process error: #{err}")

  testProc.on 'exit', (exitCode, signal) ->
    if exitCode is 0 then done()
    else notifyOfTestError(testOutput, grunt).then -> done(false)

notifyOfTestError = (testOutput, grunt) -> new Promise (resolve, reject) ->
  if (process.env("TEST_ERROR_HOOK_URL") ? "").length > 0
    testOutput = grunt.log.uncolor(testOutput)
    request.post
      url: process.env("TEST_ERROR_HOOK_URL")
      json:
        username: "Edgehill Builds"
        text: "Aghhh somebody broke the build. ```#{testOutput}```"
    , resolve
  else resolve()

module.exports = (grunt) ->

  grunt.registerTask 'run-spectron-specs', 'Run spectron specs', ->
    shellAppDir = grunt.config.get('nylasGruntConfig.shellAppDir')

    if process.platform is 'darwin'
      executablePath = path.join(shellAppDir, 'Contents', 'MacOS', 'Nylas')
    else
      executablePath = path.join(shellAppDir, 'nylas')

    done = @async()
    npmPath = path.resolve "./build/node_modules/.bin/npm"

    if process.platform is 'win32'
      grunt.log.error("run-spectron-specs only works on Mac OS X at the moment.")
      done(false)

    if not fs.existsSync(executablePath)
      grunt.log.error("run-spectron-specs requires the built version of the app at #{executablePath}")
      done(false)

    process.chdir('./spectron')
    grunt.log.writeln "Current dir: #{process.cwd()}"
    installProc = childProcess.exec "#{npmPath} install", (error) ->
      if error?
        process.chdir('..')
        grunt.log.error('Failed while running npm install in spectron folder')
        grunt.fail.warn(error)
        done(false)
      else
        appArgs = [
          'test'
          "APP_PATH=#{executablePath}"
          "APP_ARGS="
        ]
        executeTests cmd: npmPath, args: appArgs, grunt, (succeeded) ->
          process.chdir('..')
          done(succeeded)


  grunt.registerTask 'run-edgehill-specs', 'Run the specs', ->
    done = @async()
    executeTests cmd: './N1.sh', args: ['--test'], grunt, done<commit_msg>fix(specs): Remove promise usage, process.env as fn to fix node 0.10<commit_after>fs = require 'fs'
path = require 'path'
request = require 'request'
childProcess = require 'child_process'

executeTests = ({cmd, args}, grunt, done) ->
  testProc = childProcess.spawn(cmd, args)

  testOutput = ""
  testProc.stdout.pipe(process.stdout)
  testProc.stderr.pipe(process.stderr)
  testProc.stdout.on 'data', (data) -> testOutput += data.toString()
  testProc.stderr.on 'data', (data) -> testOutput += data.toString()

  testProc.on 'error', (err) -> grunt.log.error("Process error: #{err}")

  testProc.on 'exit', (exitCode, signal) ->
    if exitCode is 0
      done()
    else
      notifyOfTestError testOutput, grunt, ->
        done(false)

notifyOfTestError = (testOutput, grunt, callback) ->
  if (process.env["TEST_ERROR_HOOK_URL"] ? "").length > 0
    testOutput = grunt.log.uncolor(testOutput)
    request.post
      url: process.env["TEST_ERROR_HOOK_URL"]
      json:
        username: "Edgehill Builds"
        text: "Aghhh somebody broke the build. ```#{testOutput}```"
    , callback
  else
    callback()


module.exports = (grunt) ->

  grunt.registerTask 'run-edgehill-specs', 'Run the specs', ->
    done = @async()
    executeTests({cmd: './N1.sh', args: ['--test']}, grunt, done)

  grunt.registerTask 'run-spectron-specs', 'Run spectron specs', ->
    shellAppDir = grunt.config.get('nylasGruntConfig.shellAppDir')

    if process.platform is 'darwin'
      executablePath = path.join(shellAppDir, 'Contents', 'MacOS', 'Nylas')
    else
      executablePath = path.join(shellAppDir, 'nylas')

    done = @async()
    npmPath = path.resolve "./build/node_modules/.bin/npm"

    if process.platform is 'win32'
      grunt.log.error("run-spectron-specs only works on Mac OS X at the moment.")
      done(false)

    if not fs.existsSync(executablePath)
      grunt.log.error("run-spectron-specs requires the built version of the app at #{executablePath}")
      done(false)

    process.chdir('./spectron')
    grunt.log.writeln "Current dir: #{process.cwd()}"
    installProc = childProcess.exec "#{npmPath} install", (error) ->
      if error?
        process.chdir('..')
        grunt.log.error('Failed while running npm install in spectron folder')
        grunt.fail.warn(error)
        done(false)
      else
        appArgs = [
          'test'
          "APP_PATH=#{executablePath}"
          "APP_ARGS="
        ]
        executeTests {cmd: npmPath, args: appArgs}, grunt, (succeeded) ->
          process.chdir('..')
          done(succeeded)
{'fraction_non_alphanumeric': 0.14584964761158967, 'fraction_numerical': 0.0025450274079874707, 'mean_word_length': 3.09375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 30, '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': '1655833', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1644, 'n_words': 388}