1 | 'use strict';
|
2 |
|
3 | var comfort = require('../');
|
4 | var expect = require('chai').expect;
|
5 |
|
6 | var node_path = require('path');
|
7 |
|
8 | var create = require('./fixtures/create');
|
9 |
|
10 | function parse(argv, callback) {
|
11 | return create().parse(argv.split(' '), callback);
|
12 | }
|
13 |
|
14 |
|
15 | function cli(argv) {
|
16 | var c = create();
|
17 | c.cli(argv.split(' '));
|
18 | return c;
|
19 | }
|
20 |
|
21 |
|
22 | describe("util methods", function() {
|
23 | it(".parse(argv, callback)", function(done) {
|
24 | parse('node xxx blah -f --nw --retry 12', function(err, result) {
|
25 | done();
|
26 |
|
27 | expect(err).not.to.equal(null);
|
28 | });
|
29 | });
|
30 | });
|
31 |
|
32 |
|
33 | describe("cli methods", function() {
|
34 | it(".cli(argv)", function(done) {
|
35 | cli('node xxx blah -f --nw --retry 12').on('finish', function() {
|
36 |
|
37 | expect(true).to.equal(false);
|
38 | done();
|
39 | }).on('error', function() {
|
40 | done();
|
41 | });
|
42 | });
|
43 |
|
44 | it("support plugin", function(done) {
|
45 | var test_path = node_path.resolve('test', 'fixtures', 'plugin');
|
46 |
|
47 | test_path += ':';
|
48 |
|
49 | var origin_PATH = process.env.PATH;
|
50 |
|
51 | if (!~origin_PATH.indexOf(test_path)) {
|
52 | process.env.PATH = test_path + process.env.PATH;
|
53 | }
|
54 |
|
55 | if (~process.env.PATH.indexOf(test_path)) {
|
56 | var c = create()
|
57 | .on('finish', function(e) {
|
58 | done();
|
59 | process.env.PATH = origin_PATH;
|
60 |
|
61 |
|
62 | expect(false).to.equal(true);
|
63 |
|
64 | }).on('plugin', function(e) {
|
65 | expect(e.command).to.equal('abc');
|
66 |
|
67 | }).on('error', function(e) {
|
68 | process.env.PATH = origin_PATH;
|
69 | expect(e).to.equal(2);
|
70 | done();
|
71 | });
|
72 |
|
73 | c.cli('node xxx abc -f --nw --retry 12'.split(' '));
|
74 |
|
75 | } else {
|
76 | console.log('`process.env.PATH` could not be modified, skip checking');
|
77 | done();
|
78 | }
|
79 | });
|
80 | });
|
81 |
|
82 |
|
83 | describe("prevent pollution for a certain command", function() {
|
84 | it("independent instances", function(done) {
|
85 | var one = create().on('finish', function(e) {
|
86 | var two = create().on('finish', function(e) {
|
87 | done();
|
88 | });
|
89 |
|
90 | two.cli('node xxx polute -f --nw --retry 12'.split(' '));
|
91 | });
|
92 |
|
93 | one.cli('node xxx polute -f --nw --retry 12'.split(' '));
|
94 | });
|
95 | });
|
96 |
|
97 |
|
98 | describe("legacy node 0.6 & 0.8", function() {
|
99 | it("no error", function(done) {
|
100 | cli('node xxx blah -f --nw --retry 12').on('error', function (err) {
|
101 | expect(err).not.to.equal(null);
|
102 | done();
|
103 | });
|
104 | });
|
105 | });
|
106 |
|
107 |
|
108 | describe("with setup", function(){
|
109 | it("should delay running", function(done){
|
110 | var flag = false;
|
111 | function setup (done) {
|
112 | setTimeout(function () {
|
113 | flag = true;
|
114 | done();
|
115 | }, 50);
|
116 | }
|
117 |
|
118 | var one = create().on('finish', function(e) {
|
119 | expect(flag).to.equal(true);
|
120 | done();
|
121 | });
|
122 |
|
123 | one.setup(setup).cli('node xxx polute -f --nw --retry 12'.split(' '));
|
124 | expect(flag).to.equal(false);
|
125 | });
|
126 | });
|