Skip to content

Commit

Permalink
Fixes unit tests by uncoupling dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeibbb committed Nov 7, 2024
1 parent 870c8ba commit 20ee4a0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import { suite, test } from 'mocha';
import { getPullRequestIdentityValuesFromSearch } from '../pullRequest';
import { getPullRequestIdentityValuesFromSearch } from '../pullRequest.utils';

suite('Test GitHub PR URL parsing to identity: getPullRequestIdentityValuesFromSearch()', () => {
function t(message: string, query: string, prNumber: string | undefined, ownerAndRepo?: string) {
Expand Down
40 changes: 1 addition & 39 deletions src/git/models/pullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { formatDate, fromNow } from '../../system/date';
import { memoize } from '../../system/decorators/memoize';
import type { LeftRightCommitCountResult } from '../gitProvider';
import type { IssueOrPullRequest, IssueRepository, IssueOrPullRequestState as PullRequestState } from './issue';
import type { PullRequestURLIdentity } from './pullRequest.utils';
import { createRevisionRange, shortenRevision } from './reference';
import type { ProviderReference } from './remoteProvider';
import type { Repository } from './repository';
Expand Down Expand Up @@ -417,45 +418,6 @@ export async function getOpenedPullRequestRepo(
return repo;
}

export type PullRequestURLIdentity = {
ownerAndRepo?: string;
prNumber?: string;
};

export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
let ownerAndRepo: string | undefined = undefined;
let prNumber: string | undefined = undefined;

let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
if (match != null) {
ownerAndRepo = match[1];
prNumber = match[2];
}

if (prNumber == null) {
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
if (match != null) {
prNumber = match[1];
}
}

if (prNumber == null) {
match = search.match(/(?:\/)(\d+)/); // any number starting with "/"
if (match != null) {
prNumber = match[1];
}
}

if (prNumber == null) {
match = search.match(/^#?(\d+)$/); // just a number or with a leading "#"
if (match != null) {
prNumber = match[1];
}
}

return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
}

export function doesPullRequestSatisfyRepositoryURLIdentity(
pr: EnrichablePullRequest | undefined,
{ ownerAndRepo, prNumber }: PullRequestURLIdentity,
Expand Down
42 changes: 42 additions & 0 deletions src/git/models/pullRequest.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// pullRequest.ts pulls many dependencies through Container and some of them break the unit tests.
// To avoid this file has been created that can collect more simple functions which
// don't require Container and can be tested.

export type PullRequestURLIdentity = {
ownerAndRepo?: string;
prNumber?: string;
};

export function getPullRequestIdentityValuesFromSearch(search: string): PullRequestURLIdentity {
let ownerAndRepo: string | undefined = undefined;
let prNumber: string | undefined = undefined;

let match = search.match(/([^/]+\/[^/]+)\/pull\/(\d+)/); // with org and rep name
if (match != null) {
ownerAndRepo = match[1];
prNumber = match[2];
}

if (prNumber == null) {
match = search.match(/(?:\/|^)pull\/(\d+)/); // without repo name
if (match != null) {
prNumber = match[1];
}
}

if (prNumber == null) {
match = search.match(/(?:\/)(\d+)/); // any number starting with "/"
if (match != null) {
prNumber = match[1];
}
}

if (prNumber == null) {
match = search.match(/^#?(\d+)$/); // just a number or with a leading "#"
if (match != null) {
prNumber = match[1];
}
}

return { ownerAndRepo: ownerAndRepo, prNumber: prNumber };
}
6 changes: 2 additions & 4 deletions src/plus/launchpad/launchpad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ import { HostingIntegrationId, SelfHostedIntegrationId } from '../../constants.i
import type { LaunchpadTelemetryContext, Source, Sources, TelemetryEvents } from '../../constants.telemetry';
import type { Container } from '../../container';
import { PlusFeatures } from '../../features';
import {
doesPullRequestSatisfyRepositoryURLIdentity,
getPullRequestIdentityValuesFromSearch,
} from '../../git/models/pullRequest';
import { doesPullRequestSatisfyRepositoryURLIdentity } from '../../git/models/pullRequest';
import { getPullRequestIdentityValuesFromSearch } from '../../git/models/pullRequest.utils';
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
import { createQuickPickItemOfT, createQuickPickSeparator } from '../../quickpicks/items/common';
import type { DirectiveQuickPickItem } from '../../quickpicks/items/directive';
Expand Down
2 changes: 1 addition & 1 deletion src/plus/launchpad/launchpadProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import type { PullRequest, SearchedPullRequest } from '../../git/models/pullRequ
import {
getComparisonRefsForPullRequest,
getOrOpenPullRequestRepository,
getPullRequestIdentityValuesFromSearch,
getRepositoryIdentityForPullRequest,
} from '../../git/models/pullRequest';
import { getPullRequestIdentityValuesFromSearch } from '../../git/models/pullRequest.utils';
import type { GitRemote } from '../../git/models/remote';
import type { Repository } from '../../git/models/repository';
import type { CodeSuggestionCounts, Draft } from '../../gk/models/drafts';
Expand Down

0 comments on commit 20ee4a0

Please sign in to comment.