version-bump-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { expect } from 'chai';
  2. import { GitProcess, IGitExecutionOptions, IGitResult } from 'dugite';
  3. import { nextVersion, shouldUpdateSupported, updateSupported } from '../script/release/version-bumper';
  4. import * as utils from '../script/release/version-utils';
  5. import * as sinon from 'sinon';
  6. import { ifdescribe } from './spec-helpers';
  7. const { promises: fs } = require('fs');
  8. const path = require('path');
  9. const fixtureDir = path.resolve(__dirname, 'fixtures', 'version-bumper', 'fixture_support.md');
  10. const readFile = fs.readFile;
  11. const writeFile = fs.writeFile;
  12. class GitFake {
  13. branches: {
  14. [key: string]: string[],
  15. };
  16. constructor () {
  17. this.branches = {};
  18. }
  19. setBranch (channel: string): void {
  20. this.branches[channel] = [];
  21. }
  22. setVersion (channel: string, latestTag: string): void {
  23. const tags = [latestTag];
  24. if (channel === 'alpha') {
  25. const versionStrs = latestTag.split(`${channel}.`);
  26. const latest = parseInt(versionStrs[1]);
  27. for (let i = latest; i >= 1; i--) {
  28. tags.push(`${versionStrs[0]}${channel}.${latest - i}`);
  29. }
  30. }
  31. this.branches[channel] = tags;
  32. }
  33. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  34. exec (args: string[], path: string, options?: IGitExecutionOptions | undefined): Promise<IGitResult> {
  35. let stdout = '';
  36. const stderr = '';
  37. const exitCode = 0;
  38. // handle for promoting from current master HEAD
  39. let branch = 'stable';
  40. const v = (args[2] === 'HEAD') ? 'stable' : args[3];
  41. if (v.includes('nightly')) branch = 'nightly';
  42. if (v.includes('alpha')) branch = 'alpha';
  43. if (v.includes('beta')) branch = 'beta';
  44. if (!this.branches[branch]) this.setBranch(branch);
  45. stdout = this.branches[branch].join('\n');
  46. return Promise.resolve({ exitCode, stdout, stderr });
  47. }
  48. }
  49. describe('version-bumper', () => {
  50. describe('makeVersion', () => {
  51. it('makes a version with a period delimiter', () => {
  52. const components = {
  53. major: 2,
  54. minor: 0,
  55. patch: 0
  56. };
  57. const version = utils.makeVersion(components, '.');
  58. expect(version).to.equal('2.0.0');
  59. });
  60. it('makes a version with a period delimiter and a partial pre', () => {
  61. const components = {
  62. major: 2,
  63. minor: 0,
  64. patch: 0,
  65. pre: ['nightly', 12345678]
  66. };
  67. const version = utils.makeVersion(components, '.', utils.preType.PARTIAL);
  68. expect(version).to.equal('2.0.0.12345678');
  69. });
  70. it('makes a version with a period delimiter and a full pre', () => {
  71. const components = {
  72. major: 2,
  73. minor: 0,
  74. patch: 0,
  75. pre: ['nightly', 12345678]
  76. };
  77. const version = utils.makeVersion(components, '.', utils.preType.FULL);
  78. expect(version).to.equal('2.0.0-nightly.12345678');
  79. });
  80. });
  81. describe('updateSupported', () => {
  82. let restore: any;
  83. before(async () => {
  84. restore = await readFile(fixtureDir, 'utf8');
  85. });
  86. afterEach(async () => {
  87. await writeFile(fixtureDir, restore, 'utf8');
  88. });
  89. it('updates correctly when a new stable version is promoted from beta', async () => {
  90. const version = '4.0.0';
  91. const currentVersion = '4.0.0-beta.29';
  92. if (shouldUpdateSupported('stable', currentVersion, version)) {
  93. await updateSupported(version, fixtureDir);
  94. }
  95. const contents = await readFile(fixtureDir, 'utf8');
  96. expect(contents).to.contain('4.x.y\n* 3.x.y\n* 2.x.y');
  97. });
  98. it('should not update when a new stable patch version is promoted', async () => {
  99. const version = '3.0.1';
  100. const currentVersion = '3.0.0';
  101. if (shouldUpdateSupported('stable', currentVersion, version)) {
  102. await updateSupported(version, fixtureDir);
  103. }
  104. const contents = await readFile(fixtureDir, 'utf8');
  105. expect(contents).to.contain('3.x.y\n* 2.x.y\n* 1.x.y');
  106. });
  107. it('should not update when a new stable minor version is promoted', async () => {
  108. const version = '3.1.0';
  109. const currentVersion = '3.0.0';
  110. if (shouldUpdateSupported('minor', currentVersion, version)) {
  111. await updateSupported(version, fixtureDir);
  112. }
  113. const contents = await readFile(fixtureDir, 'utf8');
  114. expect(contents).to.contain('3.x.y\n* 2.x.y\n* 1.x.y');
  115. });
  116. it('should not update when a new beta.1 version is promoted', async () => {
  117. const version = '5.0.0-beta.1';
  118. const currentVersion = '4.0.0-beta.29';
  119. if (shouldUpdateSupported('beta', currentVersion, version)) {
  120. await updateSupported(version, fixtureDir);
  121. }
  122. const contents = await readFile(fixtureDir, 'utf8');
  123. expect(contents).to.contain('3.x.y\n* 2.x.y\n* 1.x.y');
  124. });
  125. it('should not update when a new beta.12 version is promoted', async () => {
  126. const version = '4.0.0-beta.12';
  127. const currentVersion = '4.0.0-beta.11';
  128. if (shouldUpdateSupported('beta', currentVersion, version)) {
  129. await updateSupported(version, fixtureDir);
  130. }
  131. const contents = await readFile(fixtureDir, 'utf8');
  132. expect(contents).to.contain('3.x.y\n* 2.x.y\n* 1.x.y');
  133. });
  134. it('should update when a new major nightly version is promoted', async () => {
  135. const version = '4.0.0-nightly.19950901';
  136. const currentVersion = '3.0.0-nightly.19950828';
  137. if (shouldUpdateSupported('nightly', currentVersion, version)) {
  138. await updateSupported(version, fixtureDir);
  139. }
  140. const contents = await readFile(fixtureDir, 'utf8');
  141. expect(contents).to.contain('4.x.y\n* 3.x.y\n* 2.x.y');
  142. });
  143. it('should not update when a new nightly version is promoted', async () => {
  144. const version = '3.0.0-nightly.19950901';
  145. const currentVersion = '3.0.0-nightly.19950828';
  146. if (shouldUpdateSupported('nightly', currentVersion, version)) {
  147. await updateSupported(version, fixtureDir);
  148. }
  149. const contents = await readFile(fixtureDir, 'utf8');
  150. expect(contents).to.contain('3.x.y\n* 2.x.y\n* 1.x.y');
  151. });
  152. });
  153. // On macOS Circle CI we don't have a real git environment due to running
  154. // gclient sync on a linux machine. These tests therefore don't run as expected.
  155. ifdescribe(!(process.platform === 'linux' && process.arch.indexOf('arm') === 0) && process.platform !== 'darwin')('nextVersion', () => {
  156. describe('bump versions', () => {
  157. const nightlyPattern = /[0-9.]*(-nightly.(\d{4})(\d{2})(\d{2}))$/g;
  158. const betaPattern = /[0-9.]*(-beta[0-9.]*)/g;
  159. it('bumps to nightly from stable', async () => {
  160. const version = 'v2.0.0';
  161. const next = await nextVersion('nightly', version);
  162. const matches = next.match(nightlyPattern);
  163. expect(matches).to.have.lengthOf(1);
  164. });
  165. it('bumps to nightly from beta', async () => {
  166. const version = 'v2.0.0-beta.1';
  167. const next = await nextVersion('nightly', version);
  168. const matches = next.match(nightlyPattern);
  169. expect(matches).to.have.lengthOf(1);
  170. });
  171. it('bumps to nightly from nightly', async () => {
  172. const version = 'v2.0.0-nightly.19950901';
  173. const next = await nextVersion('nightly', version);
  174. const matches = next.match(nightlyPattern);
  175. expect(matches).to.have.lengthOf(1);
  176. });
  177. it('bumps to a nightly version above our switch from N-0-x to N-x-y branch names', async () => {
  178. const version = 'v2.0.0-nightly.19950901';
  179. const next = await nextVersion('nightly', version);
  180. // If it starts with v8 then we didn't bump above the 8-x-y branch
  181. expect(next.startsWith('v8')).to.equal(false);
  182. });
  183. it('throws error when bumping to beta from stable', () => {
  184. const version = 'v2.0.0';
  185. return expect(
  186. nextVersion('beta', version)
  187. ).to.be.rejectedWith('Cannot bump to beta from stable.');
  188. });
  189. // TODO ELECTRON 15: Re-enable after Electron 15 alpha has released
  190. it.skip('bumps to beta from nightly', async () => {
  191. const version = 'v2.0.0-nightly.19950901';
  192. const next = await nextVersion('beta', version);
  193. const matches = next.match(betaPattern);
  194. expect(matches).to.have.lengthOf(1);
  195. });
  196. it('bumps to beta from beta', async () => {
  197. const version = 'v2.0.0-beta.8';
  198. const next = await nextVersion('beta', version);
  199. expect(next).to.equal('2.0.0-beta.9');
  200. });
  201. it('bumps to beta from beta if the previous beta is at least beta.10', async () => {
  202. const version = 'v6.0.0-beta.15';
  203. const next = await nextVersion('beta', version);
  204. expect(next).to.equal('6.0.0-beta.16');
  205. });
  206. it('bumps to stable from beta', async () => {
  207. const version = 'v2.0.0-beta.1';
  208. const next = await nextVersion('stable', version);
  209. expect(next).to.equal('2.0.0');
  210. });
  211. it('bumps to stable from stable', async () => {
  212. const version = 'v2.0.0';
  213. const next = await nextVersion('stable', version);
  214. expect(next).to.equal('2.0.1');
  215. });
  216. it('bumps to minor from stable', async () => {
  217. const version = 'v2.0.0';
  218. const next = await nextVersion('minor', version);
  219. expect(next).to.equal('2.1.0');
  220. });
  221. it('bumps to stable from nightly', async () => {
  222. const version = 'v2.0.0-nightly.19950901';
  223. const next = await nextVersion('stable', version);
  224. expect(next).to.equal('2.0.0');
  225. });
  226. it('throws on an invalid version', () => {
  227. const version = 'vI.AM.INVALID';
  228. return expect(
  229. nextVersion('beta', version)
  230. ).to.be.rejectedWith(`Invalid current version: ${version}`);
  231. });
  232. it('throws on an invalid bump type', () => {
  233. const version = 'v2.0.0';
  234. return expect(
  235. nextVersion('WRONG', version)
  236. ).to.be.rejectedWith('Invalid bump type.');
  237. });
  238. });
  239. });
  240. // If we don't plan on continuing to support an alpha channel past Electron 15,
  241. // these tests will be removed. Otherwise, integrate into the bump versions tests
  242. describe('bump versions - alpha channel', () => {
  243. const alphaPattern = /[0-9.]*(-alpha[0-9.]*)/g;
  244. const betaPattern = /[0-9.]*(-beta[0-9.]*)/g;
  245. const sandbox = sinon.createSandbox();
  246. const gitFake = new GitFake();
  247. beforeEach(() => {
  248. const wrapper = (args: string[], path: string, options?: IGitExecutionOptions | undefined) => gitFake.exec(args, path, options);
  249. sandbox.replace(GitProcess, 'exec', wrapper);
  250. });
  251. afterEach(() => {
  252. gitFake.branches = {};
  253. sandbox.restore();
  254. });
  255. it('bumps to alpha from nightly', async () => {
  256. const version = 'v2.0.0-nightly.19950901';
  257. gitFake.setVersion('nightly', version);
  258. const next = await nextVersion('alpha', version);
  259. const matches = next.match(alphaPattern);
  260. expect(matches).to.have.lengthOf(1);
  261. });
  262. it('throws error when bumping to alpha from stable', () => {
  263. const version = 'v2.0.0';
  264. return expect(
  265. nextVersion('alpha', version)
  266. ).to.be.rejectedWith('Cannot bump to alpha from stable.');
  267. });
  268. it('bumps to alpha from alpha', async () => {
  269. const version = 'v2.0.0-alpha.8';
  270. gitFake.setVersion('alpha', version);
  271. const next = await nextVersion('alpha', version);
  272. expect(next).to.equal('2.0.0-alpha.9');
  273. });
  274. it('bumps to alpha from alpha if the previous alpha is at least alpha.10', async () => {
  275. const version = 'v6.0.0-alpha.15';
  276. gitFake.setVersion('alpha', version);
  277. const next = await nextVersion('alpha', version);
  278. expect(next).to.equal('6.0.0-alpha.16');
  279. });
  280. it('bumps to beta from alpha', async () => {
  281. const version = 'v2.0.0-alpha.8';
  282. gitFake.setVersion('alpha', version);
  283. const next = await nextVersion('beta', version);
  284. const matches = next.match(betaPattern);
  285. expect(matches).to.have.lengthOf(1);
  286. expect(next).to.equal('2.0.0-beta.1');
  287. });
  288. });
  289. });