UNPKG

3.69 kBJavaScriptView Raw
1var sinon = require('sinon');
2var expect = require('chai').use(require('sinon-chai')).expect;
3var stream = require('mock-utf8-stream');
4var ConnectionPool = require('ssh-pool').ConnectionPool;
5var Shipit = require('../lib/shipit');
6
7describe('Shipit', function () {
8 var shipit, stdout, stderr;
9
10 beforeEach(function () {
11 stdout = new stream.MockWritableStream();
12 stderr = new stream.MockWritableStream();
13 shipit = new Shipit({
14 stdout: stdout,
15 stderr: stderr,
16 environment: 'stage'
17 });
18 shipit.stage = 'stage';
19 });
20
21 describe('#initialize', function () {
22 beforeEach(function () {
23 sinon.stub(shipit, 'initSshPool').returns(shipit);
24 });
25
26 afterEach(function () {
27 shipit.initSshPool.restore();
28 });
29
30 it('should add stage and initialize shipit', function () {
31 shipit.initialize();
32 expect(shipit.initSshPool).to.be.called;
33 });
34 });
35
36 describe('#initSshPool', function () {
37 it('should initialize an ssh pool', function () {
38 shipit.config = {servers: ['deploy@my-server']};
39 shipit.initSshPool();
40
41 expect(shipit.pool).to.be.instanceOf(ConnectionPool);
42 expect(shipit.pool).to.have.deep.property('.connections[0].remote.user', 'deploy');
43 expect(shipit.pool).to.have.deep.property('.connections[0].remote.host', 'my-server');
44 });
45 });
46
47 describe('#initConfig', function () {
48 it('should initialize config', function () {
49 shipit.initConfig({default: {foo: 'bar', servers: ['1', '2']}, stage: {kung: 'foo', servers: ['3']}});
50
51 expect(shipit.config).to.be.deep.equal({
52 branch: 'master',
53 keepReleases: 5,
54 foo: 'bar',
55 kung: 'foo',
56 servers: ['3'],
57 shallowClone: false
58 });
59 });
60 });
61
62 describe('#local', function () {
63 it('should wrap and log to stdout', function () {
64 stdout.startCapture();
65 return shipit.local('echo "hello"').then(function (res) {
66 expect(stdout.capturedData).to.equal('@ hello\n');
67 expect(res).to.have.property('stdout');
68 expect(res).to.have.property('stderr');
69 expect(res).to.have.property('child');
70 });
71 });
72 });
73
74 describe('#remote', function () {
75 beforeEach(function () {
76 shipit.pool = {run: sinon.stub()};
77 });
78
79 it('should run command on pool', function () {
80 shipit.remote('my-command');
81
82 expect(shipit.pool.run).to.be.calledWith('my-command');
83 });
84
85 it('should cd and run command on pool', function () {
86 shipit.remote('my-command', {cwd: '/my-directory'});
87
88 expect(shipit.pool.run).to.be.calledWith('cd "/my-directory" && my-command', {});
89 });
90 });
91
92 describe('#remoteCopy', function () {
93 beforeEach(function () {
94 shipit.pool = {copy: sinon.stub()};
95 });
96
97 it('should run command on pool', function () {
98 shipit.remoteCopy('src', 'dest');
99
100 expect(shipit.pool.copy).to.be.calledWith('src', 'dest');
101 });
102
103 it('should accept options for shipit.pool.copy', function () {
104 shipit.remoteCopy('src', 'dest', {
105 direction: 'remoteToLocal'
106 });
107
108 expect(shipit.pool.copy).to.be.calledWith('src', 'dest', {
109 direction: 'remoteToLocal',
110 ignores: [],
111 rsync: []
112 });
113 });
114
115 it('should support options specified in config', function () {
116 shipit.config = {
117 ignores: ['foo'],
118 rsync: ['--bar']
119 };
120
121 shipit.remoteCopy('src', 'dest', {
122 direction: 'remoteToLocal'
123 });
124
125 expect(shipit.pool.copy).to.be.calledWith('src', 'dest', {
126 direction: 'remoteToLocal',
127 ignores: ['foo'],
128 rsync: ['--bar']
129 });
130 });
131 });
132});