|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { existsTag, pushTags } from './push-tags'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import { MONGOSH_RELEASE_PACKAGES } from './constants'; |
| 5 | + |
| 6 | +describe('pushing tags', function () { |
| 7 | + let spawnSync: sinon.SinonStub; |
| 8 | + let listNpmPackages: sinon.SinonStub; |
| 9 | + |
| 10 | + describe('existsTag', function () { |
| 11 | + it('returns true with existing tags', function () { |
| 12 | + expect(existsTag('v1.0.0')).equals(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it('return false with tags that do not exist', function () { |
| 16 | + expect(existsTag('this-tag-will-never-exist-12345')).equals(false); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('pushTags', function () { |
| 21 | + const mongoshVersion = '1.2.0'; |
| 22 | + const allReleasablePackages = [ |
| 23 | + { name: 'packageA', version: '0.7.0' }, |
| 24 | + { name: 'packageB', version: '1.7.0' }, |
| 25 | + { name: 'packageC', version: '1.3.0' }, |
| 26 | + { name: 'mongosh', version: mongoshVersion }, |
| 27 | + { name: '@mongosh/cli-repl', version: mongoshVersion }, |
| 28 | + ]; |
| 29 | + const auxiliaryPackages = allReleasablePackages.filter( |
| 30 | + (p) => !MONGOSH_RELEASE_PACKAGES.includes(p.name) |
| 31 | + ); |
| 32 | + const mongoshReleasePackages = allReleasablePackages.filter((p) => |
| 33 | + MONGOSH_RELEASE_PACKAGES.includes(p.name) |
| 34 | + ); |
| 35 | + let existsVersionTag: sinon.SinonStub; |
| 36 | + |
| 37 | + beforeEach(function () { |
| 38 | + spawnSync = sinon.stub(); |
| 39 | + spawnSync.returns(undefined); |
| 40 | + |
| 41 | + listNpmPackages = sinon.stub(); |
| 42 | + listNpmPackages.returns(allReleasablePackages); |
| 43 | + existsVersionTag = sinon.stub(); |
| 44 | + existsVersionTag.returns(false); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(function () { |
| 48 | + sinon.restore(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('throws if mongosh is not existent when publishing all', function () { |
| 52 | + const packages = [{ name: 'packageA', version: '0.7.0' }]; |
| 53 | + listNpmPackages.returns(packages); |
| 54 | + |
| 55 | + expect(() => |
| 56 | + pushTags( |
| 57 | + { useAuxiliaryPackagesOnly: false }, |
| 58 | + listNpmPackages, |
| 59 | + existsVersionTag, |
| 60 | + spawnSync |
| 61 | + ) |
| 62 | + ).throws('mongosh package not found'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('takes mongosh version and pushes tags when releasing', function () { |
| 66 | + pushTags( |
| 67 | + { |
| 68 | + useAuxiliaryPackagesOnly: false, |
| 69 | + }, |
| 70 | + listNpmPackages, |
| 71 | + existsVersionTag, |
| 72 | + spawnSync |
| 73 | + ); |
| 74 | + |
| 75 | + for (const packageInfo of allReleasablePackages) { |
| 76 | + expect(spawnSync).calledWith('git', [ |
| 77 | + 'tag', |
| 78 | + '-a', |
| 79 | + `${packageInfo.name}@${packageInfo.version}`, |
| 80 | + '-m', |
| 81 | + `${packageInfo.name}@${packageInfo.version}`, |
| 82 | + ]); |
| 83 | + } |
| 84 | + |
| 85 | + expect(spawnSync).calledWith('git', [ |
| 86 | + 'tag', |
| 87 | + '-a', |
| 88 | + `v${mongoshVersion}`, |
| 89 | + '-m', |
| 90 | + `v${mongoshVersion}`, |
| 91 | + ]); |
| 92 | + expect(spawnSync).calledWith('git', ['push', '--follow-tags']); |
| 93 | + }); |
| 94 | + |
| 95 | + it('pushes only package tags when using auxiliary packages', function () { |
| 96 | + pushTags( |
| 97 | + { |
| 98 | + useAuxiliaryPackagesOnly: true, |
| 99 | + }, |
| 100 | + listNpmPackages, |
| 101 | + existsVersionTag, |
| 102 | + spawnSync |
| 103 | + ); |
| 104 | + |
| 105 | + for (const packageInfo of auxiliaryPackages) { |
| 106 | + expect(spawnSync).calledWith('git', [ |
| 107 | + 'tag', |
| 108 | + '-a', |
| 109 | + `${packageInfo.name}@${packageInfo.version}`, |
| 110 | + '-m', |
| 111 | + `${packageInfo.name}@${packageInfo.version}`, |
| 112 | + ]); |
| 113 | + } |
| 114 | + |
| 115 | + for (const packageInfo of mongoshReleasePackages) { |
| 116 | + expect(spawnSync).not.calledWith('git', [ |
| 117 | + 'tag', |
| 118 | + '-a', |
| 119 | + `${packageInfo.name}@${packageInfo.version}`, |
| 120 | + '-m', |
| 121 | + `${packageInfo.name}@${packageInfo.version}`, |
| 122 | + ]); |
| 123 | + } |
| 124 | + |
| 125 | + expect(spawnSync).not.calledWith('git', [ |
| 126 | + 'tag', |
| 127 | + '-a', |
| 128 | + `v${mongoshVersion}`, |
| 129 | + '-m', |
| 130 | + `v${mongoshVersion}`, |
| 131 | + ]); |
| 132 | + expect(spawnSync).calledWith('git', ['push', '--follow-tags']); |
| 133 | + }); |
| 134 | + |
| 135 | + it('skips pushing version tags which already exist', function () { |
| 136 | + const packagesToSkip = [ |
| 137 | + allReleasablePackages[0], |
| 138 | + allReleasablePackages[1], |
| 139 | + ]; |
| 140 | + |
| 141 | + for (const packageInfo of packagesToSkip) { |
| 142 | + existsVersionTag |
| 143 | + .withArgs(`${packageInfo.name}@${packageInfo.version}`) |
| 144 | + .returns(true); |
| 145 | + } |
| 146 | + |
| 147 | + pushTags( |
| 148 | + { |
| 149 | + useAuxiliaryPackagesOnly: true, |
| 150 | + }, |
| 151 | + listNpmPackages, |
| 152 | + existsVersionTag, |
| 153 | + spawnSync |
| 154 | + ); |
| 155 | + |
| 156 | + for (const packageInfo of auxiliaryPackages.filter( |
| 157 | + (p) => !packagesToSkip.includes(p) |
| 158 | + )) { |
| 159 | + expect(spawnSync).calledWith('git', [ |
| 160 | + 'tag', |
| 161 | + '-a', |
| 162 | + `${packageInfo.name}@${packageInfo.version}`, |
| 163 | + '-m', |
| 164 | + `${packageInfo.name}@${packageInfo.version}`, |
| 165 | + ]); |
| 166 | + } |
| 167 | + |
| 168 | + for (const packageInfo of [ |
| 169 | + ...mongoshReleasePackages, |
| 170 | + ...packagesToSkip, |
| 171 | + ]) { |
| 172 | + expect(spawnSync).not.calledWith('git', [ |
| 173 | + 'tag', |
| 174 | + '-a', |
| 175 | + `${packageInfo.name}@${packageInfo.version}`, |
| 176 | + '-m', |
| 177 | + `${packageInfo.name}@${packageInfo.version}`, |
| 178 | + ]); |
| 179 | + } |
| 180 | + |
| 181 | + expect(spawnSync).not.calledWith('git', [ |
| 182 | + 'tag', |
| 183 | + '-a', |
| 184 | + `v${mongoshVersion}`, |
| 185 | + '-m', |
| 186 | + `v${mongoshVersion}`, |
| 187 | + ]); |
| 188 | + expect(spawnSync).calledWith('git', ['push', '--follow-tags']); |
| 189 | + }); |
| 190 | + |
| 191 | + it('skips mongosh release tag push if it exists', function () { |
| 192 | + existsVersionTag.withArgs(`v${mongoshVersion}`).returns(true); |
| 193 | + |
| 194 | + pushTags( |
| 195 | + { |
| 196 | + useAuxiliaryPackagesOnly: false, |
| 197 | + }, |
| 198 | + listNpmPackages, |
| 199 | + existsVersionTag, |
| 200 | + spawnSync |
| 201 | + ); |
| 202 | + |
| 203 | + expect(spawnSync).not.calledWith('git', [ |
| 204 | + 'tag', |
| 205 | + '-a', |
| 206 | + `v${mongoshVersion}`, |
| 207 | + '-m', |
| 208 | + `v${mongoshVersion}`, |
| 209 | + ]); |
| 210 | + expect(spawnSync).calledWith('git', ['push', '--follow-tags']); |
| 211 | + }); |
| 212 | + }); |
| 213 | +}); |
0 commit comments