/
util.spec.ts
229 lines (216 loc) · 7.45 KB
/
util.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { Readable } from 'stream';
import {
getBranchNameWithoutRefsheadsPrefix,
getGitStatusContextCombinedName,
getGitStatusContextFromCombinedName,
getNewBranchName,
getProjectAndRepo,
getRenovatePRFormat,
getRepoByName,
getStorageExtraCloneOpts,
max4000Chars,
streamToString,
} from './util';
describe('platform/azure/helpers', () => {
describe('getNewBranchName', () => {
it('should add refs/heads', () => {
const res = getNewBranchName('testBB');
expect(res).toBe(`refs/heads/testBB`);
});
it('should be the same', () => {
const res = getNewBranchName('refs/heads/testBB');
expect(res).toBe(`refs/heads/testBB`);
});
});
describe('getGitStatusContextCombinedName', () => {
it('should return undefined if null context passed', () => {
const contextName = getGitStatusContextCombinedName(null);
expect(contextName).toBeUndefined();
});
it('should combine valid genre and name with slash', () => {
const contextName = getGitStatusContextCombinedName({
genre: 'my-genre',
name: 'status-name',
});
expect(contextName).toMatch('my-genre/status-name');
});
it('should combine valid empty genre and name without a slash', () => {
const contextName = getGitStatusContextCombinedName({
genre: undefined,
name: 'status-name',
});
expect(contextName).toMatch('status-name');
});
});
describe('getGitStatusContextFromCombinedName', () => {
it('should return undefined if null context passed', () => {
const context = getGitStatusContextFromCombinedName(null);
expect(context).toBeUndefined();
});
it('should parse valid genre and name with slash', () => {
const context = getGitStatusContextFromCombinedName(
'my-genre/status-name'
);
expect(context).toEqual({
genre: 'my-genre',
name: 'status-name',
});
});
it('should parse valid genre and name with multiple slashes', () => {
const context = getGitStatusContextFromCombinedName(
'my-genre/sub-genre/status-name'
);
expect(context).toEqual({
genre: 'my-genre/sub-genre',
name: 'status-name',
});
});
it('should parse valid empty genre and name without a slash', () => {
const context = getGitStatusContextFromCombinedName('status-name');
expect(context).toEqual({
genre: undefined,
name: 'status-name',
});
});
});
describe('getBranchNameWithoutRefsheadsPrefix', () => {
it('should be renamed', () => {
const res = getBranchNameWithoutRefsheadsPrefix('refs/heads/testBB');
expect(res).toBe(`testBB`);
});
it('should log error and return undefined', () => {
const res = getBranchNameWithoutRefsheadsPrefix(undefined as any);
expect(res).toBeUndefined();
});
it('should return the input', () => {
const res = getBranchNameWithoutRefsheadsPrefix('testBB');
expect(res).toBe('testBB');
});
});
describe('getRenovatePRFormat', () => {
it('should be formated (closed)', () => {
const res = getRenovatePRFormat({ status: 2 } as any);
expect(res).toMatchSnapshot();
});
it('should be formated (closed v2)', () => {
const res = getRenovatePRFormat({ status: 3 } as any);
expect(res).toMatchSnapshot();
});
it('should be formated (not closed)', () => {
const res = getRenovatePRFormat({ status: 1 } as any);
expect(res).toMatchSnapshot();
});
it('should be formated (isConflicted)', () => {
const res = getRenovatePRFormat({ mergeStatus: 2 } as any);
expect(res).toMatchSnapshot();
});
});
describe('streamToString', () => {
it('converts Readable stream to string', async () => {
const res = await streamToString(Readable.from('foobar'));
expect(res).toEqual('foobar');
});
it('handles error', async () => {
const stream = Readable.from('foobar');
const res = streamToString(stream);
stream.destroy(new Error('some unknown error'));
await expect(res).rejects.toThrow('some unknown error');
});
});
describe('getStorageExtraCloneOpts', () => {
it('should configure basic auth', () => {
const res = getStorageExtraCloneOpts({
username: 'user',
password: 'pass',
});
expect(res).toMatchSnapshot();
});
it('should configure personal access token', () => {
const res = getStorageExtraCloneOpts({
token: '1234567890123456789012345678901234567890123456789012',
});
expect(res).toMatchSnapshot();
});
it('should configure bearer token', () => {
const res = getStorageExtraCloneOpts({ token: 'token' });
expect(res).toMatchSnapshot();
});
});
describe('max4000Chars', () => {
it('should be the same', () => {
const res = max4000Chars('Hello');
expect(res).toMatchSnapshot();
});
it('should be truncated', () => {
let str = '';
for (let i = 0; i < 5000; i += 1) {
str += 'a';
}
const res = max4000Chars(str);
expect(res).toHaveLength(3999);
});
});
describe('getProjectAndRepo', () => {
it('should return the object with same strings', () => {
const res = getProjectAndRepo('myRepoName');
expect(res).toMatchSnapshot();
});
it('should return the object with project and repo', () => {
const res = getProjectAndRepo('prjName/myRepoName');
expect(res).toMatchSnapshot();
});
it('should return an error', () => {
expect(() => getProjectAndRepo('prjName/myRepoName/blalba')).toThrow(
Error(
`prjName/myRepoName/blalba can be only structured this way : 'repository' or 'projectName/repository'!`
)
);
});
});
describe('getRepoByName', () => {
it('returns null when repos array is empty', () => {
expect(getRepoByName('foo/bar', [])).toBeNull();
expect(getRepoByName('foo/bar', undefined)).toBeNull();
expect(getRepoByName('foo/bar', null)).toBeNull();
});
it('returns null when repo is not found', () => {
expect(
getRepoByName('foo/foo', [{ name: 'bar', project: { name: 'bar' } }])
).toBeNull();
});
it('finds repo', () => {
expect(
getRepoByName('foo/bar', [
{ id: '1', name: 'baz', project: { name: 'qux' } },
null,
undefined,
{ id: '2', name: 'bar' },
{ id: '3', name: 'bar', project: { name: 'foo' } },
{ id: '4', name: 'bar', project: { name: 'foo' } },
])
).toMatchObject({ id: '3' });
});
it('supports shorthand names', () => {
expect(
getRepoByName('foo', [
{ id: '1', name: 'bar', project: { name: 'bar' } },
{ id: '2', name: 'foo', project: { name: 'foo' } },
])
).toMatchObject({ id: '2' });
});
it('is case-independent', () => {
const repos = [
{ id: '1', name: 'FOO', project: { name: 'FOO' } },
{ id: '2', name: 'foo', project: { name: 'foo' } },
];
expect(getRepoByName('FOO/foo', repos)).toMatchObject({ id: '1' });
expect(getRepoByName('foo/FOO', repos)).toMatchObject({ id: '1' });
expect(getRepoByName('foo/foo', repos)).toMatchObject({ id: '1' });
});
it('throws when repo name is invalid', () => {
expect(() => getRepoByName(undefined, [])).toThrow();
expect(() => getRepoByName(null, [])).toThrow();
expect(() => getRepoByName('foo/bar/baz', [])).toThrow();
});
});
});