|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { test } = require('tap') |
| 4 | +const proxyquire = require('proxyquire') |
| 5 | +const nock = require('nock') |
| 6 | + |
| 7 | +const commitHash = 'a12b34c56' |
| 8 | + |
| 9 | +test('Test util functions', (t) => { |
| 10 | + t.test('test sha load function', (tt) => { |
| 11 | + const commitMessage = 'Commit Message' |
| 12 | + |
| 13 | + const utils = proxyquire('../lib/utils', { |
| 14 | + 'child_process': { |
| 15 | + exec: (cmd, cb) => { |
| 16 | + tt.equals(cmd, |
| 17 | + `git show --quiet --format=medium ${commitHash}`, |
| 18 | + 'cmd should be equal') |
| 19 | + cb(null, commitMessage) |
| 20 | + } |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + utils.load(commitHash, (err, stdout, stderr) => { |
| 25 | + tt.equals(err, null, 'Should not be an error') |
| 26 | + tt.equals(commitMessage, stdout, 'should have the commit message') |
| 27 | + tt.end() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + t.test('test sha load function with error', (tt) => { |
| 32 | + const utils = proxyquire('../lib/utils', { |
| 33 | + 'child_process': { |
| 34 | + exec: (cmd, cb) => { |
| 35 | + cb('Error', null) |
| 36 | + } |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + utils.load(commitHash, (err, stdout, stderr) => { |
| 41 | + tt.equals(err, 'Error', 'should have the error message') |
| 42 | + tt.end() |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + t.test('test load patch function using http', (tt) => { |
| 47 | + const commitUrl = `http://api.github.com/repos/nodejs/${commitHash}` |
| 48 | + const util = require('../lib/utils') |
| 49 | + |
| 50 | + nock('http://api.github.com', { |
| 51 | + reqheaders: { |
| 52 | + 'user-agent': 'core-validate-commit' |
| 53 | + } |
| 54 | + }) |
| 55 | + .get(`/repos/nodejs/${commitHash}`) |
| 56 | + .reply(200, {commit: 'message'}) |
| 57 | + |
| 58 | + util.load(commitUrl, (err, commitMessage) => { |
| 59 | + tt.equals(err, null, 'Should not be an error') |
| 60 | + tt.pass() |
| 61 | + tt.end() |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + t.test('test load patch function using https', (tt) => { |
| 66 | + const commitUrl = `https://api.github.com/repos/nodejs/${commitHash}` |
| 67 | + const util = require('../lib/utils') |
| 68 | + |
| 69 | + nock('https://api.github.com', { |
| 70 | + reqheaders: { |
| 71 | + 'user-agent': 'core-validate-commit' |
| 72 | + } |
| 73 | + }) |
| 74 | + .get(`/repos/nodejs/${commitHash}`) |
| 75 | + .reply(200, {commit: 'message'}) |
| 76 | + |
| 77 | + util.load(commitUrl, (err, commitMessage) => { |
| 78 | + tt.equals(err, null, 'Should not be an error') |
| 79 | + tt.pass() |
| 80 | + tt.end() |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + t.test('test load patch function - catch parse error', (tt) => { |
| 85 | + const commitUrl = `http://api.github.com/repos/nodejs/${commitHash}` |
| 86 | + const util = require('../lib/utils') |
| 87 | + |
| 88 | + nock('http://api.github.com', { |
| 89 | + reqheaders: { |
| 90 | + 'user-agent': 'core-validate-commit' |
| 91 | + } |
| 92 | + }) |
| 93 | + .get(`/repos/nodejs/${commitHash}`) |
| 94 | + .reply(200, '{commit: \'message\'}') |
| 95 | + |
| 96 | + util.load(commitUrl, (err, commitMessage) => { |
| 97 | + tt.true(err) |
| 98 | + tt.pass() |
| 99 | + tt.end() |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + t.end() |
| 104 | +}) |
0 commit comments