-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.test.ts
81 lines (70 loc) · 3.59 KB
/
utils.test.ts
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
import * as utils from './utils'
import {exec} from '@actions/exec'
jest.mock('@actions/core')
jest.mock('@actions/exec')
// just making sure the mock methods are correctly typed
const mockedExec = exec as jest.MockedFunction<typeof exec>
// reset the counter on mock fn calls after every test
beforeEach(() => jest.clearAllMocks())
describe(`Actions Utils`, () => {
test(`isHeadAncestor uses git CLI to check if the commit is part of the current branch, returns true when it is`, async () => {
mockedExec.mockResolvedValue(0)
const hash = '5265ef99f1c8e18bcd282a11a4b752731cad5665'
const output = await utils.isHeadAncestor(hash)
expect(mockedExec).toHaveBeenCalledWith('git merge-base', ['--is-ancestor', hash, 'HEAD'])
expect(output).toBe(true)
})
test(`isHeadAncestor uses git CLI to check if the commit is part of the current branch, returns false when it is not`, async () => {
mockedExec.mockRejectedValue(128)
const hash = '5265ef99f1c8e18bcd282a11a4b752731cad5665'
const output = await utils.isHeadAncestor(hash)
expect(mockedExec).toHaveBeenCalledWith('git merge-base', ['--is-ancestor', hash, 'HEAD'])
expect(output).toBe(false)
})
test(`writeLineToFile creates a file using a shell script`, async () => {
mockedExec.mockResolvedValue(0)
await utils.writeLineToFile({path: '/some/file', text: 'hello world'})
expect(mockedExec).toHaveBeenCalledWith(`/bin/bash -c "echo hello world > /some/file"`)
})
describe('S3 Utils', () => {
test(`fileExistsInS3 uses AWS CLI to check for of an object in S3 bucket, returns true if it exists`, async () => {
mockedExec.mockResolvedValue(0)
const output = await utils.fileExistsInS3({key: 'my/key', bucket: 'my-bucket'})
expect(mockedExec).toHaveBeenCalledWith('aws s3api head-object', [
'--bucket=my-bucket',
'--key=my/key'
])
expect(output).toBe(true)
})
test(`getCommitHashFromRef uses git CLI to return the latest commit hash of the repo`, async () => {
mockedExec.mockResolvedValue(0)
const output = await utils.getCommitHashFromRef('HEAD')
expect(mockedExec).toHaveBeenCalledWith('git rev-parse', ['HEAD'], {
listeners: {stdout: expect.any(Function)}
})
expect(output).toBe('')
})
test(`fileExistsInS3 uses AWS CLI to check for of an object in S3 bucket, returns true if it exists`, async () => {
mockedExec.mockRejectedValue(255)
const output = await utils.fileExistsInS3({key: 'my/key', bucket: 'my-bucket'})
expect(mockedExec).toHaveBeenCalledWith('aws s3api head-object', [
'--bucket=my-bucket',
'--key=my/key'
])
expect(output).toBe(false)
})
test(`copyFileToS3 uses AWS CLI to copy a local file to S3 bucket`, async () => {
mockedExec.mockResolvedValue(0)
await utils.copyFileToS3({path: '/some/file', key: 'my/key', bucket: 'my-bucket'})
expect(mockedExec).toHaveBeenCalledWith('aws s3 cp', [
'/some/file',
's3://my-bucket/my/key'
])
})
test(`removeFileFromS3 uses AWS CLI to delete a file from S3 bucket`, async () => {
mockedExec.mockResolvedValue(0)
await utils.removeFileFromS3({key: 'my/key', bucket: 'my-bucket'})
expect(mockedExec).toHaveBeenCalledWith('aws s3 rm', ['s3://my-bucket/my/key'])
})
})
})