-
-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy patheditorconfig-test.js
More file actions
110 lines (97 loc) · 3.69 KB
/
editorconfig-test.js
File metadata and controls
110 lines (97 loc) · 3.69 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const { resolveEditorConfig } = require('../../../lib/utils/editorconfig');
describe('resolveEditorConfig', () => {
let tmpDir;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'editorconfig-test-'));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('returns empty object when no .editorconfig exists', () => {
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result).toEqual({});
});
it('reads indent_size from .editorconfig', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});
it('matches *.hbs sections', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.hbs]', 'indent_size = 3'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(3);
});
it('does not match non-matching glob', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.js]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBeUndefined();
});
it('handles brace expansion *.{hbs,gjs}', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*.{hbs,gjs}]', 'indent_size = 6'].join('\n')
);
expect(resolveEditorConfig(path.join(tmpDir, 'test.hbs')).indent_size).toBe(6);
expect(resolveEditorConfig(path.join(tmpDir, 'test.gjs')).indent_size).toBe(6);
expect(resolveEditorConfig(path.join(tmpDir, 'test.js')).indent_size).toBeUndefined();
});
it('later sections override earlier ones', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 2', '', '[*.hbs]', 'indent_size = 4'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});
it('inner .editorconfig overrides outer', () => {
// outer
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_size = 2'].join('\n')
);
// inner dir
const innerDir = path.join(tmpDir, 'app');
fs.mkdirSync(innerDir);
fs.writeFileSync(path.join(innerDir, '.editorconfig'), ['[*]', 'indent_size = 4'].join('\n'));
const filePath = path.join(innerDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(4);
});
it('sets indent_size to tab when indent_style is tab and indent_size unset', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '', '[*]', 'indent_style = tab'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_style).toBe('tab');
expect(result.indent_size).toBe('tab');
});
it('ignores comments', () => {
fs.writeFileSync(
path.join(tmpDir, '.editorconfig'),
['root = true', '# a comment', '; another comment', '[*]', 'indent_size = 5'].join('\n')
);
const filePath = path.join(tmpDir, 'test.hbs');
const result = resolveEditorConfig(filePath);
expect(result.indent_size).toBe(5);
});
});