|
| 1 | +import td from 'testdouble' |
| 2 | +import childProcess from 'child_process' |
| 3 | +import { IServiceParams, UploaderInputs } from '../../src/types' |
| 4 | +import { createEmptyArgs } from '../test_helpers' |
| 5 | + |
| 6 | +import * as providerBitrise from '../../src/ci_providers//provider_bitrise' |
| 7 | + |
| 8 | +describe('Bitrise Params', () => { |
| 9 | + afterEach(() => { |
| 10 | + td.reset() |
| 11 | + }) |
| 12 | + |
| 13 | + describe('detect()', () => { |
| 14 | + it('does not run without Bitrise env variable', () => { |
| 15 | + const inputs: UploaderInputs = { |
| 16 | + args: { ...createEmptyArgs() }, |
| 17 | + environment: {}, |
| 18 | + } |
| 19 | + const detected = providerBitrise.detect(inputs.environment) |
| 20 | + expect(detected).toBeFalsy() |
| 21 | + }) |
| 22 | + |
| 23 | + it('does not run with only CI env variable', () => { |
| 24 | + const inputs: UploaderInputs= { |
| 25 | + args: { ...createEmptyArgs() }, |
| 26 | + environment: { |
| 27 | + CI: 'true', |
| 28 | + }, |
| 29 | + } |
| 30 | + const detected = providerBitrise.detect(inputs.environment) |
| 31 | + expect(detected).toBeFalsy() |
| 32 | + }) |
| 33 | + |
| 34 | + it('runs with Bitrise env variables', () => { |
| 35 | + const inputs: UploaderInputs= { |
| 36 | + args: { ...createEmptyArgs() }, |
| 37 | + environment: { |
| 38 | + BITRISE_IO: 'true', |
| 39 | + CI: 'true', |
| 40 | + }, |
| 41 | + } |
| 42 | + const detected = providerBitrise.detect(inputs.environment) |
| 43 | + expect(detected).toBeTruthy() |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + // This should test that the provider outputs proper default values |
| 48 | + it('gets the correct params on no env variables', () => { |
| 49 | + const inputs: UploaderInputs = { |
| 50 | + args: { ...createEmptyArgs() }, |
| 51 | + environment: { |
| 52 | + BITRISE_IO: 'true', |
| 53 | + CI: 'true', |
| 54 | + }, |
| 55 | + } |
| 56 | + const expected: IServiceParams = { |
| 57 | + branch: '', |
| 58 | + build: '', |
| 59 | + buildURL: '', |
| 60 | + commit: '', |
| 61 | + job: '', |
| 62 | + pr: '', |
| 63 | + service: 'bitrise', |
| 64 | + slug: '', |
| 65 | + } |
| 66 | + const spawnSync = td.replace(childProcess, 'spawnSync') |
| 67 | + td.when( |
| 68 | + spawnSync('git', ['config', '--get', 'remote.origin.url']), |
| 69 | + ).thenReturn({ stdout: '' }) |
| 70 | + |
| 71 | + const params = providerBitrise.getServiceParams(inputs) |
| 72 | + expect(params).toMatchObject(expected) |
| 73 | + }) |
| 74 | + |
| 75 | + // This should test that the provider outputs proper parameters when a push event is created |
| 76 | + it('gets the correct params on push', () => { |
| 77 | + const inputs: UploaderInputs = { |
| 78 | + args: { ...createEmptyArgs() }, |
| 79 | + environment: { |
| 80 | + BITRISE_BUILD_NUMBER: '2', |
| 81 | + BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2', |
| 82 | + BITRISE_GIT_BRANCH: 'main', |
| 83 | + BITRISE_IO: 'true', |
| 84 | + CI: 'true', |
| 85 | + GIT_CLONE_COMMIT_HASH: 'testingSha', |
| 86 | + }, |
| 87 | + } |
| 88 | + const expected: IServiceParams = { |
| 89 | + branch: 'main', |
| 90 | + build: '2', |
| 91 | + buildURL: 'https://bitrise.com/testOrg/testRepo/2', |
| 92 | + commit: 'testingSha', |
| 93 | + job: '', |
| 94 | + pr: '', |
| 95 | + service: 'bitrise', |
| 96 | + slug: 'testOrg/testRepo', |
| 97 | + } |
| 98 | + const spawnSync = td.replace(childProcess, 'spawnSync') |
| 99 | + td.when( |
| 100 | + spawnSync('git', ['config', '--get', 'remote.origin.url']), |
| 101 | + ).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' }) |
| 102 | + const params = providerBitrise.getServiceParams(inputs) |
| 103 | + expect(params).toMatchObject(expected) |
| 104 | + }) |
| 105 | + |
| 106 | + it('gets the correct params on pr', () => { |
| 107 | + const inputs: UploaderInputs = { |
| 108 | + args: { ...createEmptyArgs() }, |
| 109 | + environment: { |
| 110 | + BITRISE_BUILD_NUMBER: '2', |
| 111 | + BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2', |
| 112 | + BITRISE_GIT_BRANCH: 'main', |
| 113 | + BITRISE_IO: 'true', |
| 114 | + BITRISE_PULL_REQUEST: '3', |
| 115 | + CI: 'true', |
| 116 | + GIT_CLONE_COMMIT_HASH: 'testingSha', |
| 117 | + }, |
| 118 | + } |
| 119 | + const expected: IServiceParams = { |
| 120 | + branch: 'main', |
| 121 | + build: '2', |
| 122 | + buildURL: 'https://bitrise.com/testOrg/testRepo/2', |
| 123 | + commit: 'testingSha', |
| 124 | + job: '', |
| 125 | + pr: '3', |
| 126 | + service: 'bitrise', |
| 127 | + slug: 'testOrg/testRepo', |
| 128 | + } |
| 129 | + const spawnSync = td.replace(childProcess, 'spawnSync') |
| 130 | + td.when( |
| 131 | + spawnSync('git', ['config', '--get', 'remote.origin.url']), |
| 132 | + ).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' }) |
| 133 | + const params = providerBitrise.getServiceParams(inputs) |
| 134 | + expect(params).toMatchObject(expected) |
| 135 | + }) |
| 136 | + |
| 137 | + // This should test that the provider outputs proper parameters when given overrides |
| 138 | + it('gets the correct params on overrides', () => { |
| 139 | + const inputs: UploaderInputs = { |
| 140 | + args: { |
| 141 | + ...createEmptyArgs(), |
| 142 | + ...{ |
| 143 | + branch: 'test', |
| 144 | + build: '10', |
| 145 | + pr: '11', |
| 146 | + sha: 'otherTestingSha', |
| 147 | + slug: 'neworg/newRepo', |
| 148 | + }, |
| 149 | + }, |
| 150 | + environment: { |
| 151 | + BITRISE_BUILD_NUMBER: '2', |
| 152 | + BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2', |
| 153 | + BITRISE_GIT_BRANCH: 'main', |
| 154 | + BITRISE_IO: 'true', |
| 155 | + BITRISE_PULL_REQUEST: '3', |
| 156 | + CI: 'true', |
| 157 | + GIT_CLONE_COMMIT_HASH: 'testingSha', |
| 158 | + }, |
| 159 | + } |
| 160 | + const expected: IServiceParams = { |
| 161 | + branch: 'test', |
| 162 | + build: '10', |
| 163 | + buildURL: 'https://bitrise.com/testOrg/testRepo/2', |
| 164 | + commit: 'otherTestingSha', |
| 165 | + job: '', |
| 166 | + pr: '11', |
| 167 | + service: 'bitrise', |
| 168 | + slug: 'neworg/newRepo', |
| 169 | + } |
| 170 | + const spawnSync = td.replace(childProcess, 'spawnSync') |
| 171 | + td.when( |
| 172 | + spawnSync('git', ['config', '--get', 'remote.origin.url']), |
| 173 | + ).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' }) |
| 174 | + const params = providerBitrise.getServiceParams(inputs) |
| 175 | + expect(params).toMatchObject(expected) |
| 176 | + }) |
| 177 | +}) |
0 commit comments