liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
 
describe('simple cli', () => {
  let stubs;
 
  beforeEach(() => {
    stubs = {
      getDynamicValues: sinon.stub().callsArg(0),
      spawn: sinon.stub(),
      handleCustomOption: sinon.stub(),
      debug: sinon.stub()
    };
  });
 
  class Builder {
    constructor() {
      Object.assign(this, stubs);
    }
  }
 
  const cli = proxyquire('../lib/simple-cli', {
    './builder': Builder
  });
 
  it('should return a function', () => {
    cli('name').should.be.an.instanceOf(Function);
  });
 
  it('should create a multitask with a string', () => {
    const grunt = {
      registerMultiTask: sinon.stub()
    };
 
    cli('blah')(grunt);
    grunt.registerMultiTask.should.have.been.calledWith('blah', 'A simple grunt wrapper for blah', sinon.match.func);
  });
 
  it('should create a multitask with an object', () => {
    const grunt = {
      registerMultiTask: sinon.stub()
    };
 
    cli('task', { description: 'description' })(grunt);
    grunt.registerMultiTask.should.have.been.calledWith('task', 'description', sinon.match.func);
  });
 
  describe('configuring a task', () => {
    let grunt, cb, options;
 
    beforeEach(() => {
      grunt = {
        registerMultiTask: sinon.stub(),
        fail: {
          fatal: sinon.stub()
        }
      };
 
      cb = sinon.stub();
      options = {
        description: 'description',
        custom: {
          foo: 'bar'
        },
        callback: cb
      };
    });
 
    it('should handle success', () => {
      cli('task', options)(grunt);
      const task = grunt.registerMultiTask.getCall(0).args[2];
      const context = {};
      stubs.handleCustomOption.callsArg(1);
      task.apply(context);
      stubs.getDynamicValues.should.have.been.calledWith(sinon.match.func);
      stubs.handleCustomOption.should.have.been.calledWith('foo', sinon.match.func);
      stubs.spawn.should.have.been.called;
    });
 
    it('should handle async errors', () => {
      cli('task', options)(grunt);
      const task = grunt.registerMultiTask.getCall(0).args[2];
      const context = {};
      stubs.handleCustomOption.callsArgWith(1, 'error');
      task.apply(context);
      stubs.getDynamicValues.should.have.been.calledWith(sinon.match.func);
      stubs.handleCustomOption.should.have.been.calledWith('foo', sinon.match.func);
      grunt.fail.fatal.should.have.been.calledWith('error');
      stubs.spawn.called.should.be.false();
    });
 
    it('should allow debugging', () => {
      Builder.prototype.debugOn = true;
      cli('task', options)(grunt);
      const task = grunt.registerMultiTask.getCall(0).args[2];
      const context = {};
      stubs.handleCustomOption.callsArg(1);
      task.apply(context);
      stubs.getDynamicValues.should.have.been.calledWith(sinon.match.func);
      stubs.handleCustomOption.should.have.been.calledWith('foo', sinon.match.func);
      stubs.debug.should.have.been.called;
      stubs.spawn.should.not.have.been.called;
    });
  });
});