aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/helpers/__tests__/markdown-test.ts
blob: 5c74c9f20ab3009bce081b0cceb1175e7ceb54ad (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2022 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/* eslint-disable no-console */
import { filterContent, getFrontMatter, separateFrontMatter } from '../markdown';

jest.mock('../system', () => ({
  getInstance: () => 'SonarQube'
}));

it('returns parsed frontmatter of one item', () => {
  expect(
    getFrontMatter(`
      ---
      title: Foo
      ---
      
      some content here
    `)
  ).toEqual({ title: 'Foo' });
});

it('returns parsed frontmatter of two items', () => {
  expect(
    getFrontMatter(`
      ---
      title: Foo
      scope: sonarcloud
      ---
      
      some content here
    `)
  ).toEqual({ title: 'Foo', scope: 'sonarcloud' });
});

it('returns empty object when frontmatter is missing', () => {
  expect(
    getFrontMatter(`
      some content here
    `)
  ).toEqual({});
});

it('returns empty object when frontmatter is unfinished', () => {
  expect(
    getFrontMatter(`
      --- 
      title: Foo

      some content here
    `)
  ).toEqual({});
});

it('ignores frontmatter in wrong format', () => {
  expect(
    getFrontMatter(`
      --- 
      title: Foo
      scope: sonarcloud: sonarqube
      ---

      some content here
    `)
  ).toEqual({ title: 'Foo' });
});

it('returns parsed frontmatter and the rest of the content', () => {
  expect(
    separateFrontMatter(`
---
title: Foo
---

some content here`)
  ).toEqual({ content: '\nsome content here', frontmatter: { title: 'Foo' } });
});

it('returns empty object and content when  frontmatter is missing', () => {
  expect(separateFrontMatter('some content here')).toEqual({
    content: 'some content here',
    frontmatter: {}
  });
});

it('returns full content when frontmatter has bad formatting', () => {
  const content = `
    ----
    title: Foo
    scope: sonarcloud
    ---

    some content here`;

  expect(separateFrontMatter(content)).toEqual({ content, frontmatter: {} });
});

it('replaces {instance}', () => {
  expect(
    filterContent('This is {instance} content. It replaces all {instance}{instance} messages')
  ).toBe('This is SonarQube content. It replaces all SonarQubeSonarQube messages');
});

it('should cut sonarqube/sonarcloud/static content', () => {
  const content = `
This text has inline text for <!-- sonarqube -->SonarQube<!-- /sonarqube --><!-- sonarcloud -->SonarCloud<!-- /sonarcloud -->. Donec sed nulla magna.

<!-- sonarqube -->
This is text for SonarQube, multi-line. Consectetur adipiscing elit. Duis dignissim nulla at massa iaculis interdum.
Aenean sit amet lacus a tortor ullamcorper interdum. Donec sed nulla magna.
<!-- /sonarqube -->

<!-- sonarcloud -->
This is text for SonarCloud, multi-line. In hac habitasse platea dictumst. Duis sagittis semper sapien nec tempor. Nullam vehicula nisi vitae nisi interdum aliquam. Mauris volutpat nunc non fermentum rhoncus. Aenean laoreet, orci vitae tempor bibendum,
metus nisl euismod neque, vitae euismod nibh nisl eu velit. Vivamus luctus suscipit elit vel semper.
<!-- /sonarcloud -->

<!-- static -->
This is static text.
<!-- /static -->

<!-- sonarqube -->
This is text for SonarQube, single line.
<!-- /sonarqube -->

* In hac habitasse
* Duis sagittis semper sapien nec tempor
<!-- sonarqube -->* This is a bullet point for SonarQube<!-- /sonarqube -->
<!-- sonarcloud -->* This is a bullet point for SonarCloud<!-- /sonarcloud -->
* Platea dictumst

Duis sagittis semper sapien nec tempor. Nullam vehicula nisi vitae nisi interdum aliquam.

| Parameter Name        | Description |
| --------------------- | ------------------ |
| sonar.pullrequest.github.repository | SLUG of the GitHub Repo |
<!-- sonarqube -->
| sonar.pullrequest.github.endpoint | The API url for your GitHub instance. |
<!-- /sonarqube -->
`;

  expect(filterContent(content)).toMatchSnapshot();
});

it('should not break when conditional tags are misused', () => {
  const originalConsoleError = console.error;
  console.error = jest.fn();

  const content = `Random <!-- /sonarqube -->SC <!-- sonarqube -->text
  Break
  Bad <!-- /sonarcloud -->SQ conditional <!-- sonarcloud -->formatting
  Break
  <!-- sonarqube -->SC<!-- /sonarqube --><!-- sonarcloud -->SQ<!-- /sonarcloud --> text
  Break
  Bad <!-- /sonarcloud -->SQ conditional <!-- sonarcloud -->formatting
  Break
  <!-- static -->Static <!-- /sonarcloud -->stuff`;
  expect(filterContent(content)).toMatchSnapshot();
  expect(console.error).toHaveBeenCalledTimes(2);

  console.error = originalConsoleError;
});