|
| 1 | +import * as assert from 'assert'; |
| 2 | +import { suite, test } from 'mocha'; |
| 3 | +import { getPullRequestIdentityValuesFromSearch } from '../pullRequest'; |
| 4 | + |
| 5 | +suite('Test GitHub PR URL parsing to identity: getPullRequestIdentityValuesFromSearch()', () => { |
| 6 | + function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) { |
| 7 | + assert.deepStrictEqual( |
| 8 | + getPullRequestIdentityValuesFromSearch(query), |
| 9 | + { |
| 10 | + ownerAndRepo: ownerAndRepo, |
| 11 | + prNumber: prNumber, |
| 12 | + }, |
| 13 | + `${message} (${JSON.stringify(query)})`, |
| 14 | + ); |
| 15 | + } |
| 16 | + |
| 17 | + test('full URL or without protocol but with domain, should parse to ownerAndRepo and prNumber', () => { |
| 18 | + t('full URL', 'https://github.com/eamodio/vscode-gitlens/pull/1', '1', 'eamodio/vscode-gitlens'); |
| 19 | + t( |
| 20 | + 'with suffix', |
| 21 | + 'https://github.com/eamodio/vscode-gitlens/pull/1/files?diff=unified#hello', |
| 22 | + '1', |
| 23 | + 'eamodio/vscode-gitlens', |
| 24 | + ); |
| 25 | + t( |
| 26 | + 'with query', |
| 27 | + 'https://github.com/eamodio/vscode-gitlens/pull/1?diff=unified#hello', |
| 28 | + '1', |
| 29 | + 'eamodio/vscode-gitlens', |
| 30 | + ); |
| 31 | + |
| 32 | + t('with anchor', 'https://github.com/eamodio/vscode-gitlens/pull/1#hello', '1', 'eamodio/vscode-gitlens'); |
| 33 | + t('a weird suffix', 'https://github.com/eamodio/vscode-gitlens/pull/1-files', '1', 'eamodio/vscode-gitlens'); |
| 34 | + t('numeric repo name', 'https://github.com/sergeibbb/1/pull/16', '16', 'sergeibbb/1'); |
| 35 | + |
| 36 | + t('no protocol with leading slash', '/github.com/sergeibbb/1/pull/16?diff=unified', '16', 'sergeibbb/1'); |
| 37 | + t('no protocol without leading slash', 'github.com/sergeibbb/1/pull/16/files', '16', 'sergeibbb/1'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('no domain, should parse to ownerAndRepo and prNumber', () => { |
| 41 | + t('with leading slash', '/sergeibbb/1/pull/16#hello', '16', 'sergeibbb/1'); |
| 42 | + t('words in repo name', 'eamodio/vscode-gitlens/pull/1?diff=unified#hello', '1', 'eamodio/vscode-gitlens'); |
| 43 | + t('numeric repo name', 'sergeibbb/1/pull/16/files', '16', 'sergeibbb/1'); |
| 44 | + }); |
| 45 | + |
| 46 | + test('domain vs. no domain', () => { |
| 47 | + t( |
| 48 | + 'with anchor', |
| 49 | + 'https://github.com/eamodio/vscode-gitlens/pull/1#hello/sergeibbb/1/pull/16', |
| 50 | + '1', |
| 51 | + 'eamodio/vscode-gitlens', |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + test('has "pull/" fragment', () => { |
| 56 | + t('with leading slash', '/pull/16/files#hello', '16'); |
| 57 | + t('without leading slash', 'pull/16?diff=unified#hello', '16'); |
| 58 | + t('with numeric repo name', '1/pull/16?diff=unified#hello', '16'); |
| 59 | + t('with double slash', '1//pull/16?diff=unified#hello', '16'); |
| 60 | + }); |
| 61 | + |
| 62 | + test('has "/<num>" fragment', () => { |
| 63 | + t('with leading slash', '/16/files#hello', '16'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('is a number', () => { |
| 67 | + t('just a number', '16', '16'); |
| 68 | + t('with a hash', '#16', '16'); |
| 69 | + }); |
| 70 | + |
| 71 | + test('does not match', () => { |
| 72 | + t('without leading slash', '16?diff=unified#hello', undefined); |
| 73 | + t('with leading hash', '/#16/files#hello', undefined); |
| 74 | + t('number is a part of a word', 'hello16', undefined); |
| 75 | + t('number is a part of a word', '16hello', undefined); |
| 76 | + |
| 77 | + t('with a number', '1/16?diff=unified#hello', '16'); |
| 78 | + t('with a number and slash', '/1/16?diff=unified#hello', '1'); |
| 79 | + t('with a word', 'anything/16?diff=unified#hello', '16'); |
| 80 | + |
| 81 | + t('with a wrong character leading to pull', 'sergeibbb/1/-pull/16?diff=unified#hello', '1'); |
| 82 | + t('with a wrong character leading to pull', 'sergeibbb/1-pull/16?diff=unified#hello', '1'); |
| 83 | + }); |
| 84 | +}); |
0 commit comments