-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
github-client.spec.ts
93 lines (70 loc) · 2.23 KB
/
github-client.spec.ts
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
'use strict';
jest.mock('@octokit/rest');
jest.mock('../../child-process');
import { Octokit } from '@octokit/rest';
const childProcess = require('../../child-process');
import { createGitHubClient, parseGitRepo } from '../index';
childProcess.execSync.mockReturnValue('5.6.0');
describe('createGitHubClient', () => {
const oldEnv = Object.assign({}, process.env);
delete process.env.GH_TOKEN;
afterEach(() => {
process.env = oldEnv;
});
it('errors if no GH_TOKEN env var', () => {
expect(() => {
createGitHubClient();
}).toThrow('A GH_TOKEN environment variable is required.');
});
it('doesnt error if GH_TOKEN env var is set', () => {
process.env.GH_TOKEN = 'TOKEN';
expect(() => {
createGitHubClient();
}).not.toThrow();
});
it('initializes GHE plugin when GHE_VERSION env var is set', () => {
process.env.GH_TOKEN = 'TOKEN';
process.env.GHE_VERSION = '2.18';
createGitHubClient();
expect(Octokit.plugin).toHaveBeenCalledWith(expect.anything());
});
it('sets octokit `baseUrl` when GHE_API_URL is set', () => {
process.env.GH_TOKEN = 'TOKEN';
process.env.GHE_API_URL = 'http://some/host';
createGitHubClient();
expect(Octokit).toHaveBeenCalledWith({
auth: 'token TOKEN',
baseUrl: 'http://some/host',
});
});
});
describe('parseGitRepo', () => {
it('returns a parsed URL', () => {
childProcess.execSync.mockReturnValue('git@github.com:org/lerna.git');
const repo = parseGitRepo();
expect(childProcess.execSync).toHaveBeenCalledWith(
'git',
['config', '--get', 'remote.origin.url'],
undefined
);
expect(repo).toEqual(
expect.objectContaining({
name: 'lerna',
owner: 'org',
})
);
});
it('can change the origin', () => {
childProcess.execSync.mockReturnValue('git@github.com:org/lerna.git');
parseGitRepo('upstream');
expect(childProcess.execSync).toHaveBeenCalledWith(
'git',
['config', '--get', 'remote.upstream.url'],
undefined
);
});
it('throws an error if no URL returned', () => {
childProcess.execSync.mockReturnValue('');
expect(() => parseGitRepo()).toThrow('Git remote URL could not be found using "origin".');
});
});