forked from cookpete/auto-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
105 lines (92 loc) · 2.55 KB
/
utils.js
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 { describe, it } = require('mocha')
const { expect } = require('chai')
const {
updateLog,
cmd,
niceDate,
isLink,
getGitVersion,
readFile,
writeFile,
fileExists,
readJson,
__Rewire__: mock,
__ResetDependency__: unmock
} = require('../src/utils')
describe('updateLog', () => {
it('doesn\'t error', async () => {
updateLog('Test', false)
updateLog('Test')
})
})
describe('cmd', () => {
it('runs a command', async () => {
const result = await cmd('node --version')
expect(result).to.be.a('string')
})
it('runs onProgress', async () => {
const result = await cmd('node --version', bytes => expect(bytes).to.be.a('number'))
expect(result).to.be.a('string')
})
})
describe('niceDate', () => {
it('formats string into nice date', () => {
expect(niceDate('2015-10-03')).to.match(/^\d October 2015$/)
expect(niceDate('2017-11-07T19:19:02.635Z')).to.match(/^\d November 2017$/)
})
it('formats date into nice date', () => {
expect(niceDate(new Date(2016, 8, 2))).to.match(/^\d September 2016$/)
expect(niceDate(new Date('2015-10-03'))).to.match(/^\d October 2015$/)
})
})
describe('isLink', () => {
it('returns true for links', () => {
expect(isLink('http://test.com')).to.equal(true)
})
it('returns false for non-links', () => {
expect(isLink('not a link')).to.equal(false)
})
})
describe('getGitVersion', () => {
it('returns git version', async () => {
mock('cmd', () => 'git version 2.15.2 (Apple Git-101.1)')
expect(await getGitVersion()).to.equal('2.15.2')
unmock('cmd')
})
it('returns null', async () => {
mock('cmd', () => 'some sort of random output')
expect(await getGitVersion()).to.equal(null)
unmock('cmd')
})
})
describe('readFile', () => {
it('reads file', async () => {
mock('fs', { readFile: (path, type, cb) => cb(null, 'abc') })
expect(await readFile()).to.equal('abc')
unmock('fs')
})
})
describe('writeFile', () => {
it('reads file', async () => {
mock('fs', { writeFile: (path, data, cb) => cb(null, 'abc') })
expect(await writeFile()).to.equal('abc')
unmock('fs')
})
})
describe('fileExists', () => {
it('reads file', async () => {
mock('fs', { access: (path, cb) => cb(null) })
expect(await fileExists()).to.equal(true)
unmock('fs')
})
})
describe('readJson', () => {
it('reads file', async () => {
mock('fs', {
readFile: (path, type, cb) => cb(null, '{"abc":123}'),
access: (path, cb) => cb(null)
})
expect(await readJson()).to.deep.equal({ abc: 123 })
unmock('fs')
})
})