Skip to content

Commit b4949d0

Browse files
committed
squash: Adding tests in for load and loadPatch
* This also adds the proxyquire and nock modules as dev dependencies
1 parent 2254e65 commit b4949d0

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"devDependencies": {
1919
"check-pkg": "^2.0.2",
2020
"lintit": "^6.0.1",
21+
"nock": "~10.0.6",
22+
"proxyquire": "^2.1.1",
2123
"tap": "^12.6.1"
2224
},
2325
"files": [

test/utils-test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)