/
index.spec.ts
143 lines (127 loc) · 4.38 KB
/
index.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
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
135
136
137
138
139
140
141
142
143
import { PLATFORM_NOT_FOUND } from '../constants/error-messages';
import { PLATFORM_TYPE_BITBUCKET } from '../constants/platforms';
import { loadModules } from '../util/modules';
import * as azure from './azure';
import * as bitbucket from './bitbucket';
import * as bitbucketServer from './bitbucket-server';
import * as gitea from './gitea';
import * as github from './github';
import * as gitlab from './gitlab';
import * as platform from '.';
import { Platform } from '.';
jest.unmock('.');
describe('platform', () => {
beforeEach(() => {
jest.resetModules();
});
it('validates', () => {
function validate(module: Platform, name: string): boolean {
// TODO: test required api
if (!module.initPlatform) {
throw Error(`Missing api on ${name}`);
}
return true;
}
const platforms = platform.getPlatforms();
const loadedMgr = loadModules(
__dirname,
null,
(m) => !['utils', 'git'].includes(m)
);
expect(Array.from(platforms.keys())).toEqual(Object.keys(loadedMgr));
for (const name of platforms.keys()) {
const value = platforms.get(name);
expect(validate(value, name)).toBe(true);
}
});
it('throws if no platform', () => {
expect(() => platform.platform.initPlatform({})).toThrow(
PLATFORM_NOT_FOUND
);
});
it('throws if wrong platform', async () => {
const config = { platform: 'wrong', username: 'abc', password: '123' };
await expect(platform.initPlatform(config)).rejects.toThrow();
});
it('initializes', async () => {
const config = {
platform: PLATFORM_TYPE_BITBUCKET,
gitAuthor: 'user@domain.com',
username: 'abc',
password: '123',
};
expect(await platform.initPlatform(config)).toMatchSnapshot();
});
it('initializes no author', async () => {
const config = {
platform: PLATFORM_TYPE_BITBUCKET,
username: 'abc',
password: '123',
};
expect(await platform.initPlatform(config)).toMatchSnapshot();
});
it('has a list of supported methods for github', () => {
const githubMethods = Object.keys(github).sort();
expect(githubMethods).toMatchSnapshot();
});
it('has a list of supported methods for gitlab', () => {
const gitlabMethods = Object.keys(gitlab).sort();
expect(gitlabMethods).toMatchSnapshot();
});
it('has a list of supported methods for gitea', () => {
const giteaMethods = Object.keys(gitea).sort();
expect(giteaMethods).toMatchSnapshot();
});
it('has a list of supported methods for azure', () => {
const azureMethods = Object.keys(azure).sort();
expect(azureMethods).toMatchSnapshot();
});
it('has same API for github and gitlab', () => {
const githubMethods = Object.keys(github).sort();
const gitlabMethods = Object.keys(gitlab).sort();
expect(githubMethods).toMatchObject(gitlabMethods);
});
it('has same API for github and gitea', () => {
const githubMethods = Object.keys(github).sort();
const giteaMethods = Object.keys(gitea).sort();
expect(githubMethods).toMatchObject(giteaMethods);
});
it('has same API for github and azure', () => {
const githubMethods = Object.keys(github).sort();
const azureMethods = Object.keys(azure).sort();
expect(githubMethods).toMatchObject(azureMethods);
});
it('has same API for github and Bitbucket', () => {
const githubMethods = Object.keys(github).sort();
const bitbucketMethods = Object.keys(bitbucket).sort();
expect(bitbucketMethods).toMatchObject(githubMethods);
});
it('has same API for github and Bitbucket Server', () => {
const githubMethods = Object.keys(github).sort();
const bitbucketMethods = Object.keys(bitbucketServer).sort();
expect(bitbucketMethods).toMatchObject(githubMethods);
});
it('returns null if empty email given', () => {
expect(platform.parseGitAuthor(undefined)).toBeNull();
});
it('parses bot email', () => {
expect(
platform.parseGitAuthor('some[bot]@users.noreply.github.com')
).toMatchSnapshot();
});
it('parses bot name and email', () => {
expect(
platform.parseGitAuthor(
'"some[bot]" <some[bot]@users.noreply.github.com>'
)
).toMatchSnapshot();
});
it('escapes names', () => {
expect(
platform.parseGitAuthor('name [what] <name@what.com>').name
).toMatchSnapshot();
});
it('gives up', () => {
expect(platform.parseGitAuthor('a.b.c')).toBeNull();
});
});