@vida-global/release 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,28 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Node.js Package
5
+
6
+ on:
7
+ push:
8
+ branches:
9
+ - release/production
10
+
11
+ permissions:
12
+ id-token: write
13
+ contents: read
14
+
15
+ jobs:
16
+ publish:
17
+ runs-on: ubuntu-latest
18
+ steps:
19
+ - uses: actions/checkout@v4
20
+ - uses: actions/setup-node@v4
21
+ with:
22
+ node-version: '24'
23
+ registry-url: 'https://registry.npmjs.org'
24
+ - run: npm ci
25
+ env:
26
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_READ_TOKEN }}
27
+ - run: npm test
28
+ - run: npm publish
package/package.json CHANGED
@@ -1,15 +1,31 @@
1
1
  {
2
2
  "name": "@vida-global/release",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Tools for releasing Vida tools",
5
5
  "author": "",
6
6
  "license": "ISC",
7
7
  "main": "index.js",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/VIDA-Global/release"
11
+ },
12
+ "scripts": {
13
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules node_modules/jest/bin/jest.js"
14
+ },
15
+ "jest": {
16
+ "collectCoverage": true,
17
+ "coveragePathIgnorePatterns": [
18
+ "/helpers/"
19
+ ],
20
+ "setupFilesAfterEnv": ["jest-extended/all"]
21
+ },
8
22
  "dependencies": {
9
23
  "commander": "^13.1.0",
10
24
  "pino": "^9.6.0",
11
25
  "pino-pretty": "^13.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "jest": "^29.6.2",
29
+ "jest-extended": "^7.0.0"
12
30
  }
13
31
  }
14
-
15
-
@@ -0,0 +1,57 @@
1
+ const { develop } = require('../lib/release/develop');
2
+ const Git = require('../lib/release/git');
3
+ const TestHelpers = require('./helpers');
4
+ const utils = require('../lib/release/utils');
5
+
6
+
7
+ jest.mock('../lib/release/git', () => {
8
+ return { createBranch: jest.fn(), getCurrentUser: jest.fn() }
9
+ });
10
+ jest.mock('../lib/release/utils', () => {
11
+ return { confirmContinue: jest.fn() };
12
+ });
13
+
14
+
15
+ let branchType;
16
+ let description;
17
+ let firstName;
18
+ let issueNumber;
19
+ let lastName;
20
+ let sourceBranch;
21
+ beforeEach(() => {
22
+ jest.resetAllMocks();
23
+ utils.confirmContinue.mockImplementation(() => true);
24
+
25
+ firstName = TestHelpers.Faker.User.randomFirstName();
26
+ lastName = TestHelpers.Faker.User.randomLastName();
27
+ Git.getCurrentUser.mockImplementation(() => `${firstName} ${lastName}`);
28
+
29
+ branchType = TestHelpers.Faker.Text.randomString();
30
+ sourceBranch = TestHelpers.Faker.Text.randomString();
31
+ description = TestHelpers.Faker.Text.randomSentence();
32
+ issueNumber = TestHelpers.Faker.Math.randomNumber();
33
+ });
34
+
35
+
36
+ describe('develop', () => {
37
+ it ('confirms and creates a branch once', async () => {
38
+ await develop(branchType, issueNumber, description, sourceBranch);
39
+ expect(utils.confirmContinue).toHaveBeenCalledTimes(1);
40
+ expect(Git.createBranch).toHaveBeenCalledTimes(1);
41
+ });
42
+
43
+ it ('generates the correct branch name', async () => {
44
+ const initials = `${firstName[0]}${lastName[0]}`.toLowerCase();
45
+ const branchName = `${branchType}/${issueNumber}/${initials}/${description.replace(/\s+/g, '-').toLowerCase().replace(/[^a-z0-9-]/g, '')}`;
46
+ await develop(branchType, issueNumber, description, sourceBranch);
47
+
48
+ expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to create branch ${branchName}?`);
49
+ expect(Git.createBranch).toHaveBeenCalledWith(branchName, sourceBranch);
50
+ });
51
+
52
+ it ('does not create a branch if the user does not confirm', async () => {
53
+ utils.confirmContinue.mockImplementation(() => false);
54
+ await develop(branchType, issueNumber, description, sourceBranch);
55
+ expect(Git.createBranch).not.toHaveBeenCalled();
56
+ });
57
+ });
@@ -0,0 +1,189 @@
1
+ const { execSync } = require('child_process');
2
+ const Git = require('../lib/release/git');
3
+ const TestHelpers = require('./helpers');
4
+
5
+
6
+ jest.mock('child_process', () => {
7
+ const execSync = jest.fn();
8
+ return { execSync };
9
+ });
10
+
11
+
12
+ beforeEach(() => {
13
+ jest.resetAllMocks();
14
+ });
15
+
16
+
17
+ describe('Git.add', () => {
18
+ it ('calls git add with the file name', () => {
19
+ const fileName = TestHelpers.Faker.Text.randomString();
20
+ Git.add(fileName);
21
+ expect(execSync).toHaveBeenCalledTimes(1);
22
+ expect(execSync).toHaveBeenCalledWith(`git add ${fileName}`);
23
+ });
24
+ });
25
+
26
+
27
+ describe('Git.branches', () => {
28
+ it ('calls git branch and returns an array of the resulting strings', () => {
29
+ const b1 = TestHelpers.Faker.Text.randomString();
30
+ const b2 = TestHelpers.Faker.Text.randomString();
31
+ const b3 = TestHelpers.Faker.Text.randomString();
32
+ const branchStr = ` ${b1} \n ${b2}\n${b3} `;
33
+ execSync.mockImplementation(() => branchStr);
34
+
35
+ const branches = Git.branches();
36
+
37
+ expect(execSync).toHaveBeenCalledTimes(1);
38
+ expect(branches).toEqual([b1, b2, b3]);
39
+ expect(execSync).toHaveBeenCalledWith(`git branch -r`);
40
+ });
41
+
42
+ it ('does not include the remote option when needed', () => {
43
+ execSync.mockImplementation(() => '');
44
+ const branches = Git.branches(false);
45
+ expect(execSync).toHaveBeenCalledWith(`git branch`);
46
+ });
47
+ });
48
+
49
+
50
+ describe('Git.checkout', () => {
51
+ it ('calls git checkout with the branch name', () => {
52
+ const branchName = TestHelpers.Faker.Text.randomString();
53
+ Git.checkout(branchName);
54
+ expect(execSync).toHaveBeenCalledTimes(1);
55
+ expect(execSync).toHaveBeenCalledWith(`git checkout ${branchName}`);
56
+ });
57
+ });
58
+
59
+
60
+ describe('Git.commit', () => {
61
+ it ('calls git commit with comment', () => {
62
+ const comment = TestHelpers.Faker.Text.randomString();
63
+ Git.commit(comment);
64
+ expect(execSync).toHaveBeenCalledTimes(1);
65
+ expect(execSync).toHaveBeenCalledWith(`git commit -m '${comment}'`);
66
+ });
67
+ });
68
+
69
+
70
+ describe('Git.createBranch', () => {
71
+ it ('creates a branch and switches to it', () => {
72
+ const branch1 = TestHelpers.Faker.Text.randomString();
73
+ const branch2 = TestHelpers.Faker.Text.randomString();
74
+
75
+ Git.createBranch(branch1, branch2);
76
+
77
+ expect(execSync).toHaveBeenCalledTimes(2);
78
+ expect(execSync).toHaveBeenNthCalledWith(1, `git branch ${branch1} ${branch2}`);
79
+ expect(execSync).toHaveBeenNthCalledWith(2, `git checkout ${branch1}`);
80
+ });
81
+
82
+ it ('defaults to the primary branch', () => {
83
+ const branch1 = TestHelpers.Faker.Text.randomString();
84
+
85
+ execSync.mockImplementation(() => 'origin/main');
86
+
87
+ Git.createBranch(branch1);
88
+
89
+ expect(execSync).toHaveBeenCalledTimes(3);
90
+ expect(execSync).toHaveBeenNthCalledWith(2, `git branch ${branch1} origin/main`);
91
+ });
92
+ });
93
+
94
+
95
+ describe('Git.forceRemotePush', () => {
96
+ it ('calls git push with the origin', () => {
97
+ const branch1 = TestHelpers.Faker.Text.randomString();
98
+ const branch2 = TestHelpers.Faker.Text.randomString();
99
+
100
+ Git.forceRemotePush(branch1, branch2);
101
+ expect(execSync).toHaveBeenCalledTimes(1);
102
+ expect(execSync).toHaveBeenCalledWith(`git push origin origin/${branch1}:${branch2} --force`)
103
+ });
104
+ });
105
+
106
+
107
+ describe('Git.getCurrentUser', () => {
108
+ it ('returns the result of git config.name', () => {
109
+ const name = TestHelpers.Faker.User.randomName();
110
+ execSync.mockImplementation(() => name);
111
+
112
+ const response = Git.getCurrentUser();
113
+
114
+ expect(execSync).toHaveBeenCalledTimes(1);
115
+ expect(execSync).toHaveBeenCalledWith(`git config user.name`);
116
+ expect(response).toEqual(name);
117
+ });
118
+ });
119
+
120
+
121
+ describe('Git.getPrimaryBranch', () => {
122
+ it ('returns master when that is the primary branch', () => {
123
+ const branch1 = TestHelpers.Faker.Text.randomString();
124
+ const branch2 = TestHelpers.Faker.Text.randomString();
125
+ const branchStr = ` origin/${branch1}\n origin/master \n origin/${branch2}`;
126
+ execSync.mockImplementation(() => branchStr);
127
+
128
+ const branch = Git.getPrimaryBranch();
129
+
130
+ expect(branch).toEqual('origin/master');
131
+ });
132
+
133
+ it ('returns main when that is the primary branch', () => {
134
+ const branch1 = TestHelpers.Faker.Text.randomString();
135
+ const branch2 = TestHelpers.Faker.Text.randomString();
136
+ const branchStr = `origin/${branch1}\norigin/${branch2}\n origin/main `;
137
+ execSync.mockImplementation(() => branchStr);
138
+
139
+ const branch = Git.getPrimaryBranch();
140
+
141
+ expect(branch).toEqual('origin/main');
142
+ });
143
+
144
+ it ('returns undefined when it cannot determine the primary branch', () => {
145
+ const branch1 = TestHelpers.Faker.Text.randomString();
146
+ const branch2 = TestHelpers.Faker.Text.randomString();
147
+ const branch3 = TestHelpers.Faker.Text.randomString();
148
+ const branchStr = `origin/${branch1}\norigin/${branch2}\n origin/${branch3} `;
149
+ execSync.mockImplementation(() => branchStr);
150
+
151
+ const branch = Git.getPrimaryBranch();
152
+
153
+ expect(branch).toBeUndefined();
154
+ });
155
+ });
156
+
157
+
158
+ describe('Git.getCurrentBranch', () => {
159
+ it ('returns the branch with the *', () => {
160
+ const branch1 = TestHelpers.Faker.Text.randomString();
161
+ const branch2 = TestHelpers.Faker.Text.randomString();
162
+ const branch3 = TestHelpers.Faker.Text.randomString();
163
+ const branchStr = ` origin/${branch1}\n * ${branch3} \n origin/${branch2}`;
164
+ execSync.mockImplementation(() => branchStr);
165
+
166
+ const branch = Git.getCurrentBranch();
167
+
168
+ expect(branch).toEqual(branch3);
169
+ });
170
+ });
171
+
172
+
173
+ describe('Git.pull', () => {
174
+ it ('calls git pull with the prune option', () => {
175
+ Git.pull();
176
+ expect(execSync).toHaveBeenCalledTimes(1);
177
+ expect(execSync).toHaveBeenCalledWith(`git pull --prune`);
178
+ });
179
+ });
180
+
181
+
182
+ describe('Git.push', () => {
183
+ it ('pushes to the current branch', () => {
184
+ const branch = TestHelpers.Faker.Text.randomString();
185
+ execSync.mockImplementation(() => `* ${branch}`);
186
+ Git.push();
187
+ expect(execSync).toHaveBeenCalledWith(`git push -u origin ${branch}`);
188
+ });
189
+ });
@@ -0,0 +1,63 @@
1
+ const { FirstNames, LastNames} = require('./names');
2
+
3
+
4
+ const Faker = {
5
+ Math: {
6
+ randomNumber: function(max=1000) {
7
+ return Math.ceil(Math.random() * max)
8
+ }
9
+ },
10
+
11
+
12
+ Text: {
13
+ randomString: function(length=10) {
14
+ let result = '';
15
+ const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
16
+ const charactersLength = characters.length;
17
+ while (result.length < length) {
18
+ let idx = Faker.Math.randomNumber(charactersLength-1);
19
+ result += characters.charAt(idx);
20
+ }
21
+ return result;
22
+ },
23
+
24
+
25
+ randomSentence({ numWords=5, maxWordLength=10, punctuationOptions='.!?', includeComma=true }={}) {
26
+ const words = [...Array(numWords)].map(() => Faker.Text.randomString(maxWordLength));
27
+
28
+ if (includeComma && numWords > 2) {
29
+ const commaIdx = Math.floor(Math.random() * (numWords - 1));
30
+ words[commaIdx] = `${words[commaIdx]},`;
31
+ }
32
+
33
+ const puncIdx = Math.floor(Math.random() * punctuationOptions.length);
34
+ const punctuation = punctuationOptions[puncIdx];
35
+
36
+ return words.join(' ') + punctuation;
37
+ }
38
+ },
39
+
40
+
41
+ User: {
42
+ randomName: function() {
43
+ return `${this.randomFirstName()} ${this.randomLastName()}`;
44
+ },
45
+
46
+
47
+ randomFirstName: function() {
48
+ const idx = Faker.Math.randomNumber(FirstNames.length-1);
49
+ return FirstNames[idx];
50
+ },
51
+
52
+
53
+ randomLastName: function() {
54
+ const idx = Faker.Math.randomNumber(LastNames.length-1);
55
+ return LastNames[idx];
56
+ }
57
+ }
58
+ };
59
+
60
+
61
+ module.exports = {
62
+ Faker
63
+ }
@@ -0,0 +1,311 @@
1
+ const FirstNames = [
2
+ "Aaron",
3
+ "Abigail",
4
+ "Adam",
5
+ "Alan",
6
+ "Albert",
7
+ "Alexander",
8
+ "Alexis",
9
+ "Alice",
10
+ "Amanda",
11
+ "Amber",
12
+ "Amy",
13
+ "Andrea",
14
+ "Andrew",
15
+ "Angela",
16
+ "Ann",
17
+ "Anna",
18
+ "Anthony",
19
+ "Arthur",
20
+ "Ashley",
21
+ "Austin",
22
+ "Barbara",
23
+ "Benjamin",
24
+ "Betty",
25
+ "Beverly",
26
+ "Billy",
27
+ "Bobby",
28
+ "Bradley",
29
+ "Brandon",
30
+ "Brenda",
31
+ "Brian",
32
+ "Brittany",
33
+ "Bruce",
34
+ "Bryan",
35
+ "Caleb",
36
+ "Carl",
37
+ "Carol",
38
+ "Carolyn",
39
+ "Catherine",
40
+ "Charles",
41
+ "Charlotte",
42
+ "Cheryl",
43
+ "Christian",
44
+ "Christina",
45
+ "Christine",
46
+ "Christopher",
47
+ "Cynthia",
48
+ "Daniel",
49
+ "Danielle",
50
+ "David",
51
+ "Deborah",
52
+ "Debra",
53
+ "Denise",
54
+ "Dennis",
55
+ "Diana",
56
+ "Diane",
57
+ "Donald",
58
+ "Donna",
59
+ "Doris",
60
+ "Dorothy",
61
+ "Douglas",
62
+ "Dylan",
63
+ "Edward",
64
+ "Elijah",
65
+ "Elizabeth",
66
+ "Emily",
67
+ "Emma",
68
+ "Eric",
69
+ "Ethan",
70
+ "Evelyn",
71
+ "Frances",
72
+ "Frank",
73
+ "Gabriel",
74
+ "Gary",
75
+ "George",
76
+ "Gerald",
77
+ "Gloria",
78
+ "Grace",
79
+ "Gregory",
80
+ "Hannah",
81
+ "Harold",
82
+ "Heather",
83
+ "Helen",
84
+ "Henry",
85
+ "Isabella",
86
+ "Jack",
87
+ "Jacob",
88
+ "Jacqueline",
89
+ "James",
90
+ "Janet",
91
+ "Janice",
92
+ "Jason",
93
+ "Jean",
94
+ "Jeffrey",
95
+ "Jennifer",
96
+ "Jeremy",
97
+ "Jerry",
98
+ "Jesse",
99
+ "Jessica",
100
+ "Joan",
101
+ "Joe",
102
+ "John",
103
+ "Jonathan",
104
+ "Jordan",
105
+ "Jose",
106
+ "Joseph",
107
+ "Joshua",
108
+ "Joyce",
109
+ "Juan",
110
+ "Judith",
111
+ "Judy",
112
+ "Julia",
113
+ "Julie",
114
+ "Justin",
115
+ "Karen",
116
+ "Katherine",
117
+ "Kathleen",
118
+ "Kathryn",
119
+ "Kayla",
120
+ "Keith",
121
+ "Kelly",
122
+ "Kenneth",
123
+ "Kevin",
124
+ "Kimberly",
125
+ "Kyle",
126
+ "Larry",
127
+ "Laura",
128
+ "Lauren",
129
+ "Lawrence",
130
+ "Liam",
131
+ "Linda",
132
+ "Lisa",
133
+ "Logan",
134
+ "Lori",
135
+ "Lucas",
136
+ "Madison",
137
+ "Margaret",
138
+ "Maria",
139
+ "Marilyn",
140
+ "Mark",
141
+ "Martha",
142
+ "Mary",
143
+ "Mason",
144
+ "Matthew",
145
+ "Megan",
146
+ "Melissa",
147
+ "Michael",
148
+ "Michelle",
149
+ "Nancy",
150
+ "Natalie",
151
+ "Nathan",
152
+ "Nicholas",
153
+ "Nicole",
154
+ "Noah",
155
+ "Olivia",
156
+ "Pamela",
157
+ "Patricia",
158
+ "Patrick",
159
+ "Paul",
160
+ "Peter",
161
+ "Rachel",
162
+ "Randy",
163
+ "Raymond",
164
+ "Rebecca",
165
+ "Richard",
166
+ "Robert",
167
+ "Roger",
168
+ "Ronald",
169
+ "Roy",
170
+ "Russell",
171
+ "Ruth",
172
+ "Ryan",
173
+ "Samantha",
174
+ "Samuel",
175
+ "Sandra",
176
+ "Sara",
177
+ "Sarah",
178
+ "Scott",
179
+ "Sean",
180
+ "Sharon",
181
+ "Shirley",
182
+ "Sophia",
183
+ "Stephanie",
184
+ "Stephen",
185
+ "Steven",
186
+ "Susan",
187
+ "Teresa",
188
+ "Terry",
189
+ "Theresa",
190
+ "Thomas",
191
+ "Tiffany",
192
+ "Timothy",
193
+ "Tyler",
194
+ "Victoria",
195
+ "Vincent",
196
+ "Virginia",
197
+ "Walter",
198
+ "Wayne",
199
+ "William",
200
+ "Willie",
201
+ "Zachary",
202
+ ];
203
+
204
+
205
+ const LastNames = [
206
+ "Adams",
207
+ "Allen",
208
+ "Alvarez",
209
+ "Anderson",
210
+ "Bailey",
211
+ "Baker",
212
+ "Bennett",
213
+ "Brooks",
214
+ "Brown",
215
+ "Campbell",
216
+ "Carter",
217
+ "Castillo",
218
+ "Chavez",
219
+ "Clark",
220
+ "Collins",
221
+ "Cook",
222
+ "Cooper",
223
+ "Cox",
224
+ "Cruz",
225
+ "Davis",
226
+ "Diaz",
227
+ "Edwards",
228
+ "Evans",
229
+ "Flores",
230
+ "Foster",
231
+ "Garcia",
232
+ "Gomez",
233
+ "Gonzalez",
234
+ "Gray",
235
+ "Green",
236
+ "Gutierrez",
237
+ "Hall",
238
+ "Harris",
239
+ "Hernandez",
240
+ "Hill",
241
+ "Howard",
242
+ "Hughes",
243
+ "Jackson",
244
+ "James",
245
+ "Jimenez,",
246
+ "Johnson",
247
+ "Jones",
248
+ "Kelly",
249
+ "Kim",
250
+ "King",
251
+ "Lee",
252
+ "Lewis",
253
+ "Long",
254
+ "Lopez",
255
+ "Martin",
256
+ "Martinez",
257
+ "Mendoza",
258
+ "Miller",
259
+ "Mitchell",
260
+ "Moore",
261
+ "Morales",
262
+ "Morgan",
263
+ "Morris",
264
+ "Murphy",
265
+ "Myers",
266
+ "Nelson",
267
+ "Nguyen",
268
+ "Ortiz",
269
+ "Parker",
270
+ "Patel",
271
+ "Perez",
272
+ "Peterson",
273
+ "Phillips",
274
+ "Price",
275
+ "Ramirez",
276
+ "Ramos",
277
+ "Reed",
278
+ "Reyes",
279
+ "Richardson",
280
+ "Rivera",
281
+ "Roberts",
282
+ "Robinson",
283
+ "Rodriguez",
284
+ "Rogers",
285
+ "Ross",
286
+ "Ruiz",
287
+ "Sanchez",
288
+ "Sanders",
289
+ "Scott",
290
+ "Smith",
291
+ "Stewart",
292
+ "Taylor",
293
+ "Thomas",
294
+ "Thompson",
295
+ "Torres",
296
+ "Turner",
297
+ "Walker",
298
+ "Ward",
299
+ "Watson",
300
+ "White",
301
+ "Williams",
302
+ "Wilson",
303
+ "Wood",
304
+ "Wright"
305
+ ];
306
+
307
+
308
+ module.exports = {
309
+ FirstNames,
310
+ LastNames,
311
+ };
@@ -0,0 +1,145 @@
1
+ const fs = require('fs');
2
+ const { increment } = require('../lib/release/increment');
3
+ const Git = require('../lib/release/git');
4
+ const TestHelpers = require('./helpers');
5
+ const utils = require('../lib/release/utils');
6
+
7
+
8
+ jest.mock('../lib/release/git', () => {
9
+ return { add: jest.fn(), createBranch: jest.fn(), commit: jest.fn(), pull: jest.fn(), push: jest.fn() };
10
+ });
11
+ jest.mock('../lib/release/utils', () => {
12
+ return { getCurrentVersion: jest.fn(), confirmContinue: jest.fn() };
13
+ });
14
+ jest.mock('fs', () => {
15
+ return { readFileSync: jest.fn(), writeFileSync: jest.fn() };
16
+ });
17
+
18
+
19
+ let config;
20
+ let version;
21
+ const packageJsonPath = `${process.cwd()}/package.json`;
22
+ beforeEach(() => {
23
+ jest.resetAllMocks();
24
+ version = [TestHelpers.Faker.Math.randomNumber(),
25
+ TestHelpers.Faker.Math.randomNumber(),
26
+ TestHelpers.Faker.Math.randomNumber()];
27
+ utils.getCurrentVersion.mockImplementation(() => version);
28
+ utils.confirmContinue.mockImplementation(() => true);
29
+
30
+ config = {
31
+ [TestHelpers.Faker.Text.randomString()]: TestHelpers.Faker.Text.randomString(),
32
+ [TestHelpers.Faker.Text.randomString()]: TestHelpers.Faker.Text.randomString(),
33
+ version: version.join('.')
34
+ }
35
+ fs.readFileSync.mockImplementation(() => JSON.stringify(config, null, 4));
36
+ });
37
+
38
+
39
+ describe('increment', () => {
40
+ it ('runs steps in the proper order', async () => {
41
+ await increment('point');
42
+
43
+ expect(Git.pull).toHaveBeenCalledTimes(1);
44
+ expect(utils.getCurrentVersion).toHaveBeenCalledTimes(1);
45
+ expect(Git.pull).toHaveBeenCalledBefore(utils.getCurrentVersion);
46
+
47
+ expect(utils.confirmContinue).toHaveBeenCalledTimes(1);
48
+ expect(utils.getCurrentVersion).toHaveBeenCalledBefore(utils.confirmContinue);
49
+
50
+ expect(Git.createBranch).toHaveBeenCalledTimes(1);
51
+ expect(utils.confirmContinue).toHaveBeenCalledBefore(Git.createBranch);
52
+
53
+ expect(fs.readFileSync).toHaveBeenCalledTimes(1);
54
+ expect(Git.createBranch).toHaveBeenCalledBefore(fs.readFileSync);
55
+
56
+ expect(fs.writeFileSync).toHaveBeenCalledTimes(1);
57
+ expect(fs.readFileSync).toHaveBeenCalledBefore(fs.writeFileSync);
58
+
59
+ expect(Git.push).toHaveBeenCalledTimes(1);
60
+ expect(fs.writeFileSync).toHaveBeenCalledBefore(Git.push);
61
+ });
62
+
63
+
64
+ describe('getNewVersion', () => {
65
+ it ('increments the major version', async () => {
66
+ await increment('major');
67
+ const newVersion = [...version];
68
+ newVersion[0] = newVersion[0] + 1;
69
+ newVersion[1] = 0;
70
+ newVersion[2] = 0;
71
+ expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
72
+ });
73
+
74
+ it ('increments the minor version', async () => {
75
+ await increment('minor');
76
+ const newVersion = [...version];
77
+ newVersion[1] = newVersion[1] + 1;
78
+ newVersion[2] = 0;
79
+ expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
80
+ });
81
+
82
+ it ('increments the point version', async () => {
83
+ await increment('point');
84
+ const newVersion = [...version];
85
+ newVersion[2] = newVersion[2] + 1;
86
+ expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion.join('.')}`);
87
+ });
88
+
89
+ it ('throws an error for an invalid type', async () => {
90
+ expect(async () => {await increment('foo')}).rejects.toThrow();;
91
+ });
92
+ });
93
+
94
+
95
+ describe('confirmation', () => {
96
+ it ('asks the user if they want to upgrade to the new version', async () => {
97
+ await increment('point');
98
+ const newVersion = [version[0], version[1], version[2]+1].join('.');
99
+ expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to create version ${newVersion}?`);
100
+ });
101
+
102
+ it ('does not continue if the user responds "n"', async () => {
103
+ utils.confirmContinue.mockImplementation(() => false);
104
+ await increment('point');
105
+
106
+ expect(Git.createBranch).not.toHaveBeenCalled();
107
+ expect(fs.readFileSync).not.toHaveBeenCalled();
108
+ expect(fs.writeFileSync).not.toHaveBeenCalled();
109
+ expect(Git.push).not.toHaveBeenCalled();
110
+ });
111
+ });
112
+
113
+
114
+ describe('createBranch', () => {
115
+ it ('creates a release branch for the new version', async () => {
116
+ await increment('point');
117
+ const newVersion = [version[0], version[1], version[2]+1].join('.');
118
+ expect(Git.createBranch).toHaveBeenCalledWith(`release/${newVersion}`);
119
+ });
120
+ });
121
+
122
+
123
+ describe('updatePackageVersion', () => {
124
+ it ('writes the new package version to package.json', async () => {
125
+ const newVersion = [version[0], version[1], version[2]+1].join('.');
126
+ const newConfig = structuredClone(config);
127
+ newConfig.version = newVersion;
128
+
129
+ await increment('point');
130
+
131
+ expect(fs.writeFileSync).toHaveBeenCalledWith(packageJsonPath, JSON.stringify(newConfig, null, 4));
132
+ });
133
+
134
+ it ('adds package.json to be committed', async () => {
135
+ await increment('point');
136
+ expect(Git.add).toHaveBeenCalledWith(packageJsonPath);
137
+ });
138
+
139
+ it ('adds a descriptive commit message', async () => {
140
+ const newVersion = [version[0], version[1], version[2]+1].join('.');
141
+ await increment('point');
142
+ expect(Git.commit).toHaveBeenCalledWith(`build: incrementing build to ${newVersion}`);
143
+ });
144
+ });
145
+ });
@@ -0,0 +1,72 @@
1
+ const { release } = require('../lib/release/release');
2
+ const Git = require('../lib/release/git');
3
+ const TestHelpers = require('./helpers');
4
+ const utils = require('../lib/release/utils');
5
+
6
+
7
+ jest.mock('../lib/release/git', () => {
8
+ return { pull: jest.fn(), forceRemotePush: jest.fn(), branches: jest.fn() };
9
+ });
10
+ jest.mock('../lib/release/utils', () => {
11
+ return { getCurrentVersion: jest.fn(), confirmContinue: jest.fn() };
12
+ });
13
+
14
+
15
+ let env;
16
+ let version;
17
+ beforeEach(() => {
18
+ jest.resetAllMocks();
19
+ env = TestHelpers.Faker.Text.randomString();
20
+ version = [TestHelpers.Faker.Math.randomNumber(),
21
+ TestHelpers.Faker.Math.randomNumber(),
22
+ TestHelpers.Faker.Math.randomNumber()];
23
+ utils.getCurrentVersion.mockImplementation(() => version);
24
+ utils.confirmContinue.mockImplementation(() => true);
25
+ });
26
+
27
+
28
+ describe('release', () => {
29
+ it ('runs steps in the proper order', async () => {
30
+ await release(env);
31
+
32
+ expect(Git.pull).toHaveBeenCalledTimes(1)
33
+ expect(utils.confirmContinue).toHaveBeenCalledTimes(1)
34
+ expect(Git.pull).toHaveBeenCalledBefore(utils.confirmContinue)
35
+
36
+ expect(Git.forceRemotePush).toHaveBeenCalledTimes(1)
37
+ expect(utils.confirmContinue).toHaveBeenCalledBefore(Git.forceRemotePush)
38
+ });
39
+
40
+ it ('defaults to the current version', async () => {
41
+ await release(env);
42
+ const versionStr = version.join('.');
43
+ expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to release version ${versionStr} to ${env}?`);
44
+ expect(Git.forceRemotePush).toHaveBeenCalledWith(`release/${versionStr}`, `release/${env}`);
45
+ });
46
+
47
+ it ('accepts an alternate version', async () => {
48
+ const altVersion = [TestHelpers.Faker.Math.randomNumber(),
49
+ TestHelpers.Faker.Math.randomNumber(),
50
+ TestHelpers.Faker.Math.randomNumber()].join('.');
51
+ Git.branches.mockImplementation(() => [`origin/release/${version.join('.')}`, `origin/release/${altVersion}`, TestHelpers.Faker.Text.randomString()]);
52
+ await release(env, altVersion);
53
+ expect(utils.confirmContinue).toHaveBeenCalledWith(`Do you want to release version ${altVersion} to ${env}?`);
54
+ expect(Git.forceRemotePush).toHaveBeenCalledWith(`release/${altVersion}`, `release/${env}`);
55
+ });
56
+
57
+ it ('does not continue if the user responds "n"', async () => {
58
+ utils.confirmContinue.mockImplementation(() => false);
59
+ await release(env);
60
+
61
+ expect(Git.forceRemotePush).not.toHaveBeenCalled();
62
+ });
63
+
64
+ it ('throws an error if the version does not exist', async () => {
65
+ const altVersion = [TestHelpers.Faker.Math.randomNumber(),
66
+ TestHelpers.Faker.Math.randomNumber(),
67
+ TestHelpers.Faker.Math.randomNumber()].join('.');
68
+ Git.branches.mockImplementation(() => [`origin/release/${version.join('.')}`, TestHelpers.Faker.Text.randomString()]);
69
+ expect(async () => { await release(env, altVersion)}).rejects.toThrow();
70
+ });
71
+ });
72
+
@@ -0,0 +1,148 @@
1
+ const Git = require('../lib/release/git');
2
+ const TestHelpers = require('./helpers');
3
+ const utils = require('../lib/release/utils');
4
+ const readline = require('node:readline/promises');
5
+
6
+
7
+ jest.mock('node:readline/promises', () => {
8
+ return { createInterface: jest.fn(), question: jest.fn(), close: jest.fn() };
9
+ });
10
+
11
+ jest.mock('../lib/release/git', () => {
12
+ return {branches: jest.fn()};
13
+ });
14
+
15
+
16
+ beforeEach(() => {
17
+ jest.resetAllMocks();
18
+ readline.createInterface.mockImplementation(() => {
19
+ return {question: readline.question, close: readline.close};
20
+ });
21
+ });
22
+
23
+
24
+ describe('getCurrentVersion', () => {
25
+ const major1 = TestHelpers.Faker.Math.randomNumber();
26
+ const minor1 = TestHelpers.Faker.Math.randomNumber();
27
+ const point1 = TestHelpers.Faker.Math.randomNumber();
28
+ const diff1 = TestHelpers.Faker.Math.randomNumber();
29
+ const diff2 = TestHelpers.Faker.Math.randomNumber();
30
+
31
+ const b1 = `origin/release/${major1}/${minor1}/${point1}`;
32
+
33
+ it ('sorts branches by primary version first', () => {
34
+ const b2 = `origin/release/${major1 + diff1 }.${minor1 + diff2}.${point1 + diff1}`;
35
+ const b3 = `origin/release/${major1 + diff1 + diff2}.${minor1 }.${point1}`;
36
+ Git.branches.mockImplementation(() => [b1, b3, b2]);
37
+ expect(utils.getCurrentVersion()).toEqual([major1+diff1+diff2, minor1, point1]);
38
+ });
39
+
40
+ it ('sorts branches by minor version second', () => {
41
+ const b2 = `origin/release/${major1 + diff1}.${minor1 + diff1 + diff2}.${point1}`;
42
+ const b3 = `origin/release/${major1 + diff1}.${minor1 + diff1 }.${point1 + diff1}`;
43
+ Git.branches.mockImplementation(() => [b1, b3, b2]);
44
+ expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1+diff1+diff2, point1]);
45
+ });
46
+
47
+ it ('sorts branches by point version last', () => {
48
+ const b2 = `origin/release/${major1 + diff1}.${minor1 + diff1}.${point1 + diff1}`;
49
+ const b3 = `origin/release/${major1 + diff1}.${minor1 + diff1}.${point1 + diff1 + diff2}`;
50
+ Git.branches.mockImplementation(() => [b1, b3, b2]);
51
+ expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1+diff1, point1+diff1+diff2]);
52
+ });
53
+
54
+ it ('ignores branches that don not fit the pattern', () => {
55
+ const b2 = `origin/release/${major1 + diff1}.${minor1}.${point1}`;
56
+ const b3 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
57
+ const b4 = TestHelpers.Faker.Text.randomString();
58
+ Git.branches.mockImplementation(() => [b1, b3, b2, b4]);
59
+ expect(utils.getCurrentVersion()).toEqual([major1+diff1, minor1, point1]);
60
+ });
61
+
62
+ it ('defaults to 1.0.0', () => {
63
+ const b2 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
64
+ const b3 = `origin/release/${TestHelpers.Faker.Text.randomString()}`;
65
+ const b4 = TestHelpers.Faker.Text.randomString();
66
+ Git.branches.mockImplementation(() => [b3, b2, b4]);
67
+ expect(utils.getCurrentVersion()).toEqual([1, 0, 0]);
68
+ });
69
+ });
70
+
71
+
72
+ describe('getInput', () => {
73
+ let question;
74
+ let numOptions;
75
+ let options;
76
+ let answer;
77
+ beforeEach(() => {
78
+ question = TestHelpers.Faker.Text.randomSentence();
79
+ numOptions = TestHelpers.Faker.Math.randomNumber(10);
80
+ options = [...Array(numOptions)].map(() => TestHelpers.Faker.Text.randomString());
81
+ answer = options[Math.floor(Math.random() * numOptions)];
82
+ readline.question.mockImplementation(() => answer);
83
+ });
84
+
85
+ it ('asks the question and appends the options', async () => {
86
+ await utils.getInput(question, options)
87
+ expect(readline.question).toHaveBeenCalledWith(`${question} [${options.join(',')}] `);
88
+ });
89
+
90
+ it ('asks the question once if it gets a valid answer', async () => {
91
+ const theAnswer = await utils.getInput(question, options)
92
+ expect(readline.question).toHaveBeenCalledTimes(1);
93
+ expect(theAnswer).toEqual(answer);
94
+ });
95
+
96
+ it ('closes the input after getting an answer', async () => {
97
+ await utils.getInput(question, options)
98
+ expect(readline.question).toHaveBeenCalledTimes(1);
99
+ expect(readline.close).toHaveBeenCalledTimes(1);
100
+ expect(readline.question).toHaveBeenCalledBefore(readline.close);
101
+ });
102
+
103
+ it ('repeats the question until it gets a valid answer', async () => {
104
+ const numAttempts = TestHelpers.Faker.Math.randomNumber(10);
105
+ let attemptNumber = 1;
106
+
107
+ readline.question.mockImplementation(() => {
108
+ if (attemptNumber == numAttempts) return answer;
109
+ attemptNumber = attemptNumber + 1;
110
+ return TestHelpers.Faker.Text.randomString();
111
+ });
112
+
113
+ const theAnswer = await utils.getInput(question, options)
114
+ expect(readline.question).toHaveBeenCalledTimes(numAttempts);
115
+ expect(readline.close).toHaveBeenCalledTimes(numAttempts);
116
+ expect(theAnswer).toEqual(answer);
117
+
118
+ for(let i=1; i <= numAttempts; i++){
119
+ expect(readline.question).toHaveBeenNthCalledWith(i, `${question} [${options.join(',')}] `);
120
+ }
121
+ });
122
+ });
123
+
124
+
125
+ describe('confirmContinue', () => {
126
+ let question;
127
+ beforeEach(() => {
128
+ question = TestHelpers.Faker.Text.randomSentence();
129
+ });
130
+
131
+ it ('asks the question with "y" or "n" as options', async () => {
132
+ readline.question.mockImplementation(() => 'y');
133
+ await utils.confirmContinue(question);
134
+ expect(readline.question).toHaveBeenCalledWith(`${question} [y,n] `);
135
+ });
136
+
137
+ it ('returns true if the user answers "y"', async () => {
138
+ readline.question.mockImplementation(() => 'y');
139
+ const confirmed = await utils.confirmContinue(question);
140
+ expect(confirmed).toBeTruthy();
141
+ });
142
+
143
+ it ('returns false if the user answers "n"', async () => {
144
+ readline.question.mockImplementation(() => 'n');
145
+ const confirmed = await utils.confirmContinue(question);
146
+ expect(confirmed).toBeFalsy();
147
+ });
148
+ });