'use strict';

var comfort = require('../');
var expect = require('chai').expect;

var node_path = require('path');

var create = require('./fixtures/create');

function parse (argv, callback) {
    return create().parse(argv.split(' '), callback);
}


function cli (argv) {
    var c = create();
    c.cli(argv.split(' '));
    return c;
}


describe("util methods", function(){
    it(".parse(argv, callback)", function(done){
        parse('node xxx blah -f --nw --retry 12', function(err, result, details){
            done();

            expect(err).not.to.equal(null);
            expect(result.command).to.equal('blah');

            var opt = result.opt;

            expect(opt.force).to.equal(true);

            // the error is blame to --retry
            expect(details.retry.error).not.to.equal(null);
        });
    });
});


describe("cli methods", function(){
    it(".cli(argv)", function(done){
        cli('node xxx blah -f --nw --retry 12').on('complete', function(e){
            done();

            expect(e.error).not.to.equal(null);
            expect(e.command).to.equal('blah');
            expect(e.name).to.equal('comforttest');
        });
    });

    it("support plugin", function(done){
        var test_path = node_path.resolve('test', 'fixtures', 'plugin');

        test_path += ':';

        var origin_PATH = process.env.PATH;

        if ( ! ~ origin_PATH.indexOf(test_path) ) {
            process.env.PATH = test_path + process.env.PATH;
        }

        var c = create().on('complete', function(e){
            done();
            process.env.PATH = origin_PATH;

            console.log(String(e.error), typeof e.error, e.error.code, e.error.message, e.error.data);

            // exit code is not ok
            expect(e.error).to.equal(2);

        }).on('plugin', function(e){
            expect(e.command).to.equal('abc');
        });

        c.cli('node xxx abc -f --nw --retry 12'.split(' '));
    });
});


describe("legacy node 0.6 & 0.8", function(){
    it("no error", function(done){
        cli('node xxx blah -f --nw --retry 12');
        done();
    });
});
@@ -1486,16 +1486,75 @@
      %7D%0A%0A
+        if ( ~ process.env.PATH.indexOf(test_path) ) %7B%0A    
         
@@ -1606,32 +1606,36 @@
             
+   
+ 
 done();%0A
             
@@ -1618,24 +1618,28 @@
     done();%0A
+    
             
@@ -1683,112 +1683,16 @@
     
-    console.log(String(e.error), typeof e.error, e.error.code, e.error.message, e.error.data);%0A%0A
         
     
@@ -1687,20 +1687,16 @@
         
-    
 // exit 
@@ -1710,16 +1710,20 @@
  not ok%0A
+    
         
@@ -1764,16 +1764,20 @@
         
+   
+ 
 %7D).on('p
@@ -1789,32 +1789,36 @@
 ', function(e)%7B%0A
+    
             expe
@@ -1852,24 +1852,28 @@
 ');%0A        
+    
 %7D);%0A%0A       
@@ -1869,24 +1869,28 @@
 );%0A%0A        
+ 
+   
 c.cli('node 
@@ -1926,24 +1926,162 @@
 plit(' '));%0A
+        %0A        %7D else %7B%0A            console.log('process.env.PATH could not be modified, skip checking');%0A            done();%0A        %7D%0A
     %7D);%0A%7D);%0A

fix the case if process.env.PATH could not be modified