|
6 | 6 | import { default as assert } from 'assert'; |
7 | 7 | import { replaceImages } from '../../github/prComment'; |
8 | 8 |
|
| 9 | +describe('commit SHA replacement', function () { |
| 10 | + it('should match 7-character commit SHAs', function () { |
| 11 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 12 | + const text = 'Fixed in commit 5cf56bc and also in abc1234'; |
| 13 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 14 | + assert.strictEqual(matches.length, 2); |
| 15 | + assert.strictEqual(matches[0][1], '5cf56bc'); |
| 16 | + assert.strictEqual(matches[1][1], 'abc1234'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should match 40-character commit SHAs', function () { |
| 20 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 21 | + const text = 'Fixed in commit 5cf56bc1234567890abcdef1234567890abcdef0'; |
| 22 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 23 | + assert.strictEqual(matches.length, 1); |
| 24 | + assert.strictEqual(matches[0][0], '5cf56bc1234567890abcdef1234567890abcdef0'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should not match SHAs in URLs', function () { |
| 28 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 29 | + const text = 'https://github.com/owner/repo/commit/5cf56bc'; |
| 30 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 31 | + assert.strictEqual(matches.length, 0); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should not match SHAs in code blocks', function () { |
| 35 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 36 | + const text = 'Fixed in commit 5cf56bc but not in `abc1234`'; |
| 37 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 38 | + // The regex will match both, but the replacement logic checks backtick count |
| 39 | + assert.strictEqual(matches.length, 2); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should not match non-hex strings', function () { |
| 43 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 44 | + const text = 'Not a SHA: 1234xyz or ABCDEFG'; |
| 45 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 46 | + assert.strictEqual(matches.length, 0); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should not match SHAs with alphanumeric prefix', function () { |
| 50 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 51 | + const text = 'prefix5cf56bc is not a SHA'; |
| 52 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 53 | + assert.strictEqual(matches.length, 0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not match SHAs with alphanumeric suffix', function () { |
| 57 | + const commitShaRegex = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g; |
| 58 | + const text = '5cf56bcsuffix is not a SHA'; |
| 59 | + const matches = Array.from(text.matchAll(commitShaRegex)); |
| 60 | + assert.strictEqual(matches.length, 0); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
9 | 64 | describe('replace images', function () { |
10 | 65 | it('github.com', function () { |
11 | 66 | const markdownBody = `Test image |
|
0 commit comments