Skip to content

Commit af6fabb

Browse files
committed
Add tests for bugs in reverted formatter changes
- Readd the fix that doesn't return any text edits if the formatter changes nothing - Add a bunch of tests for the cases where the new formatter was failing Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 58b628b commit af6fabb

2 files changed

Lines changed: 88 additions & 5 deletions

File tree

src/languageservice/services/yamlFormatter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ export class YAMLFormatter {
4949
};
5050

5151
const formatted = await format(text, prettierOptions);
52+
if (formatted === text) {
53+
return [];
54+
}
5255

5356
return [TextEdit.replace(Range.create(Position.create(0, 0), document.positionAt(text.length)), formatted)];
5457
} catch (error) {

test/formatter.test.ts

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55
import * as assert from 'assert';
66
import * as sinon from 'sinon';
7-
import { TextEdit } from 'vscode-languageserver-types';
7+
import { FormattingOptions, TextEdit } from 'vscode-languageserver-types';
8+
import { CustomFormatterOptions } from '../src';
89
import { LanguageHandlers } from '../src/languageserver/handlers/languageHandlers';
910
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
1011
import { ServiceSetup } from './utils/serviceSetup';
@@ -38,13 +39,16 @@ describe('Formatter Tests', () => {
3839
describe('Formatter', function () {
3940
describe('Test that formatter works with custom tags', function () {
4041
// eslint-disable-next-line @typescript-eslint/no-explicit-any
41-
function parseSetup(content: string, options: any = {}): Promise<TextEdit[]> {
42+
function parseSetup(
43+
content: string,
44+
options: Partial<FormattingOptions | CustomFormatterOptions> = {}
45+
): Promise<TextEdit[]> {
4246
const testTextDocument = setupTextDocument(content);
4347
yamlSettings.documents = new TextDocumentTestManager();
4448
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
45-
yamlSettings.yamlFormatterSettings = options;
49+
yamlSettings.yamlFormatterSettings = options as CustomFormatterOptions;
4650
return languageHandler.formatterHandler({
47-
options,
51+
options: options as FormattingOptions,
4852
textDocument: testTextDocument,
4953
});
5054
}
@@ -98,7 +102,7 @@ describe('Formatter Tests', () => {
98102
}
99103
`;
100104
const edits = await parseSetup(content, { singleQuote: true });
101-
assert.equal(edits[0].newText, content);
105+
assert.equal(edits.length, 0);
102106
});
103107

104108
it('Formatting handles trailing commas (disabled)', async () => {
@@ -194,6 +198,82 @@ list:
194198
`;
195199
assert.equal(edits[0].newText, expected);
196200
});
201+
202+
it("Formatting doesn't replace escaped newlines with real ones", async () => {
203+
const content = `- name: Example task
204+
set_fact:
205+
my_var: "{{ content | regex_replace('\\\\\\\\n', '\\n') }}"
206+
`;
207+
208+
const edits = await parseSetup(content, {
209+
tabSize: 1,
210+
tabWidth: 2,
211+
});
212+
213+
assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`);
214+
});
215+
it("Formatting doesn't strip trailing zeros of floats", async () => {
216+
const content = `value: 1.0e+4
217+
`;
218+
219+
const edits = await parseSetup(content, {
220+
tabSize: 1,
221+
tabWidth: 2,
222+
});
223+
224+
assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`);
225+
});
226+
it("Formatting doesn't convert long integers to floats", async () => {
227+
const content = `value: 12345678901234567890
228+
`;
229+
230+
const edits = await parseSetup(content, {
231+
tabSize: 1,
232+
tabWidth: 2,
233+
});
234+
235+
assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`);
236+
});
237+
it("Formatting respects 'no prose wrap' setting", async () => {
238+
const content = `value: aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa
239+
`;
240+
241+
const edits = await parseSetup(content, {
242+
tabSize: 1,
243+
tabWidth: 2,
244+
proseWrap: 'never',
245+
});
246+
247+
assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`);
248+
249+
const edits2 = await parseSetup(content, {
250+
tabSize: 1,
251+
tabWidth: 2,
252+
proseWrap: 'preserve',
253+
});
254+
255+
assert.equal(edits2.length, 0, `Edits: ${JSON.stringify(edits)}`);
256+
});
257+
it('Formatting keeps comments on the same line', async () => {
258+
const content = `0002-https-from-avdpool-to-server: # Allow HTTPS access from AVD Pool 2 to Qlik server
259+
name: 0002-https-from-avdpool-to-server
260+
source_addresses:
261+
- 1.2.3.4/32
262+
protocols:
263+
- TCP
264+
destination_ports:
265+
- "443"
266+
destination_addresses:
267+
- 4.5.6.7/32
268+
`;
269+
270+
const edits = await parseSetup(content, {
271+
tabSize: 1,
272+
tabWidth: 2,
273+
});
274+
275+
assert.equal(edits.length, 0, `Edits: ${JSON.stringify(edits)}`);
276+
});
197277
});
198278
});
199279
});

0 commit comments

Comments
 (0)