|
| 1 | +import { |
| 2 | + getBranchName, |
| 3 | + checkAnyBranch, |
| 4 | + checkAllBranch, |
| 5 | + toBranchMatchConfig, |
| 6 | + BranchMatchConfig |
| 7 | +} from '../src/branch'; |
| 8 | +import * as github from '@actions/github'; |
| 9 | + |
| 10 | +jest.mock('@actions/core'); |
| 11 | +jest.mock('@actions/github'); |
| 12 | + |
| 13 | +describe('getBranchName', () => { |
| 14 | + describe('when the pull requests base branch is requested', () => { |
| 15 | + it('returns the base branch name', () => { |
| 16 | + const result = getBranchName('base'); |
| 17 | + expect(result).toEqual('base-branch-name'); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('when the pull requests head branch is requested', () => { |
| 22 | + it('returns the head branch name', () => { |
| 23 | + const result = getBranchName('head'); |
| 24 | + expect(result).toEqual('head-branch-name'); |
| 25 | + }); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('checkAllBranch', () => { |
| 30 | + beforeEach(() => { |
| 31 | + github.context.payload.pull_request!.head = { |
| 32 | + ref: 'test/feature/123' |
| 33 | + }; |
| 34 | + github.context.payload.pull_request!.base = { |
| 35 | + ref: 'main' |
| 36 | + }; |
| 37 | + }); |
| 38 | + |
| 39 | + describe('when a single pattern is provided', () => { |
| 40 | + describe('and the pattern matches the head branch', () => { |
| 41 | + it('returns true', () => { |
| 42 | + const result = checkAllBranch(['^test'], 'head'); |
| 43 | + expect(result).toBe(true); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('and the pattern does not match the head branch', () => { |
| 48 | + it('returns false', () => { |
| 49 | + const result = checkAllBranch(['^feature/'], 'head'); |
| 50 | + expect(result).toBe(false); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('when multiple patterns are provided', () => { |
| 56 | + describe('and not all patterns matched', () => { |
| 57 | + it('returns false', () => { |
| 58 | + const result = checkAllBranch(['^test/', '^feature/'], 'head'); |
| 59 | + expect(result).toBe(false); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('and all patterns match', () => { |
| 64 | + it('returns true', () => { |
| 65 | + const result = checkAllBranch(['^test/', '/feature/'], 'head'); |
| 66 | + expect(result).toBe(true); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('and no patterns match', () => { |
| 71 | + it('returns false', () => { |
| 72 | + const result = checkAllBranch(['^feature/', '/test$'], 'head'); |
| 73 | + expect(result).toBe(false); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('when the branch to check is specified as the base branch', () => { |
| 79 | + describe('and the pattern matches the base branch', () => { |
| 80 | + it('returns true', () => { |
| 81 | + const result = checkAllBranch(['^main$'], 'base'); |
| 82 | + expect(result).toBe(true); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('checkAnyBranch', () => { |
| 89 | + beforeEach(() => { |
| 90 | + github.context.payload.pull_request!.head = { |
| 91 | + ref: 'test/feature/123' |
| 92 | + }; |
| 93 | + github.context.payload.pull_request!.base = { |
| 94 | + ref: 'main' |
| 95 | + }; |
| 96 | + }); |
| 97 | + |
| 98 | + describe('when a single pattern is provided', () => { |
| 99 | + describe('and the pattern matches the head branch', () => { |
| 100 | + it('returns true', () => { |
| 101 | + const result = checkAnyBranch(['^test'], 'head'); |
| 102 | + expect(result).toBe(true); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('and the pattern does not match the head branch', () => { |
| 107 | + it('returns false', () => { |
| 108 | + const result = checkAnyBranch(['^feature/'], 'head'); |
| 109 | + expect(result).toBe(false); |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('when multiple patterns are provided', () => { |
| 115 | + describe('and at least one pattern matches', () => { |
| 116 | + it('returns true', () => { |
| 117 | + const result = checkAnyBranch(['^test/', '^feature/'], 'head'); |
| 118 | + expect(result).toBe(true); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('and all patterns match', () => { |
| 123 | + it('returns true', () => { |
| 124 | + const result = checkAnyBranch(['^test/', '/feature/'], 'head'); |
| 125 | + expect(result).toBe(true); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('and no patterns match', () => { |
| 130 | + it('returns false', () => { |
| 131 | + const result = checkAnyBranch(['^feature/', '/test$'], 'head'); |
| 132 | + expect(result).toBe(false); |
| 133 | + }); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('when the branch to check is specified as the base branch', () => { |
| 138 | + describe('and the pattern matches the base branch', () => { |
| 139 | + it('returns true', () => { |
| 140 | + const result = checkAnyBranch(['^main$'], 'base'); |
| 141 | + expect(result).toBe(true); |
| 142 | + }); |
| 143 | + }); |
| 144 | + }); |
| 145 | +}); |
| 146 | + |
| 147 | +describe('toBranchMatchConfig', () => { |
| 148 | + describe('when there are no branch keys in the config', () => { |
| 149 | + const config = {'changed-files': [{any: ['testing']}]}; |
| 150 | + |
| 151 | + it('returns an empty object', () => { |
| 152 | + const result = toBranchMatchConfig(config); |
| 153 | + expect(result).toEqual({}); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('when the config contains a head-branch option', () => { |
| 158 | + const config = {'head-branch': ['testing']}; |
| 159 | + |
| 160 | + it('sets headBranch in the matchConfig', () => { |
| 161 | + const result = toBranchMatchConfig(config); |
| 162 | + expect(result).toEqual<BranchMatchConfig>({ |
| 163 | + headBranch: ['testing'] |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('and the matching option is a string', () => { |
| 168 | + const stringConfig = {'head-branch': 'testing'}; |
| 169 | + |
| 170 | + it('sets headBranch in the matchConfig', () => { |
| 171 | + const result = toBranchMatchConfig(stringConfig); |
| 172 | + expect(result).toEqual<BranchMatchConfig>({ |
| 173 | + headBranch: ['testing'] |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('when the config contains a base-branch option', () => { |
| 180 | + const config = {'base-branch': ['testing']}; |
| 181 | + it('sets baseBranch in the matchConfig', () => { |
| 182 | + const result = toBranchMatchConfig(config); |
| 183 | + expect(result).toEqual<BranchMatchConfig>({ |
| 184 | + baseBranch: ['testing'] |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('and the matching option is a string', () => { |
| 189 | + const stringConfig = {'base-branch': 'testing'}; |
| 190 | + |
| 191 | + it('sets baseBranch in the matchConfig', () => { |
| 192 | + const result = toBranchMatchConfig(stringConfig); |
| 193 | + expect(result).toEqual<BranchMatchConfig>({ |
| 194 | + baseBranch: ['testing'] |
| 195 | + }); |
| 196 | + }); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe('when the config contains both a base-branch and head-branch option', () => { |
| 201 | + const config = {'base-branch': ['testing'], 'head-branch': ['testing']}; |
| 202 | + it('sets headBranch and baseBranch in the matchConfig', () => { |
| 203 | + const result = toBranchMatchConfig(config); |
| 204 | + expect(result).toEqual<BranchMatchConfig>({ |
| 205 | + baseBranch: ['testing'], |
| 206 | + headBranch: ['testing'] |
| 207 | + }); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments