-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathupdate-upgrade-notice.test.js
More file actions
executable file
·435 lines (334 loc) · 9.71 KB
/
update-upgrade-notice.test.js
File metadata and controls
executable file
·435 lines (334 loc) · 9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env node
/**
* Tests for update-upgrade-notice.js
*
* Run with: node scripts/update-upgrade-notice.test.js
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const assert = require('assert');
const SCRIPT_PATH = path.join(__dirname, 'update-upgrade-notice.js');
const TEST_DIR = path.join(__dirname, '..', '.test-upgrade-notice');
/**
* Setup test directory with test files
*/
function setup() {
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true });
}
fs.mkdirSync(TEST_DIR, { recursive: true });
}
/**
* Cleanup test directory
*/
function cleanup() {
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true });
}
}
/**
* Create test CHANGELOG.md
*/
function createChangelog(content) {
fs.writeFileSync(path.join(TEST_DIR, 'CHANGELOG.md'), content);
}
/**
* Create test readme.txt
*/
function createReadme(content) {
fs.writeFileSync(path.join(TEST_DIR, 'readme.txt'), content);
}
/**
* Read readme.txt content
*/
function readReadme() {
return fs.readFileSync(path.join(TEST_DIR, 'readme.txt'), 'utf8');
}
/**
* Run the script with given arguments
*/
function runScript(version) {
const cmd = `node "${SCRIPT_PATH}" --version=${version} --plugin-dir=.test-upgrade-notice`;
try {
const output = execSync(cmd, {
cwd: path.join(__dirname, '..'),
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'pipe'],
});
return { success: true, output };
} catch (error) {
return { success: false, output: error.stdout || error.message };
}
}
/**
* Test helper to run a test case
*/
function test(name, fn) {
try {
setup();
fn();
console.log(`✅ ${name}`);
return true;
} catch (error) {
console.log(`❌ ${name}`);
console.log(` Error: ${error.message}`);
return false;
} finally {
cleanup();
}
}
// ===========================================
// TEST CASES
// ===========================================
const tests = [
// Test 1: Basic breaking changes extraction
() =>
test('extracts breaking changes from standard release-please format', () => {
createChangelog(`# Changelog
## [3.0.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v3.0.0) (2026-01-20)
### ⚠ BREAKING CHANGES
* Remove deprecated query ([#123](https://github.com/wp-graphql/wp-graphql/pull/123))
* Change default behavior ([#124](https://github.com/wp-graphql/wp-graphql/pull/124))
### Features
* Add new feature ([#125](https://github.com/wp-graphql/wp-graphql/pull/125))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('Found 2 breaking change(s)'),
'Should find 2 breaking changes'
);
const readme = readReadme();
assert(
readme.includes('== Upgrade Notice =='),
'Should add Upgrade Notice section'
);
assert(
readme.includes('= 3.0.0 ='),
'Should include version header'
);
assert(
readme.includes('Remove deprecated query'),
'Should include first breaking change'
);
assert(
readme.includes('Change default behavior'),
'Should include second breaking change'
);
assert(readme.includes('#123'), 'Should preserve PR link');
}),
// Test 2: No breaking changes
() =>
test('handles version with no breaking changes', () => {
createChangelog(`# Changelog
## [2.1.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v2.1.0) (2026-01-20)
### Features
* Add new feature ([#125](https://github.com/wp-graphql/wp-graphql/pull/125))
### Bug Fixes
* Fix bug ([#126](https://github.com/wp-graphql/wp-graphql/pull/126))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('2.1.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('No breaking changes'),
'Should report no breaking changes'
);
const readme = readReadme();
assert(
!readme.includes('== Upgrade Notice =='),
'Should NOT add Upgrade Notice section'
);
}),
// Test 3: Version not found in changelog
() =>
test('handles version not found in changelog', () => {
createChangelog(`# Changelog
## [2.0.0](https://github.com/wp-graphql/wp-graphql/compare/v1.0.0...v2.0.0) (2026-01-20)
### Features
* Add new feature
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('No changelog entry found'),
'Should report version not found'
);
}),
// Test 4: Update existing upgrade notice
() =>
test('updates existing upgrade notice for same version', () => {
createChangelog(`# Changelog
## [3.0.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v3.0.0) (2026-01-20)
### ⚠ BREAKING CHANGES
* New breaking change ([#127](https://github.com/wp-graphql/wp-graphql/pull/127))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Upgrade Notice ==
= 3.0.0 =
**⚠️ BREAKING CHANGES**: Old notice content.
* Old breaking change
Please review these changes before upgrading.
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('Updated existing upgrade notice'),
'Should update existing notice'
);
const readme = readReadme();
assert(
readme.includes('New breaking change'),
'Should contain new breaking change'
);
// Count occurrences of "= 3.0.0 =" to ensure no duplicates
const matches = readme.match(/= 3\.0\.0 =/g);
assert(
matches && matches.length === 1,
'Should have exactly one version header'
);
}),
// Test 5: Alternative breaking changes header format
() =>
test('handles alternative BREAKING CHANGES header (without emoji)', () => {
createChangelog(`# Changelog
## [3.0.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v3.0.0) (2026-01-20)
### BREAKING CHANGES
* Remove old API ([#128](https://github.com/wp-graphql/wp-graphql/pull/128))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('Found 1 breaking change(s)'),
'Should find breaking change'
);
}),
// Test 6: Version format without brackets
() =>
test('handles version without brackets in changelog', () => {
createChangelog(`# Changelog
## 3.0.0 (2026-01-20)
### ⚠ BREAKING CHANGES
* Breaking change without brackets ([#129](https://github.com/wp-graphql/wp-graphql/pull/129))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
assert(
result.output.includes('Found 1 breaking change(s)'),
'Should find breaking change'
);
}),
// Test 7: Multiple versions in changelog
() =>
test('extracts breaking changes for correct version only', () => {
createChangelog(`# Changelog
## [4.0.0](https://github.com/wp-graphql/wp-graphql/compare/v3.0.0...v4.0.0) (2026-02-01)
### ⚠ BREAKING CHANGES
* Version 4 breaking change ([#200](https://github.com/wp-graphql/wp-graphql/pull/200))
## [3.0.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v3.0.0) (2026-01-20)
### ⚠ BREAKING CHANGES
* Version 3 breaking change ([#100](https://github.com/wp-graphql/wp-graphql/pull/100))
## [2.0.0](https://github.com/wp-graphql/wp-graphql/compare/v1.0.0...v2.0.0) (2026-01-01)
### Features
* Some feature
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
const readme = readReadme();
assert(
readme.includes('Version 3 breaking change'),
'Should include v3 breaking change'
);
assert(
!readme.includes('Version 4 breaking change'),
'Should NOT include v4 breaking change'
);
assert(readme.includes('#100'), 'Should include correct PR link');
assert(!readme.includes('#200'), 'Should NOT include v4 PR link');
}),
// Test 8: Scoped breaking changes (e.g., **resolver:**)
() =>
test('preserves scope prefix in breaking changes', () => {
createChangelog(`# Changelog
## [3.0.0](https://github.com/wp-graphql/wp-graphql/compare/v2.0.0...v3.0.0) (2026-01-20)
### ⚠ BREAKING CHANGES
* **resolver:** Change resolver behavior ([#130](https://github.com/wp-graphql/wp-graphql/pull/130))
* **types:** Remove deprecated type ([#131](https://github.com/wp-graphql/wp-graphql/pull/131))
`);
createReadme(`=== Test Plugin ===
Stable tag: 2.0.0
== Changelog ==
= 2.0.0 =
* Initial release
`);
const result = runScript('3.0.0');
assert(result.success, 'Script should succeed');
const readme = readReadme();
assert(
readme.includes('**resolver:**'),
'Should preserve resolver scope'
);
assert(
readme.includes('**types:**'),
'Should preserve types scope'
);
}),
];
// ===========================================
// RUN TESTS
// ===========================================
console.log('\n🧪 Running update-upgrade-notice.js tests...\n');
let passed = 0;
let failed = 0;
for (const testFn of tests) {
if (testFn()) {
passed++;
} else {
failed++;
}
}
console.log(`\n${'─'.repeat(50)}`);
console.log(`Results: ${passed} passed, ${failed} failed`);
console.log(`${'─'.repeat(50)}\n`);
// Exit with error code if any tests failed
process.exit(failed > 0 ? 1 : 0);