-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
84 lines (69 loc) · 2.49 KB
/
index.test.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
require('jest-fetch-mock').enableMocks()
const injectRemoteFile = require('./injectRemoteFile')
const io = require('@actions/io')
const fs = require('fs')
const TEST_DIRECTORY = './.test'
const TEST_TARGET = TEST_DIRECTORY + '/test.md'
beforeAll(() => {
fetch.mockResponse('hello world')
return io.mkdirP(TEST_DIRECTORY)
})
beforeEach(() => fs.writeFileSync(TEST_TARGET, ''))
test('fails if url is invalid', () => {
expect.assertions(1)
return expect(
injectRemoteFile(
'ssh://raw.githubusercontent.com/olivr-com/defaults/master/readme/support.md',
TEST_TARGET
)
).rejects.toThrow('Please ensure your url is a valid http(s) url')
})
test('fails if target file does not exist', () => {
fs.unlinkSync(TEST_TARGET)
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET)
).rejects.toThrow('Please ensure your target file already exists')
})
test('fails if no pattern is specified and cannot detect pattern', () => {
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com', TEST_TARGET)
).rejects.toThrow('Could not detect a pattern, please specify it manually')
})
test('succeeds if no pattern is specified and can detect pattern', () => {
fs.writeFileSync(TEST_TARGET, '<!-- auto-support --><!-- auto-support -->')
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET)
).resolves.toContain(TEST_TARGET)
})
test('succeeds if a pattern is specified', () => {
const PATTERN = '<!-- my-pattern -->'
fs.writeFileSync(TEST_TARGET, PATTERN + PATTERN)
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET, PATTERN)
).resolves.toContain(TEST_TARGET)
})
test('succeeds if target does not contain the pattern and force is TRUE', () => {
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET)
).resolves.toContain(TEST_TARGET)
})
test('succeeds if target does not contain the pattern and force is FALSE', () => {
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET, '', false)
).resolves.toContain('')
})
test('outputs that no file is changed if it is the case', () => {
expect.assertions(1)
return expect(
injectRemoteFile('http://test.com/support.md', TEST_TARGET).then(() =>
injectRemoteFile('http://test.com/support.md', TEST_TARGET)
)
).resolves.toContain('')
})
afterAll(() => io.rmRF(TEST_DIRECTORY))