-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathgithubRepository.test.ts
More file actions
134 lines (118 loc) · 5.67 KB
/
githubRepository.test.ts
File metadata and controls
134 lines (118 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { default as assert } from 'assert';
import { SinonSandbox, createSandbox } from 'sinon';
import { CredentialStore } from '../../github/credentials';
import { MockCommandRegistry } from '../mocks/mockCommandRegistry';
import { MockTelemetry } from '../mocks/mockTelemetry';
import { GitHubRemote, Remote } from '../../common/remote';
import { Protocol } from '../../common/protocol';
import { GitHubRepository } from '../../github/githubRepository';
import { Uri } from 'vscode';
import { MockExtensionContext } from '../mocks/mockExtensionContext';
import { GitHubManager } from '../../authentication/githubServer';
import { GitHubServerType } from '../../common/authentication';
import { CheckState, PullRequestCheckStatus } from '../../github/interface';
describe('GitHubRepository', function () {
let sinon: SinonSandbox;
let credentialStore: CredentialStore;
let telemetry: MockTelemetry;
let context: MockExtensionContext;
beforeEach(function () {
sinon = createSandbox();
MockCommandRegistry.install(sinon);
telemetry = new MockTelemetry();
context = new MockExtensionContext();
credentialStore = new CredentialStore(telemetry, context);
});
afterEach(function () {
sinon.restore();
});
describe('isGitHubDotCom', function () {
it('detects when the remote is pointing to github.com', function () {
const url = 'https://github.com/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
const dotcomRepository = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
assert(GitHubManager.isGithubDotCom(Uri.parse(remote.url).authority));
});
it('detects when the remote is pointing somewhere other than github.com', function () {
const url = 'https://github.enterprise.horse/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
const dotcomRepository = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
// assert(! dotcomRepository.isGitHubDotCom);
});
});
describe('deduplicateStatusChecks', function () {
function createStatus(overrides: Partial<PullRequestCheckStatus> & { id: string; context: string }): PullRequestCheckStatus {
return {
databaseId: undefined,
url: undefined,
avatarUrl: undefined,
state: CheckState.Success,
description: null,
targetUrl: null,
workflowName: undefined,
event: undefined,
isRequired: false,
isCheckRun: true,
...overrides,
};
}
function callDeduplicateStatusChecks(repo: GitHubRepository, statuses: PullRequestCheckStatus[]): PullRequestCheckStatus[] {
return (repo as any).deduplicateStatusChecks(statuses);
}
let repo: GitHubRepository;
beforeEach(function () {
const url = 'https://github.com/some/repo';
const remote = new GitHubRemote('origin', url, new Protocol(url), GitHubServerType.GitHubDotCom);
const rootUri = Uri.file('C:\\users\\test\\repo');
repo = new GitHubRepository(1, remote, rootUri, credentialStore, telemetry);
});
it('keeps checks with different events as separate entries', function () {
const statuses = [
createStatus({ id: '1', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux' }),
createStatus({ id: '2', context: 'Build Linux / x86-64', event: 'pull_request', workflowName: 'Build Linux' }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
it('deduplicates checks with the same name, event, and workflow', function () {
const statuses = [
createStatus({ id: '1', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux', state: CheckState.Success }),
createStatus({ id: '2', context: 'Build Linux / x86-64', event: 'push', workflowName: 'Build Linux', state: CheckState.Success }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].id, '2'); // higher ID preferred
});
it('keeps checks from different workflows as separate entries', function () {
const statuses = [
createStatus({ id: '1', context: 'build', event: 'push', workflowName: 'CI' }),
createStatus({ id: '2', context: 'build', event: 'push', workflowName: 'Nightly' }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
it('prefers pending checks over completed ones during deduplication', function () {
const statuses = [
createStatus({ id: '1', context: 'test', event: 'push', workflowName: 'CI', state: CheckState.Success }),
createStatus({ id: '2', context: 'test', event: 'push', workflowName: 'CI', state: CheckState.Pending }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 1);
assert.strictEqual(result[0].state, CheckState.Pending);
});
it('handles status contexts without event or workflowName', function () {
const statuses = [
createStatus({ id: '1', context: 'ci/jenkins', isCheckRun: false }),
createStatus({ id: '2', context: 'ci/travis', isCheckRun: false }),
];
const result = callDeduplicateStatusChecks(repo, statuses);
assert.strictEqual(result.length, 2);
});
});
});