Socialify

Folder ..

Viewing fetchGitData.js
192 lines (190 loc) • 4.9 KB

  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';

const should = require('should');
const fetchGitData = require('../lib/fetchGitData');
const { getOptions } = require('..');

describe('fetchGitData', () => {
  beforeEach(() => {
    process.env = { PATH: process.env.PATH };
  });
  it('should throw an error when no data is passed', () => {
    fetchGitData.should.throw(/fetchGitData requires a callback/);
  });
  it('should throw an error when no git context is provided', done => {
    fetchGitData(undefined, err => {
      err.should.match(/No options passed/);
      done();
    });
  });
  it('should throw an error if no head is provided', done => {
    fetchGitData({
    }, err => {
      err.should.match(/You must provide the head/);
      done();
    });
  });
  it('should throw an error if no head.id is provided', done => {
    fetchGitData({
      head: {}
    }, err => {
      err.should.match(/You must provide the head.id/);
      done();
    });
  });
  it('should return default values', done => {
    fetchGitData({
      head: {
        id: 'COMMIT_HASH'
      }
    }, (err, options) => {
      should.not.exist(err);
      options.should.eql({
        'head': {
          'id': 'COMMIT_HASH',
          'author_name': 'Unknown Author',
          'author_email': '',
          'committer_name': 'Unknown Committer',
          'committer_email': '',
          'message': 'Unknown Commit Message'
        },
        'branch': '',
        'remotes': []
      });
      done();
    });
  });
  it('should override default values', done => {
    fetchGitData({
      'head': {
        'id': 'COMMIT_HASH',
        'author_name': 'MY AUTHOR',
        'author_email': '',
        'committer_name': 'MY COMMITTER',
        'committer_email': '',
        'message': 'MY COMMIT MESSAGE'
      },
      'branch': 'TEST',
      'remotes': [
        {
          'name': 'TEST',
          'url': 'test-url'
        }
      ]
    }, (err, options) => {
      should.not.exist(err);
      options.should.eql({
        'head': {
          'id': 'COMMIT_HASH',
          'author_name': 'MY AUTHOR',
          'author_email': '',
          'committer_name': 'MY COMMITTER',
          'committer_email': '',
          'message': 'MY COMMIT MESSAGE'
        },
        'branch': 'TEST',
        'remotes': [
          {
            'name': 'TEST',
            'url': 'test-url'
          }
        ]
      });
      done();
    });
  });
  it('should convert git.branch to a string', done => {
    fetchGitData({
      'head': {
        'id': 'COMMIT_HASH'
      },
      'branch': {
        'covert': 'to a string'
      }
    }, (err, str) => {
      should.not.exist(err);
      str.branch.should.be.String();
      fetchGitData({
        'head': {
          'id': 'COMMIT_HASH'
        },
        'branch': ['convert', 'to', 'a', 'string']
      }, (err, str) => {
        should.not.exist(err);
        str.branch.should.be.String();
        done();
      });
    });
  });
  it('should convert git.remotes to an array', done => {
    fetchGitData({
      'head': {
        'id': 'COMMIT_HASH'
      },
      'remotes': 'convert from string to an array'
    }, (err, arr) => {
      should.not.exist(err);
      arr.remotes.should.be.instanceof(Array);
      fetchGitData({
        'head': {
          'id': 'COMMIT_HASH'
        },
        'remotes': {
          'convert': 'from object to an array'
        }
      }, (err, arr) => {
        should.not.exist(err);
        arr.remotes.should.be.instanceof(Array);
        done();
      });
    });
  });
  it('should save passed remotes', done => {
    fetchGitData({
      'head': {
        'id': 'COMMIT_HASH'
      },
      'remotes': [
        {
          'name': 'test',
          'url': 'https://my.test.url'
        }
      ]
    }, (err, options) => {
      should.not.exist(err);
      options.should.eql({
        'head': {
          'id': 'COMMIT_HASH',
          'author_name': 'Unknown Author',
          'author_email': '',
          'committer_name': 'Unknown Committer',
          'committer_email': '',
          'message': 'Unknown Commit Message'
        },
        'branch': '',
        'remotes': [
          {
            'name': 'test',
            'url': 'https://my.test.url'
          }
        ]
      });
      done();
    });
  });
  it('should execute git commands when a valid commit hash is given', done => {
    process.env.COVERALLS_GIT_COMMIT = 'HEAD';
    process.env.COVERALLS_GIT_BRANCH = 'master';
    getOptions((err, options) => {
      should.not.exist(err);
      options = options.git;
      options.head.should.be.Object();
      options.head.author_name.should.not.equal('Unknown Author');
      options.head.committer_name.should.not.equal('Unknown Committer');
      options.head.message.should.not.equal('Unknown Commit Message');
      options.branch.should.be.String();
      options.should.have.property('remotes');
      options.remotes.should.be.instanceof(Array);
      options.remotes.length.should.be.above(0);
      done();
    });
  });
});