'use strict';

var gulp = require('gulp');
var chroot = require('./index');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var through2 = require('through2');
chroot(gulp);

before(function(done) {
  fs.mkdir(path.join(__dirname,'child'), function(err) {
    if (err) done(err);
    fs.writeFile(path.join(__dirname, 'child', 'test.txt'), 'test', done);
  });
});
after(function(done) {
  fs.unlink(path.join(__dirname, 'child', 'test.txt'), function(err) {
    if (err) done(err);
    fs.rmdir(path.join(__dirname,'child'), done);
  });
});

it('should chroot and restore', function() {
  gulp.chroot('child', function(){
    gulp.task('test', function() {
      console.log('inside test task');
      console.log(process.cwd());
      assert.equal(process.cwd(), path.join(__dirname, 'child'));
    });
  });
  gulp.task('restore', function() {
      console.log('inside restore task');
      console.log(process.cwd());
      assert.equal(process.cwd(), path.join(__dirname));
  });
  gulp.start('test');
  gulp.start('restore');
});

it('should work with dep only', function() {
  gulp.chroot('child', function() {
    gulp.task('dep', function() {
      console.log('inside dep task');
      console.log(process.cwd());
      assert.equal(process.cwd(), path.join(__dirname, 'child'));
    });
    gulp.task('testDep', ['dep']);
  });
  gulp.start('testDep');
});

it('should work with promise', function(done) {
  gulp.chroot('child', function() {
    gulp.task('promise', function() {
      return gulp.src('test.txt')
                .pipe(through2.obj(function(){
                  console.log('inside promise task');
                  console.log(process.cwd());
                  assert.equal(process.cwd(), path.join(__dirname, 'child'));
                }))
                .pipe(gulp.dest(path.join(__dirname, 'child', '2.txt')));
    });
  });
  gulp.task('sync', ['promise'], function() {
    console.log('inside sync');
    done();
  });
  gulp.start('sync');
});

@@ -1596,24 +1596,38 @@
 bj(function(
+chunk, enc, cb
 )%7B%0A         
@@ -1791,24 +1791,48 @@
  'child'));%0A
+                  cb();%0A
             

fix test