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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
import { shallowMount } from '@vue/test-utils';
import Line from '~/jobs/components/log/line.vue';
import LineNumber from '~/jobs/components/log/line_number.vue';
import setWindowLocation from 'helpers/set_window_location_helper';
const httpUrl = 'http://example.com';
const httpsUrl = 'https://example.com';
const queryUrl = 'https://example.com?param=val';
const mockProps = ({ text = 'Running with gitlab-runner 12.1.0 (de7731dd)' } = {}) => ({
line: {
content: [
{
text,
style: 'term-fg-l-green',
},
],
lineNumber: 0,
},
path: '/jashkenas/underscore/-/jobs/335',
});
describe('Job Log Line', () => {
let wrapper;
let data;
const createComponent = (props = {}) => {
wrapper = shallowMount(Line, {
propsData: {
...props,
},
});
};
const findLine = () => wrapper.find('span');
const findLink = () => findLine().find('a');
const findLinks = () => findLine().findAll('a');
const findLinkAttributeByIndex = (i) => findLinks().at(i).attributes();
beforeEach(() => {
data = mockProps();
createComponent(data);
});
it('renders the line number component', () => {
expect(wrapper.findComponent(LineNumber).exists()).toBe(true);
});
it('renders a span the provided text', () => {
expect(findLine().text()).toBe(data.line.content[0].text);
});
it('renders the provided style as a class attribute', () => {
expect(findLine().classes()).toContain(data.line.content[0].style);
});
describe('job urls as links', () => {
it('renders an http link', () => {
createComponent(mockProps({ text: httpUrl }));
expect(findLink().text()).toBe(httpUrl);
expect(findLink().attributes().href).toBe(httpUrl);
});
it('renders an https link', () => {
createComponent(mockProps({ text: httpsUrl }));
expect(findLink().text()).toBe(httpsUrl);
expect(findLink().attributes().href).toBe(httpsUrl);
});
it('renders a link with rel nofollow and noopener', () => {
createComponent(mockProps({ text: httpsUrl }));
expect(findLink().attributes().rel).toBe('nofollow noopener noreferrer');
});
it('renders a link with corresponding styles', () => {
createComponent(mockProps({ text: httpsUrl }));
expect(findLink().classes()).toEqual(['gl-reset-color!', 'gl-text-decoration-underline']);
});
it('renders links with queries, surrounded by questions marks', () => {
createComponent(mockProps({ text: `Did you see my url ${queryUrl}??` }));
expect(findLine().text()).toBe('Did you see my url https://example.com?param=val??');
expect(findLinkAttributeByIndex(0).href).toBe(queryUrl);
});
it('renders links with queries, surrounded by exclamation marks', () => {
createComponent(mockProps({ text: `No! The ${queryUrl}!?` }));
expect(findLine().text()).toBe('No! The https://example.com?param=val!?');
expect(findLinkAttributeByIndex(0).href).toBe(queryUrl);
});
it('renders links that have brackets `[]` in their parameters', () => {
const url = `${httpUrl}?label_name[]=frontend`;
createComponent(mockProps({ text: url }));
expect(findLine().text()).toBe(url);
expect(findLinks().at(0).text()).toBe(url);
expect(findLinks().at(0).attributes('href')).toBe(url);
});
it('renders multiple links surrounded by text', () => {
createComponent(
mockProps({ text: `Well, my HTTP url: ${httpUrl} and my HTTPS url: ${httpsUrl}` }),
);
expect(findLine().text()).toBe(
'Well, my HTTP url: http://example.com and my HTTPS url: https://example.com',
);
expect(findLinks()).toHaveLength(2);
expect(findLinkAttributeByIndex(0).href).toBe(httpUrl);
expect(findLinkAttributeByIndex(1).href).toBe(httpsUrl);
});
it('renders multiple links surrounded by text, with other symbols', () => {
createComponent(
mockProps({ text: `${httpUrl}, ${httpUrl}: ${httpsUrl}; ${httpsUrl}. ${httpsUrl}...` }),
);
expect(findLine().text()).toBe(
'http://example.com, http://example.com: https://example.com; https://example.com. https://example.com...',
);
expect(findLinks()).toHaveLength(5);
expect(findLinkAttributeByIndex(0).href).toBe(httpUrl);
expect(findLinkAttributeByIndex(1).href).toBe(httpUrl);
expect(findLinkAttributeByIndex(2).href).toBe(httpsUrl);
expect(findLinkAttributeByIndex(3).href).toBe(httpsUrl);
expect(findLinkAttributeByIndex(4).href).toBe(httpsUrl);
});
it('renders multiple links surrounded by brackets', () => {
createComponent(mockProps({ text: `(${httpUrl}) <${httpUrl}> {${httpsUrl}}` }));
expect(findLine().text()).toBe(
'(http://example.com) <http://example.com> {https://example.com}',
);
const links = findLinks();
expect(links).toHaveLength(3);
expect(links.at(0).text()).toBe(httpUrl);
expect(links.at(0).attributes('href')).toBe(httpUrl);
expect(links.at(1).text()).toBe(httpUrl);
expect(links.at(1).attributes('href')).toBe(httpUrl);
expect(links.at(2).text()).toBe(httpsUrl);
expect(links.at(2).attributes('href')).toBe(httpsUrl);
});
it('renders text with symbols in it', () => {
const text = 'apt-get update < /dev/null > /dev/null';
createComponent(mockProps({ text }));
expect(findLine().text()).toBe(text);
});
const jshref = 'javascript:doEvil();'; // eslint-disable-line no-script-url
it.each`
type | text
${'html link'} | ${'<a href="#">linked</a>'}
${'html script'} | ${'<script>doEvil();</script>'}
${'html strong'} | ${'<strong>highlighted</strong>'}
${'js'} | ${jshref}
${'file'} | ${'file:///a-file'}
${'ftp'} | ${'ftp://example.com/file'}
${'email'} | ${'email@example.com'}
${'no scheme'} | ${'example.com/page'}
`('does not render a $type link', ({ text }) => {
createComponent(mockProps({ text }));
expect(findLink().exists()).toBe(false);
});
});
describe('job log search', () => {
const mockSearchResults = [
{
offset: 1533,
content: [{ text: '$ echo "82.71"', style: 'term-fg-l-green term-bold' }],
section: 'step-script',
lineNumber: 20,
},
{ offset: 1560, content: [{ text: '82.71' }], section: 'step-script', lineNumber: 21 },
];
it('applies highlight class to search result elements', () => {
createComponent({
line: {
offset: 1560,
content: [{ text: '82.71' }],
section: 'step-script',
lineNumber: 21,
},
path: '/root/ci-project/-/jobs/1089',
searchResults: mockSearchResults,
});
expect(wrapper.classes()).toContain('gl-bg-gray-700');
});
it('does not apply highlight class to search result elements', () => {
createComponent({
line: {
offset: 1560,
content: [{ text: 'docker' }],
section: 'step-script',
lineNumber: 29,
},
path: '/root/ci-project/-/jobs/1089',
searchResults: mockSearchResults,
});
expect(wrapper.classes()).not.toContain('gl-bg-gray-700');
});
});
describe('job log hash highlighting', () => {
describe('with hash', () => {
beforeEach(() => {
setWindowLocation(`http://foo.com/root/ci-project/-/jobs/6353#L77`);
});
it('applies highlight class to job log line', () => {
createComponent({
line: {
offset: 24526,
content: [{ text: 'job log content' }],
section: 'custom-section',
lineNumber: 76,
},
path: '/root/ci-project/-/jobs/6353',
});
expect(wrapper.classes()).toContain('gl-bg-gray-700');
});
});
describe('without hash', () => {
beforeEach(() => {
setWindowLocation(`http://foo.com/root/ci-project/-/jobs/6353`);
});
it('does not apply highlight class to job log line', () => {
createComponent({
line: {
offset: 24500,
content: [{ text: 'line' }],
section: 'custom-section',
lineNumber: 10,
},
path: '/root/ci-project/-/jobs/6353',
});
expect(wrapper.classes()).not.toContain('gl-bg-gray-700');
});
});
});
});
|