var assert = require('assert'),
    fs = require('fs'),
    lib = require('./lib'),
    watcher = require('../lib/watcher'),
    wrench = require('wrench');

exports['read'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch');

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  watcher.watchFile(outdir + '/index.html', [], function(type, fileName, sourceChange) {
    assert.fail('Watch event occurred.');
  });

  fs.open(outdir + '/index.html', 'r+', function(err, fd) {
    var buffer = new Buffer(4);
    fs.read(fd, buffer, 0, buffer.length, 0, function(err, bytesRead, buffer) {
      fs.close(fd, function() {
        watcher.unwatchAll();
        setTimeout(done, 500);
      });
    });
  });
};

exports['write'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch'),
      count = 0;

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  var testFile = outdir + '/index.html';
  watcher.watchFile(testFile, [], function(type, fileName, sourceChange) {
    assert.equal(1, ++count);
    assert.equal('change', type);
    assert.equal(testFile, fileName);
    assert.equal(testFile, sourceChange);
    watcher.unwatchAll();
    done();
  });

  fs.open(testFile, 'w', function(err, fd) {
    var buffer = new Buffer([1, 2, 3, 4]);
    fs.write(fd, buffer, 0, buffer.length, 0, function(err, written, buffer) {
      fs.close(fd);
    });
  });
};

exports['unlink'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch'),
      count = 0;

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  var testFile = outdir + '/index.html';
  watcher.watchFile(testFile, [], function(type, fileName, sourceChange) {
    assert.equal(1, ++count);
    assert.equal('remove', type);
    assert.equal(testFile, fileName);
    assert.equal(testFile, sourceChange);
    watcher.unwatchAll();
    done();
  });

  setTimeout(function() {
    fs.unlink(testFile);
  }, 100);
};

exports['rename'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch'),
      count = 0;

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  var testFile = outdir + '/index.html';
  watcher.watchFile(testFile, [], function(type, fileName, sourceChange) {
    assert.equal(1, ++count);
    assert.equal('remove', type);
    assert.equal(testFile, fileName);
    assert.equal(testFile, sourceChange);
    watcher.unwatchAll();
    done();
  });

  setTimeout(function() {
    fs.rename(testFile, outdir + '/foo');
  }, 100);
};

exports['overwrite'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch'),
      count = 0;

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  var testFile = outdir + '/index.html';
  watcher.watchFile(testFile, [], function(type, fileName, sourceChange) {
    assert.ok(2 >= ++count, 'Unexpected count:' + count);
    assert.equal(count === 1 ? 'rename' : 'change', type);
    assert.equal(testFile, fileName);
    assert.equal(testFile, sourceChange);
    if (count >= 2) {
      watcher.unwatchAll();
      done();
    }
  });

  setTimeout(function() {
    fs.rename(outdir + '/static.json', testFile, function() {
      fs.open(testFile, 'w', function(err, fd) {
        var buffer = new Buffer([1, 2, 3, 4]);
        fs.write(fd, buffer, 0, buffer.length, 0, function(err, written, buffer) {
          fs.close(fd);
        });
      });
    });
  }, 100);
};

exports['create-child'] = function(done) {
  var outdir = lib.testDir('watcher', 'touch'),
      count = 0;

  wrench.copyDirSyncRecursive('test/artifacts', outdir);

  var testFile = outdir + '/foo';
  watcher.watchFile(outdir, [], function(type, fileName, sourceChange) {
    assert.equal(1, ++count);
    assert.equal('create', type);
    assert.equal(outdir, fileName);
    assert.equal(outdir, sourceChange);
    watcher.unwatchAll();
    done();
  });

  setTimeout(function() {
    fs.writeFile(testFile, 'foo');
  }, 100);
};

@@ -151,16 +151,93 @@
 nch');%0A%0A
+exports%5B'teardown'%5D = function(done) %7B%0A  watcher.unwatchAll();%0A  done();%0A%7D;%0A%0A
 exports%5B
@@ -255,32 +255,32 @@
 unction(done) %7B%0A
-
   var outdir = l
@@ -716,38 +716,8 @@
 ) %7B%0A
-        watcher.unwatchAll();%0A
     
@@ -1195,34 +1195,8 @@
 e);%0A
-    watcher.unwatchAll();%0A
     
@@ -1840,34 +1840,8 @@
 e);%0A
-    watcher.unwatchAll();%0A
     
@@ -2346,34 +2346,8 @@
 e);%0A
-    watcher.unwatchAll();%0A
     
@@ -2947,36 +2947,8 @@
 ) %7B%0A
-      watcher.unwatchAll();%0A
     
@@ -3672,32 +3672,32 @@
 dir, fileName);%0A
+
     assert.equal
@@ -3724,34 +3724,8 @@
 e);%0A
-    watcher.unwatchAll();%0A
     

Move unwatch to teardown