Skip to content

Commit d2ff189

Browse files
committed
Fixes unit tests by uncoupling dependencies
(#3621)
1 parent 23aa287 commit d2ff189

File tree

5 files changed

+47
-45
lines changed

5 files changed

+47
-45
lines changed

src/git/models/__tests__/pullRequest.test.ts renamed to src/git/models/__tests__/pullRequest.utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert';
22
import { suite, test } from 'mocha';
3-
import { getPullRequestIdentityValuesFromSearch } from '../pullRequest';
3+
import { getPullRequestIdentityValuesFromSearch } from '../pullRequest.utils';
44

55
suite('Test GitHub PR URL parsing to identity: getPullRequestIdentityValuesFromSearch()', () => {
66
function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) {

src/git/models/pullRequest.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { formatDate, fromNow } from '../../system/date';
77
import { memoize } from '../../system/decorators/memoize';
88
import type { LeftRightCommitCountResult } from '../gitProvider';
99
import type { IssueOrPullRequest, IssueRepository, IssueOrPullRequestState as PullRequestState } from './issue';
10+
import type { PullRequestURLIdentity } from './pullRequest.utils';
1011
import { createRevisionRange, shortenRevision } from './reference';
1112
import type { ProviderReference } from './remoteProvider';
1213
import type { Repository } from './repository';
@@ -417,45 +418,6 @@ export async function getOpenedPullRequestRepo(
417418
return repo;
418419
}
419420

420-
export type PullRequestURLIdentity = {
421-
ownerAndRepo?: string;
422-
prNumber?: string;
423-
};
424-
425-
export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
426-
let ownerAndRepo: string | undefined = undefined;
427-
let prNumber: string | undefined = undefined;
428-
429-
let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
430-
if (match != null) {
431-
ownerAndRepo = match[1];
432-
prNumber = match[2];
433-
}
434-
435-
if (prNumber == null) {
436-
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
437-
if (match != null) {
438-
prNumber = match[1];
439-
}
440-
}
441-
442-
if (prNumber == null) {
443-
match = search.match(/(?:\/)(\d+)/); // any number starting with "/"
444-
if (match != null) {
445-
prNumber = match[1];
446-
}
447-
}
448-
449-
if (prNumber == null) {
450-
match = search.match(/^#?(\d+)$/); // just a number or with a leading "#"
451-
if (match != null) {
452-
prNumber = match[1];
453-
}
454-
}
455-
456-
return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
457-
}
458-
459421
export function doesPullRequestSatisfyRepositoryURLIdentity(
460422
pr: EnrichablePullRequest | undefined,
461423
{ ownerAndRepo, prNumber }: PullRequestURLIdentity,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// pullRequest.ts pulls many dependencies through Container and some of them break the unit tests.
2+
// To avoid this file has been created that can collect more simple functions which
3+
// don't require Container and can be tested.
4+
5+
export type PullRequestURLIdentity = {
6+
ownerAndRepo?: string;
7+
prNumber?: string;
8+
};
9+
10+
export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
11+
let ownerAndRepo: string | undefined = undefined;
12+
let prNumber: string | undefined = undefined;
13+
14+
let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
15+
if (match != null) {
16+
ownerAndRepo = match[1];
17+
prNumber = match[2];
18+
}
19+
20+
if (prNumber == null) {
21+
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
22+
if (match != null) {
23+
prNumber = match[1];
24+
}
25+
}
26+
27+
if (prNumber == null) {
28+
match = search.match(/(?:\/)(\d+)/); // any number starting with "/"
29+
if (match != null) {
30+
prNumber = match[1];
31+
}
32+
}
33+
34+
if (prNumber == null) {
35+
match = search.match(/^#?(\d+)$/); // just a number or with a leading "#"
36+
if (match != null) {
37+
prNumber = match[1];
38+
}
39+
}
40+
41+
return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
42+
}

src/plus/launchpad/launchpad.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ import { HostingIntegrationId, SelfHostedIntegrationId } from '../../constants.i
4242
import type { LaunchpadTelemetryContext, Source, Sources, TelemetryEvents } from '../../constants.telemetry';
4343
import type { Container } from '../../container';
4444
import { PlusFeatures } from '../../features';
45-
import {
46-
doesPullRequestSatisfyRepositoryURLIdentity,
47-
getPullRequestIdentityValuesFromSearch,
48-
} from '../../git/models/pullRequest';
45+
import { doesPullRequestSatisfyRepositoryURLIdentity } from '../../git/models/pullRequest';
46+
import { getPullRequestIdentityValuesFromSearch } from '../../git/models/pullRequest.utils';
4947
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
5048
import { createQuickPickItemOfT, createQuickPickSeparator } from '../../quickpicks/items/common';
5149
import type { DirectiveQuickPickItem } from '../../quickpicks/items/directive';

src/plus/launchpad/launchpadProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import type { PullRequest, SearchedPullRequest } from '../../git/models/pullRequ
1919
import {
2020
getComparisonRefsForPullRequest,
2121
getOrOpenPullRequestRepository,
22-
getPullRequestIdentityValuesFromSearch,
2322
getRepositoryIdentityForPullRequest,
2423
} from '../../git/models/pullRequest';
24+
import { getPullRequestIdentityValuesFromSearch } from '../../git/models/pullRequest.utils';
2525
import type { GitRemote } from '../../git/models/remote';
2626
import type { Repository } from '../../git/models/repository';
2727
import type { CodeSuggestionCounts, Draft } from '../../gk/models/drafts';

0 commit comments

Comments
 (0)