<commit_before>var fs = require('fs')
  , child = require('child_process')
  , test = require('tap').test
  , touch = require('touch')

var dir = __dirname +  '/fixture'
  , bin = __dirname + '/../bin/node-dev'
  , msgFile = dir + '/message.js'

// Constants
var MESSAGE = fs.readFileSync(msgFile).toString()

// Helpers
function touchFile() {
  touch.sync(msgFile);
}

function spawn(cmd, cb) {
  var ps = child.spawn('node', [bin].concat(cmd.split(' ')), { cwd: dir })
    , out = ''

  if (cb) ps.stdout.on('data', function(data) {
    out += data.toString()
    var ret = cb.call(ps, out)
    if (typeof ret == 'function') cb = ret
    else if (ret && ret.exit) {
      ps.stdout.removeAllListeners('data')
      ps.on('exit', function() { setTimeout(ret.exit, 1000) })
      ps.kill()
    }
  })
  return ps
}

function run(cmd, done) {
  spawn(cmd, function(out) {
    if (out.match(/touch message.js/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: done }
        }
      }
    }
  })
}

// Tests
test('should restart the server', function(t) {
  run('server.js', t.end.bind(t))
})

test('should restart the server twice', function(t) {
  spawn('server.js', function(out) {
    if (out.match(/touch message.js/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          setTimeout(touchFile, 500)
          return function(out) {
            if (out.match(/Restarting/)) {
              return { exit: t.end.bind(t) }
            }
          }
        }
      }
    }
  })
})

test('should restart the cluster', function(t) {
  run('cluster.js', t.end.bind(t))
})

test('should support vm functions', function(t) {
  run('vmtest.js', t.end.bind(t))
})

test('should support coffee-script', function(t) {
  run('server.coffee', t.end.bind(t))
})

test('should restart when a file is renamed', function(t) {
  var tmp = dir + '/message.tmp'
  fs.writeFileSync(tmp, MESSAGE)
  spawn('log.js', function(out) {
    if (out.match(/touch message.js/)) {
      fs.renameSync(tmp, msgFile)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: t.end.bind(t) }
        }
      }
    }
  })
})

test('should handle errors', function(t) {
  spawn('error.js', function(out) {
    if (out.match(/ERROR/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: t.end.bind(t) }
        }
      }
    }
  })
})

test('should watch if no such module', function(t) {
  spawn('noSuchModule.js', function(out) {
    t.like(out, /ERROR/)
    return { exit: t.end.bind(t) }
  })
})

test('should ignore caught errors', function(t) {
  spawn('catchNoSuchModule.js', function(out) {
    t.like(out, /Caught/)
    return { exit: t.end.bind(t) }
  })
})

test('should not show up in argv', function(t) {
  spawn('argv.js foo', function(out) {
    var argv = JSON.parse(out.replace(/'/g, '"'))
    t.like(argv[0], /.*?node(js|\.exe)?$/)
    t.is(argv[1], 'argv.js')
    t.is(argv[2], 'foo')
    return { exit: t.end.bind(t) }
  })
})

test('should pass through the exit code', function(t) {
  spawn('exit.js').on('exit', function(code) {
    t.is(code, 101)
    t.end()
  })
})

test('should conceil the wrapper', function(t) {
  // require.main should be main.js not wrap.js!
  spawn('main.js').on('exit', function(code) {<commit_msg>Fix typo: conceil --> conceal<commit_after>var fs = require('fs')
  , child = require('child_process')
  , test = require('tap').test
  , touch = require('touch')

var dir = __dirname +  '/fixture'
  , bin = __dirname + '/../bin/node-dev'
  , msgFile = dir + '/message.js'

// Constants
var MESSAGE = fs.readFileSync(msgFile).toString()

// Helpers
function touchFile() {
  touch.sync(msgFile);
}

function spawn(cmd, cb) {
  var ps = child.spawn('node', [bin].concat(cmd.split(' ')), { cwd: dir })
    , out = ''

  if (cb) ps.stdout.on('data', function(data) {
    out += data.toString()
    var ret = cb.call(ps, out)
    if (typeof ret == 'function') cb = ret
    else if (ret && ret.exit) {
      ps.stdout.removeAllListeners('data')
      ps.on('exit', function() { setTimeout(ret.exit, 1000) })
      ps.kill()
    }
  })
  return ps
}

function run(cmd, done) {
  spawn(cmd, function(out) {
    if (out.match(/touch message.js/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: done }
        }
      }
    }
  })
}

// Tests
test('should restart the server', function(t) {
  run('server.js', t.end.bind(t))
})

test('should restart the server twice', function(t) {
  spawn('server.js', function(out) {
    if (out.match(/touch message.js/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          setTimeout(touchFile, 500)
          return function(out) {
            if (out.match(/Restarting/)) {
              return { exit: t.end.bind(t) }
            }
          }
        }
      }
    }
  })
})

test('should restart the cluster', function(t) {
  run('cluster.js', t.end.bind(t))
})

test('should support vm functions', function(t) {
  run('vmtest.js', t.end.bind(t))
})

test('should support coffee-script', function(t) {
  run('server.coffee', t.end.bind(t))
})

test('should restart when a file is renamed', function(t) {
  var tmp = dir + '/message.tmp'
  fs.writeFileSync(tmp, MESSAGE)
  spawn('log.js', function(out) {
    if (out.match(/touch message.js/)) {
      fs.renameSync(tmp, msgFile)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: t.end.bind(t) }
        }
      }
    }
  })
})

test('should handle errors', function(t) {
  spawn('error.js', function(out) {
    if (out.match(/ERROR/)) {
      setTimeout(touchFile, 500)
      return function(out) {
        if (out.match(/Restarting/)) {
          return { exit: t.end.bind(t) }
        }
      }
    }
  })
})

test('should watch if no such module', function(t) {
  spawn('noSuchModule.js', function(out) {
    t.like(out, /ERROR/)
    return { exit: t.end.bind(t) }
  })
})

test('should ignore caught errors', function(t) {
  spawn('catchNoSuchModule.js', function(out) {
    t.like(out, /Caught/)
    return { exit: t.end.bind(t) }
  })
})

test('should not show up in argv', function(t) {
  spawn('argv.js foo', function(out) {
    var argv = JSON.parse(out.replace(/'/g, '"'))
    t.like(argv[0], /.*?node(js|\.exe)?$/)
    t.is(argv[1], 'argv.js')
    t.is(argv[2], 'foo')
    return { exit: t.end.bind(t) }
  })
})

test('should pass through the exit code', function(t) {
  spawn('exit.js').on('exit', function(code) {
    t.is(code, 101)
    t.end()
  })
})

test('should conceal the wrapper', function(t) {
  // require.main should be main.js not wrap.js!
  spawn('main.js').on('exit', function(code) {
{'fraction_non_alphanumeric': 0.18980414746543778, 'fraction_numerical': 0.006336405529953917, 'mean_word_length': 2.645669291338583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '6148617', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2498, 'n_words': 511}