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