This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
/
codebuild.test.js
94 lines (86 loc) · 3.2 KB
/
codebuild.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
85
86
87
88
89
90
91
92
93
94
var codebuild = require('../../lib/services/codebuild')
var git = require('../../lib/git.js')
// Set all module functions to jest.fn
jest.mock('../../lib/git.js')
describe('AWS CodeBuild Provider', function() {
it('can detect codebuild', function() {
process.env.CODEBUILD_CI = 'true'
expect(codebuild.detect()).toBe(true)
})
it('can get codebuild env info', function() {
process.env.CODEBUILD_CI = 'true'
process.env.CODEBUILD_BUILD_ID =
'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969'
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
process.env.CODEBUILD_SOURCE_VERSION = 'pr/1'
process.env.CODEBUILD_SOURCE_REPO_URL =
'https://github.com/my-org/my-project.git'
expect(codebuild.configuration()).toEqual({
service: 'codebuild',
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
branch: 'master',
pr: '1',
slug: 'my-org/my-project',
})
})
it('throws if branch name cannot be detected', function() {
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
git.branch.mockImplementation(function() {
throw new Error()
})
expect(function() {
codebuild.configuration()
}).toThrow()
})
it('Test build triggered via AWS SDK', function() {
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
git.branch.mockReturnValue('master')
expect(codebuild.configuration()).toEqual({
service: 'codebuild',
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
branch: 'master',
pr: undefined,
slug: 'my-org/my-project',
})
})
it('Test PR build triggered via Github Webhook', function() {
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
expect(codebuild.configuration()).toEqual({
service: 'codebuild',
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
branch: 'master',
pr: '1',
slug: 'my-org/my-project',
})
})
it('Test non-PR build triggered via Github Webhook', function() {
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
process.env.CODEBUILD_SOURCE_VERSION =
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
expect(codebuild.configuration()).toEqual({
service: 'codebuild',
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
branch: 'master',
pr: undefined,
slug: 'my-org/my-project',
})
})
it('throws if slug cannot be detected', function() {
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
delete process.env.CODEBUILD_SOURCE_REPO_URL
expect(function() {
codebuild.configuration()
}).toThrow()
})
})