Path: blob/main/src/vs/editor/test/common/services/languagesAssociations.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { URI } from '../../../../base/common/uri.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js';8import { getMimeTypes, registerPlatformLanguageAssociation, registerConfiguredLanguageAssociation } from '../../../common/services/languagesAssociations.js';910suite('LanguagesAssociations', () => {1112ensureNoDisposablesAreLeakedInTestSuite();1314test('Dynamically Register Text Mime', () => {15let guess = getMimeTypes(URI.file('foo.monaco'));16assert.deepStrictEqual(guess, ['application/unknown']);1718registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });19guess = getMimeTypes(URI.file('foo.monaco'));20assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);2122guess = getMimeTypes(URI.file('.monaco'));23assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);2425registerPlatformLanguageAssociation({ id: 'codefile', filename: 'Codefile', mime: 'text/code' });26guess = getMimeTypes(URI.file('Codefile'));27assert.deepStrictEqual(guess, ['text/code', 'text/plain']);2829guess = getMimeTypes(URI.file('foo.Codefile'));30assert.deepStrictEqual(guess, ['application/unknown']);3132registerPlatformLanguageAssociation({ id: 'docker', filepattern: 'Docker*', mime: 'text/docker' });33guess = getMimeTypes(URI.file('Docker-debug'));34assert.deepStrictEqual(guess, ['text/docker', 'text/plain']);3536guess = getMimeTypes(URI.file('docker-PROD'));37assert.deepStrictEqual(guess, ['text/docker', 'text/plain']);3839registerPlatformLanguageAssociation({ id: 'niceregex', mime: 'text/nice-regex', firstline: /RegexesAreNice/ });40guess = getMimeTypes(URI.file('Randomfile.noregistration'), 'RegexesAreNice');41assert.deepStrictEqual(guess, ['text/nice-regex', 'text/plain']);4243guess = getMimeTypes(URI.file('Randomfile.noregistration'), 'RegexesAreNotNice');44assert.deepStrictEqual(guess, ['application/unknown']);4546guess = getMimeTypes(URI.file('Codefile'), 'RegexesAreNice');47assert.deepStrictEqual(guess, ['text/code', 'text/plain']);48});4950test('Mimes Priority', () => {51registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });52registerPlatformLanguageAssociation({ id: 'foobar', mime: 'text/foobar', firstline: /foobar/ });5354let guess = getMimeTypes(URI.file('foo.monaco'));55assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);5657guess = getMimeTypes(URI.file('foo.monaco'), 'foobar');58assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);5960registerPlatformLanguageAssociation({ id: 'docker', filename: 'dockerfile', mime: 'text/winner' });61registerPlatformLanguageAssociation({ id: 'docker', filepattern: 'dockerfile*', mime: 'text/looser' });62guess = getMimeTypes(URI.file('dockerfile'));63assert.deepStrictEqual(guess, ['text/winner', 'text/plain']);6465registerPlatformLanguageAssociation({ id: 'azure-looser', mime: 'text/azure-looser', firstline: /azure/ });66registerPlatformLanguageAssociation({ id: 'azure-winner', mime: 'text/azure-winner', firstline: /azure/ });67guess = getMimeTypes(URI.file('azure'), 'azure');68assert.deepStrictEqual(guess, ['text/azure-winner', 'text/plain']);69});7071test('Specificity priority 1', () => {72registerPlatformLanguageAssociation({ id: 'monaco2', extension: '.monaco2', mime: 'text/monaco2' });73registerPlatformLanguageAssociation({ id: 'monaco2', filename: 'specific.monaco2', mime: 'text/specific-monaco2' });7475assert.deepStrictEqual(getMimeTypes(URI.file('specific.monaco2')), ['text/specific-monaco2', 'text/plain']);76assert.deepStrictEqual(getMimeTypes(URI.file('foo.monaco2')), ['text/monaco2', 'text/plain']);77});7879test('Specificity priority 2', () => {80registerPlatformLanguageAssociation({ id: 'monaco3', filename: 'specific.monaco3', mime: 'text/specific-monaco3' });81registerPlatformLanguageAssociation({ id: 'monaco3', extension: '.monaco3', mime: 'text/monaco3' });8283assert.deepStrictEqual(getMimeTypes(URI.file('specific.monaco3')), ['text/specific-monaco3', 'text/plain']);84assert.deepStrictEqual(getMimeTypes(URI.file('foo.monaco3')), ['text/monaco3', 'text/plain']);85});8687test('Mimes Priority - Longest Extension wins', () => {88registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco', mime: 'text/monaco' });89registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml', mime: 'text/monaco-xml' });90registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml.build', mime: 'text/monaco-xml-build' });9192let guess = getMimeTypes(URI.file('foo.monaco'));93assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);9495guess = getMimeTypes(URI.file('foo.monaco.xml'));96assert.deepStrictEqual(guess, ['text/monaco-xml', 'text/plain']);9798guess = getMimeTypes(URI.file('foo.monaco.xml.build'));99assert.deepStrictEqual(guess, ['text/monaco-xml-build', 'text/plain']);100});101102test('Mimes Priority - User configured wins', () => {103registerConfiguredLanguageAssociation({ id: 'monaco', extension: '.monaco.xnl', mime: 'text/monaco' });104registerPlatformLanguageAssociation({ id: 'monaco', extension: '.monaco.xml', mime: 'text/monaco-xml' });105106const guess = getMimeTypes(URI.file('foo.monaco.xnl'));107assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);108});109110test('Mimes Priority - Pattern matches on path if specified', () => {111registerPlatformLanguageAssociation({ id: 'monaco', filepattern: '**/dot.monaco.xml', mime: 'text/monaco' });112registerPlatformLanguageAssociation({ id: 'other', filepattern: '*ot.other.xml', mime: 'text/other' });113114const guess = getMimeTypes(URI.file('/some/path/dot.monaco.xml'));115assert.deepStrictEqual(guess, ['text/monaco', 'text/plain']);116});117118test('Mimes Priority - Last registered mime wins', () => {119registerPlatformLanguageAssociation({ id: 'monaco', filepattern: '**/dot.monaco.xml', mime: 'text/monaco' });120registerPlatformLanguageAssociation({ id: 'other', filepattern: '**/dot.monaco.xml', mime: 'text/other' });121122const guess = getMimeTypes(URI.file('/some/path/dot.monaco.xml'));123assert.deepStrictEqual(guess, ['text/other', 'text/plain']);124});125126test('Data URIs', () => {127registerPlatformLanguageAssociation({ id: 'data', extension: '.data', mime: 'text/data' });128129assert.deepStrictEqual(getMimeTypes(URI.parse(`data:;label:something.data;description:data,`)), ['text/data', 'text/plain']);130});131});132133134