Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/test/common/services/languagesAssociations.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { URI } from '../../../../base/common/uri.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';
9
import { getMimeTypes, registerPlatformLanguageAssociation, registerConfiguredLanguageAssociation } from '../../../common/services/languagesAssociations.js';
10
11
suite('LanguagesAssociations', () => {
12
13
ensureNoDisposablesAreLeakedInTestSuite();
14
15
test('Dynamically Register Text Mime', () => {
16
let guess = getMimeTypes(URI.file('foo.monaco'));
17
assert.deepStrictEqual(guess, ['application/unknown']);
18
19
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });
20
guess = getMimeTypes(URI.file('foo.monaco'));
21
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
22
23
guess = getMimeTypes(URI.file('.monaco'));
24
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
25
26
registerPlatformLanguageAssociation({ id: 'codefile', filename: 'Codefile', mime: 'text/code' });
27
guess = getMimeTypes(URI.file('Codefile'));
28
assert.deepStrictEqual(guess, ['text/code', 'text/plain']);
29
30
guess = getMimeTypes(URI.file('foo.Codefile'));
31
assert.deepStrictEqual(guess, ['application/unknown']);
32
33
registerPlatformLanguageAssociation({ id: 'docker', filepattern: 'Docker*', mime: 'text/docker' });
34
guess = getMimeTypes(URI.file('Docker-debug'));
35
assert.deepStrictEqual(guess, ['text/docker', 'text/plain']);
36
37
guess = getMimeTypes(URI.file('docker-PROD'));
38
assert.deepStrictEqual(guess, ['text/docker', 'text/plain']);
39
40
registerPlatformLanguageAssociation({ id: 'niceregex', mime: 'text/nice-regex', firstline: /RegexesAreNice/ });
41
guess = getMimeTypes(URI.file('Randomfile.noregistration'), 'RegexesAreNice');
42
assert.deepStrictEqual(guess, ['text/nice-regex', 'text/plain']);
43
44
guess = getMimeTypes(URI.file('Randomfile.noregistration'), 'RegexesAreNotNice');
45
assert.deepStrictEqual(guess, ['application/unknown']);
46
47
guess = getMimeTypes(URI.file('Codefile'), 'RegexesAreNice');
48
assert.deepStrictEqual(guess, ['text/code', 'text/plain']);
49
});
50
51
test('Mimes Priority', () => {
52
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });
53
registerPlatformLanguageAssociation({ id: 'foobar', mime: 'text/foobar', firstline: /foobar/ });
54
55
let guess = getMimeTypes(URI.file('foo.monaco'));
56
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
57
58
guess = getMimeTypes(URI.file('foo.monaco'), 'foobar');
59
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
60
61
registerPlatformLanguageAssociation({ id: 'docker', filename: 'dockerfile', mime: 'text/winner' });
62
registerPlatformLanguageAssociation({ id: 'docker', filepattern: 'dockerfile*', mime: 'text/looser' });
63
guess = getMimeTypes(URI.file('dockerfile'));
64
assert.deepStrictEqual(guess, ['text/winner', 'text/plain']);
65
66
registerPlatformLanguageAssociation({ id: 'azure-looser', mime: 'text/azure-looser', firstline: /azure/ });
67
registerPlatformLanguageAssociation({ id: 'azure-winner', mime: 'text/azure-winner', firstline: /azure/ });
68
guess = getMimeTypes(URI.file('azure'), 'azure');
69
assert.deepStrictEqual(guess, ['text/azure-winner', 'text/plain']);
70
});
71
72
test('Specificity priority 1', () => {
73
registerPlatformLanguageAssociation({ id: 'monaco2', extension: '.monaco2', mime: 'text/monaco2' });
74
registerPlatformLanguageAssociation({ id: 'monaco2', filename: 'specific.monaco2', mime: 'text/specific-monaco2' });
75
76
assert.deepStrictEqual(getMimeTypes(URI.file('specific.monaco2')), ['text/specific-monaco2', 'text/plain']);
77
assert.deepStrictEqual(getMimeTypes(URI.file('foo.monaco2')), ['text/monaco2', 'text/plain']);
78
});
79
80
test('Specificity priority 2', () => {
81
registerPlatformLanguageAssociation({ id: 'monaco3', filename: 'specific.monaco3', mime: 'text/specific-monaco3' });
82
registerPlatformLanguageAssociation({ id: 'monaco3', extension: '.monaco3', mime: 'text/monaco3' });
83
84
assert.deepStrictEqual(getMimeTypes(URI.file('specific.monaco3')), ['text/specific-monaco3', 'text/plain']);
85
assert.deepStrictEqual(getMimeTypes(URI.file('foo.monaco3')), ['text/monaco3', 'text/plain']);
86
});
87
88
test('Mimes Priority - Longest Extension wins', () => {
89
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });
90
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml', mime: 'text/monaco-xml' });
91
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml.build', mime: 'text/monaco-xml-build' });
92
93
let guess = getMimeTypes(URI.file('foo.monaco'));
94
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
95
96
guess = getMimeTypes(URI.file('foo.monaco.xml'));
97
assert.deepStrictEqual(guess, ['text/monaco-xml', 'text/plain']);
98
99
guess = getMimeTypes(URI.file('foo.monaco.xml.build'));
100
assert.deepStrictEqual(guess, ['text/monaco-xml-build', 'text/plain']);
101
});
102
103
test('Mimes Priority - User configured wins', () => {
104
registerConfiguredLanguageAssociation({ id: 'monaco', extension: '.monaco.xnl', mime: 'text/monaco' });
105
registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml', mime: 'text/monaco-xml' });
106
107
const guess = getMimeTypes(URI.file('foo.monaco.xnl'));
108
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
109
});
110
111
test('Mimes Priority - Pattern matches on path if specified', () => {
112
registerPlatformLanguageAssociation({ id: 'monaco', filepattern: '**/dot.monaco.xml', mime: 'text/monaco' });
113
registerPlatformLanguageAssociation({ id: 'other', filepattern: '*ot.other.xml', mime: 'text/other' });
114
115
const guess = getMimeTypes(URI.file('/some/path/dot.monaco.xml'));
116
assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);
117
});
118
119
test('Mimes Priority - Last registered mime wins', () => {
120
registerPlatformLanguageAssociation({ id: 'monaco', filepattern: '**/dot.monaco.xml', mime: 'text/monaco' });
121
registerPlatformLanguageAssociation({ id: 'other', filepattern: '**/dot.monaco.xml', mime: 'text/other' });
122
123
const guess = getMimeTypes(URI.file('/some/path/dot.monaco.xml'));
124
assert.deepStrictEqual(guess, ['text/other', 'text/plain']);
125
});
126
127
test('Data URIs', () => {
128
registerPlatformLanguageAssociation({ id: 'data', extension: '.data', mime: 'text/data' });
129
130
assert.deepStrictEqual(getMimeTypes(URI.parse(`data:;label:something.data;description:data,`)), ['text/data', 'text/plain']);
131
});
132
});
133
134