From 55ef45360b93d23a588715b916323d5eed9b001b Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 24 Apr 2024 17:17:54 +0100 Subject: [PATCH] More types (tests) Related: #1225 --- .../src/providers/completionProvider.ts | 4 +- .../test/providers/completionProvider.test.ts | 1133 +++++++++-------- .../test/providers/completionResolver.test.ts | 117 +- .../test/providers/validationProvider.test.ts | 327 ++--- .../test/utils/yaml.test.ts | 53 +- 5 files changed, 869 insertions(+), 765 deletions(-) diff --git a/packages/ansible-language-server/src/providers/completionProvider.ts b/packages/ansible-language-server/src/providers/completionProvider.ts index 75a0fbc70..58b61b222 100644 --- a/packages/ansible-language-server/src/providers/completionProvider.ts +++ b/packages/ansible-language-server/src/providers/completionProvider.ts @@ -59,7 +59,7 @@ export async function doCompletion( document: TextDocument, position: Position, context: WorkspaceFolderContext, -): Promise { +): Promise { isAnsiblePlaybook = isPlaybook(document); let preparedText = document.getText(); @@ -437,7 +437,7 @@ export async function doCompletion( } } } - return null; + return []; } function getKeywordCompletion( diff --git a/packages/ansible-language-server/test/providers/completionProvider.test.ts b/packages/ansible-language-server/test/providers/completionProvider.test.ts index d8821dc06..88556dbfd 100644 --- a/packages/ansible-language-server/test/providers/completionProvider.test.ts +++ b/packages/ansible-language-server/test/providers/completionProvider.test.ts @@ -1,6 +1,10 @@ import { TextDocument } from "vscode-languageserver-textdocument"; import { expect } from "chai"; -import { Position, CompletionItemKind } from "vscode-languageserver"; +import { + Position, + CompletionItemKind, + CompletionItem, +} from "vscode-languageserver"; import { doCompletion, doCompletionResolve, @@ -20,7 +24,7 @@ import { } from "../helper"; function testPlayKeywords( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -38,27 +42,30 @@ function testPlayKeywords( }, ]; - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testRoleKeywords( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -76,27 +83,30 @@ function testRoleKeywords( }, ]; - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testBlockKeywords( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -113,28 +123,30 @@ function testBlockKeywords( completion: "become", }, ]; + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testTaskKeywords( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -151,28 +163,30 @@ function testTaskKeywords( completion: "debugger", }, ]; + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testModuleNames( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -228,25 +242,30 @@ function testModuleNames( tests.forEach(({ name, position, triggerCharacter, completion }) => { it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).to.contain(completion); - expect(filteredCompletion[0].textEdit.newText).to.contain(completion); + expect(context).is.not.undefined; + if (context) { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).to.contain(completion); + expect(filteredCompletion[0].textEdit?.newText).to.contain( + completion, + ); + } } }); }); } function testModuleOptions( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -290,25 +309,28 @@ function testModuleOptions( tests.forEach(({ name, position, triggerCharacter, completion }) => { it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); + expect(context).is.not.undefined; + if (context) { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } } }); }); } function testModuleOptionsValues( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -346,39 +368,42 @@ function testModuleOptionsValues( tests.forEach(({ name, position, triggerCharacter, completion }) => { it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const labelCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ).map((completion) => { - return completion.label; - }); - - if (!completion) { - expect(labelCompletion.length).be.equal(0); - } else { - expect(labelCompletion).be.deep.equal(completion); - } - - const newTextCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ).map((completion) => { - return completion.textEdit.newText; - }); - - if (!completion) { - expect(newTextCompletion.length).be.equal(0); - } else { - expect(newTextCompletion).be.deep.equal(completion); + expect(context).is.not.undefined; + if (context) { + const actualCompletion = await doCompletion(textDoc, position, context); + + const labelCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ).map((completion) => { + return completion.label; + }); + + if (!completion) { + expect(labelCompletion.length).be.equal(0); + } else { + expect(labelCompletion).be.deep.equal(completion); + } + + const newTextCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ).map((completion) => { + return completion.textEdit?.newText; + }); + + if (!completion) { + expect(newTextCompletion.length).be.equal(0); + } else { + expect(newTextCompletion).be.deep.equal(completion); + } } }); }); } function testModuleNamesWithoutFQCN( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -408,36 +433,40 @@ function testModuleNamesWithoutFQCN( }, ]; - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - // Update setting to stop using FQCN for module names - const docSettings = context.documentSettings.get(textDoc.uri); - const cachedDefaultSetting = (await docSettings).validation.lint.enabled; - (await docSettings).ansible.useFullyQualifiedCollectionNames = false; - - const actualCompletion = await doCompletion(textDoc, position, context); - - // Revert back the default settings - (await docSettings).ansible.useFullyQualifiedCollectionNames = - cachedDefaultSetting; - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + // Update setting to stop using FQCN for module names + const docSettings = context.documentSettings.get(textDoc.uri); + const cachedDefaultSetting = (await docSettings).validation.lint + .enabled; + (await docSettings).ansible.useFullyQualifiedCollectionNames = false; + + const actualCompletion = await doCompletion(textDoc, position, context); + + // Revert back the default settings + (await docSettings).ansible.useFullyQualifiedCollectionNames = + cachedDefaultSetting; + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testPlaybookAdjacentCollection( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -460,28 +489,30 @@ function testPlaybookAdjacentCollection( completion: "sub_opt_1", }, ]; + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testNonPlaybookAdjacentCollection( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -498,28 +529,30 @@ function testNonPlaybookAdjacentCollection( completion: "", }, ]; + expect(context).is.not.undefined; + if (context) { + tests.forEach(({ name, position, triggerCharacter, completion }) => { + it(`should not provide completion for ${name}`, async function () { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ); - tests.forEach(({ name, position, triggerCharacter, completion }) => { - it(`should not provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion[0].label).be.equal(completion); - expect(filteredCompletion[0].textEdit.newText).be.equal(completion); - } + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion[0].label).be.equal(completion); + expect(filteredCompletion[0].textEdit?.newText).be.equal(completion); + } + }); }); - }); + } } function testHostValues( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -551,26 +584,29 @@ function testHostValues( tests.forEach(({ name, position, triggerCharacter, completion }) => { it(`should provide completion for ${name} as hosts value`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ).map((completion) => { - return completion.label; - }); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion).be.deep.equal(completion); + expect(context).is.not.undefined; + if (context) { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ).map((completion) => { + return completion.label; + }); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion).be.deep.equal(completion); + } } }); }); } function testVarsCompletionInsideJinja( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -608,26 +644,29 @@ function testVarsCompletionInsideJinja( tests.forEach(({ name, position, triggerCharacter, completion }) => { it(`should provide completion for ${name}`, async function () { - const actualCompletion = await doCompletion(textDoc, position, context); - - const filteredCompletion = smartFilter( - actualCompletion, - triggerCharacter, - ).map((completion) => { - return completion.label; - }); - - if (!completion) { - expect(filteredCompletion.length).be.equal(0); - } else { - expect(filteredCompletion).be.deep.equal(completion); + expect(context).is.not.undefined; + if (context) { + const actualCompletion = await doCompletion(textDoc, position, context); + + const filteredCompletion = smartFilter( + actualCompletion, + triggerCharacter, + ).map((completion) => { + return completion.label; + }); + + if (!completion) { + expect(filteredCompletion.length).be.equal(0); + } else { + expect(filteredCompletion).be.deep.equal(completion); + } } }); }); } function testModuleKindAndDocumentation( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, textDoc: TextDocument, ) { const tests = [ @@ -658,24 +697,34 @@ function testModuleKindAndDocumentation( ]; tests.forEach(({ moduleName, kind, documentation }) => { - let resolvedItem; + let resolvedItem: CompletionItem; const position = { line: 34, character: 19 } as Position; - before(async () => { - const completion = await doCompletion(textDoc, position, context); - const filteredCompletion = completion.filter( - (item) => item.label === moduleName, - ); - expect(filteredCompletion.length).be.equal(1); - expect(filteredCompletion[0].label).be.equal(moduleName); - resolvedItem = await doCompletionResolve(filteredCompletion[0], context); - }); - it(`should provide completion kind ${kind} for ${moduleName}`, () => { - expect(resolvedItem.kind).be.equal(kind); - }); - it(`should provide documentation for ${moduleName}`, () => { - expect(resolvedItem.documentation["value"]).be.equal(documentation); - }); + expect(context).is.not.undefined; + if (context) { + before(async () => { + const completion = await doCompletion(textDoc, position, context); + const filteredCompletion = completion.filter( + (item) => item.label === moduleName, + ); + expect(filteredCompletion.length).be.equal(1); + expect(filteredCompletion[0].label).be.equal(moduleName); + resolvedItem = await doCompletionResolve( + filteredCompletion[0], + context, + ); + }); + it(`should provide completion kind ${kind} for ${moduleName}`, () => { + expect(resolvedItem.kind).be.equal(kind); + }); + it(`should provide documentation for ${moduleName}`, () => { + let doc = resolvedItem.documentation; + if (typeof doc !== "string" && doc) { + doc = doc["value"]; + } + expect(doc).be.equal(documentation); + }); + } }); } @@ -686,403 +735,421 @@ describe("doCompletion()", () => { let context = workspaceManager.getContext(fixtureFileUri); let textDoc = getDoc(fixtureFilePath); - let docSettings = context.documentSettings.get(textDoc.uri); - - describe("Completion for host values with static inventory file", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); - }); - - testHostValues(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for host values with static inventory file", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testHostValues(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); - setAnsibleConfigEnv(); - }); + setAnsibleConfigEnv(); + }); - testHostValues(context, textDoc); + testHostValues(context, textDoc); - after(() => { - unsetAnsibleConfigEnv(); + after(() => { + unsetAnsibleConfigEnv(); + }); }); }); - }); - describe("Completion for play keywords", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + describe("Completion for play keywords", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testPlayKeywords(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testPlayKeywords(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testPlayKeywords(context, textDoc); }); }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testPlayKeywords(context, textDoc); - }); - }); - + } fixtureFilePath = "completion/with_roles.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - describe("Completion for role keywords", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for role keywords", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testRoleKeywords(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testRoleKeywords(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testRoleKeywords(context, textDoc); }); - - testRoleKeywords(context, textDoc); }); - }); - + } fixtureFilePath = "completion/with_blocks.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("Completion for block keywords", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for block keywords", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testBlockKeywords(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testBlockKeywords(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testBlockKeywords(context, textDoc); }); }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testBlockKeywords(context, textDoc); - }); - }); - + } fixtureFilePath = "completion/simple_tasks.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("Completion for task keywords", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for task keywords", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testTaskKeywords(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testTaskKeywords(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testTaskKeywords(context, textDoc); }); - - testTaskKeywords(context, textDoc); }); - }); - describe("Completion for module names (with different trigger scenarios)", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + describe("Completion for module names (with different trigger scenarios)", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testModuleNames(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testModuleNames(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testModuleNames(context, textDoc); }); }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + describe("Check module kind and documentation of completion item", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testModuleKindAndDocumentation(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testModuleNames(context, textDoc); - }); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - describe("Check module kind and documentation of completion item", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); - }); - - testModuleKindAndDocumentation(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testModuleKindAndDocumentation(context, textDoc); }); }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + describe("Completion for module options and suboptions", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testModuleOptions(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testModuleKindAndDocumentation(context, textDoc); - }); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - describe("Completion for module options and suboptions", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + testModuleOptions(context, textDoc); }); - - testModuleOptions(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testModuleOptions(context, textDoc); }); - }); - describe("Completion for option and suboption values", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + describe("Completion for option and suboption values", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testModuleOptionsValues(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testModuleOptionsValues(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testModuleOptionsValues(context, textDoc); }); }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testModuleOptionsValues(context, textDoc); - }); - }); - + } fixtureFilePath = "completion/tasks_without_fqcn.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("Completion for module name without FQCN", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + let docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for module name without FQCN", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testModuleNamesWithoutFQCN(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testModuleNamesWithoutFQCN(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testModuleNamesWithoutFQCN(context, textDoc); }); }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testModuleNamesWithoutFQCN(context, textDoc); - }); - }); - - describe("Completion for variables inside jinja inline brackets", () => { - fixtureFilePath = "completion/playbook_with_vars.yml"; - fixtureFileUri = resolveDocUri(fixtureFilePath); - context = workspaceManager.getContext(fixtureFileUri); - textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); - }); - - testVarsCompletionInsideJinja(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testVarsCompletionInsideJinja(context, textDoc); + describe("Completion for variables inside jinja inline brackets", () => { + fixtureFilePath = "completion/playbook_with_vars.yml"; + fixtureFileUri = resolveDocUri(fixtureFilePath); + context = workspaceManager.getContext(fixtureFileUri); + textDoc = getDoc(fixtureFilePath); + expect(context).is.not.undefined; + if (context) { + docSettings = context.documentSettings.get(textDoc.uri); + + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testVarsCompletionInsideJinja(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + }); + + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + + testVarsCompletionInsideJinja(context, textDoc); + }); + } }); - }); - + } fixtureFilePath = "playbook_adjacent_collection/playbook.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("Completion for playbook adjacent collection", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Completion for playbook adjacent collection", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testPlaybookAdjacentCollection(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testPlaybookAdjacentCollection(context, textDoc); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testPlaybookAdjacentCollection(context, textDoc); }); - - testPlaybookAdjacentCollection(context, textDoc); }); - }); - + } fixtureFilePath = "playbook_adjacent_collection/non_adjacent_playbooks/playbook2.yml"; fixtureFileUri = resolveDocUri(fixtureFilePath); context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("Negate completion for non playbook adjacent collection", () => { - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + expect(context).is.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); + + describe("Negate completion for non playbook adjacent collection", () => { + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testNonPlaybookAdjacentCollection(context, textDoc); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testNonPlaybookAdjacentCollection(context, textDoc); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testNonPlaybookAdjacentCollection(context, textDoc); }); }); - - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testNonPlaybookAdjacentCollection(context, textDoc); - }); - }); + } }); diff --git a/packages/ansible-language-server/test/providers/completionResolver.test.ts b/packages/ansible-language-server/test/providers/completionResolver.test.ts index 91530460c..6cc8c882c 100644 --- a/packages/ansible-language-server/test/providers/completionResolver.test.ts +++ b/packages/ansible-language-server/test/providers/completionResolver.test.ts @@ -216,49 +216,86 @@ describe("doCompletionResolve()", () => { const context = workspaceManager.getContext(fixtureFileUri); const textDoc = getDoc(fixtureFilePath); - const docSettings = context.documentSettings.get(textDoc.uri); + expect(context).be.not.undefined; + if (context) { + const docSettings = context.documentSettings.get(textDoc.uri); - describe("Resolve completion for module names", () => { - describe("Resolve completion for module names when FQCN is enabled", function () { - describe("with EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); + describe("Resolve completion for module names", () => { + describe("Resolve completion for module names when FQCN is enabled", function () { + describe("with EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + testFQCNEnabled(context); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - testFQCNEnabled(context); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + describe("with EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + testFQCNEnabled(context); }); }); - describe("with EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + describe("Resolve completion for module names when FQCN is disabled", function () { + describe("with EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + (await docSettings).ansible.useFullyQualifiedCollectionNames = + false; + }); + testFQCNDisabled(context); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + (await docSettings).ansible.useFullyQualifiedCollectionNames = true; + }); + }); + + describe("with EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + (await docSettings).ansible.useFullyQualifiedCollectionNames = + false; + }); + testFQCNDisabled(context); + + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + (await docSettings).ansible.useFullyQualifiedCollectionNames = true; + }); }); - testFQCNEnabled(context); }); }); - describe("Resolve completion for module names when FQCN is disabled", function () { + describe("Resolve completion for module options and suboptions", () => { describe("with EE enabled @ee", () => { before(async () => { setFixtureAnsibleCollectionPathEnv( "/home/runner/.ansible/collections:/usr/share/ansible", ); await enableExecutionEnvironmentSettings(docSettings); - (await docSettings).ansible.useFullyQualifiedCollectionNames = false; }); - testFQCNDisabled(context); + + testResolveModuleOptionCompletion(context); after(async () => { setFixtureAnsibleCollectionPathEnv(); await disableExecutionEnvironmentSettings(docSettings); - (await docSettings).ansible.useFullyQualifiedCollectionNames = true; }); }); @@ -266,42 +303,10 @@ describe("doCompletionResolve()", () => { before(async () => { setFixtureAnsibleCollectionPathEnv(); await disableExecutionEnvironmentSettings(docSettings); - (await docSettings).ansible.useFullyQualifiedCollectionNames = false; - }); - testFQCNDisabled(context); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - (await docSettings).ansible.useFullyQualifiedCollectionNames = true; }); - }); - }); - }); - describe("Resolve completion for module options and suboptions", () => { - describe("with EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); - }); - - testResolveModuleOptionCompletion(context); - - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + testResolveModuleOptionCompletion(context); }); }); - - describe("with EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testResolveModuleOptionCompletion(context); - }); - }); + } }); diff --git a/packages/ansible-language-server/test/providers/validationProvider.test.ts b/packages/ansible-language-server/test/providers/validationProvider.test.ts index 5c66544ff..3f5a99e34 100644 --- a/packages/ansible-language-server/test/providers/validationProvider.test.ts +++ b/packages/ansible-language-server/test/providers/validationProvider.test.ts @@ -30,13 +30,14 @@ function testValidationFromCache( function assertValidateTests( tests: testType[], - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, validationManager: ValidationManager, textDoc: TextDocument, validationEnabled: boolean, ) { tests.forEach((test) => { it(`should provide diagnostics for ${test.name}`, async function () { + expect(context).is.not.undefined; const actualDiagnostics: Map = await doValidate( textDoc, validationManager, @@ -78,7 +79,7 @@ type testType = { }; function testAnsibleLintErrors( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, validationManager: ValidationManager, textDoc: TextDocument, validationEnabled: boolean, @@ -184,7 +185,7 @@ function testAnsibleLintErrors( } function testAnsibleSyntaxCheckNoErrors( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, validationManager: ValidationManager, textDoc: TextDocument, validationEnabled: boolean, @@ -195,17 +196,20 @@ function testAnsibleSyntaxCheckNoErrors( diagnosticReport: [], }, ]; - assertValidateTests( - tests, - context, - validationManager, - textDoc, - validationEnabled, - ); + expect(context).to.not.be.undefined; + if (context) { + assertValidateTests( + tests, + context, + validationManager, + textDoc, + validationEnabled, + ); + } } function testAnsibleSyntaxCheckEmptyPlaybook( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, validationManager: ValidationManager, textDoc: TextDocument, validationEnabled: boolean, @@ -226,7 +230,7 @@ function testAnsibleSyntaxCheckEmptyPlaybook( } function testAnsibleSyntaxCheckNoHost( - context: WorkspaceFolderContext, + context: WorkspaceFolderContext | undefined, validationManager: ValidationManager, textDoc: TextDocument, validationEnabled: boolean, @@ -384,7 +388,9 @@ describe("doValidate()", () => { await enableExecutionEnvironmentSettings(docSettings); }); - testAnsibleLintErrors(context, validationManager, textDoc, true); + if (context) { + testAnsibleLintErrors(context, validationManager, textDoc, true); + } after(async () => { setFixtureAnsibleCollectionPathEnv(); @@ -454,50 +460,53 @@ describe("doValidate()", () => { context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); + expect(context).is.not.undefined; + if (context) { + docSettings = context.documentSettings.get(textDoc.uri); - describe("With EE enabled @ee", () => { - before(async () => { - (await docSettings).validation.lint.enabled = false; - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", + describe("With EE enabled @ee", () => { + before(async () => { + (await docSettings).validation.lint.enabled = false; + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckEmptyPlaybook( + context, + validationManager, + textDoc, + true, ); - await enableExecutionEnvironmentSettings(docSettings); - }); - testAnsibleSyntaxCheckEmptyPlaybook( - context, - validationManager, - textDoc, - true, - ); + after(async () => { + (await docSettings).validation.lint.enabled = true; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + }); + describe("With EE disabled", () => { + before(async () => { + (await docSettings).validation.lint.enabled = false; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckEmptyPlaybook( + context, + validationManager, + textDoc, + true, + ); + }); after(async () => { (await docSettings).validation.lint.enabled = true; setFixtureAnsibleCollectionPathEnv(); await disableExecutionEnvironmentSettings(docSettings); }); - }); - - describe("With EE disabled", () => { - before(async () => { - (await docSettings).validation.lint.enabled = false; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testAnsibleSyntaxCheckEmptyPlaybook( - context, - validationManager, - textDoc, - true, - ); - }); - after(async () => { - (await docSettings).validation.lint.enabled = true; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); + } }); describe("no host", () => { @@ -562,27 +571,55 @@ describe("doValidate()", () => { context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); + expect(context).is.not.undefined; + if (context) { + docSettings = context.documentSettings.get(textDoc.uri); - describe("With EE enabled @ee", () => { - before(async () => { - // (await docSettings).validation.lint.enabled = false; - // (await docSettings).validation.lint.path = - // "invalid-ansible-lint-path"; - (await docSettings).validation.enabled = false; - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", + describe("With EE enabled @ee", () => { + before(async () => { + // (await docSettings).validation.lint.enabled = false; + // (await docSettings).validation.lint.path = + // "invalid-ansible-lint-path"; + (await docSettings).validation.enabled = false; + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckNoErrors( + context, + validationManager, + textDoc, + false, ); - await enableExecutionEnvironmentSettings(docSettings); - }); - testAnsibleSyntaxCheckNoErrors( - context, - validationManager, - textDoc, - false, - ); + after(async () => { + // (await docSettings).validation.lint.enabled = true; + // (await docSettings).validation.lint.path = "ansible-lint"; + (await docSettings).validation.enabled = true; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + }); + describe("With EE disabled", () => { + before(async () => { + // (await docSettings).validation.lint.enabled = false; + // (await docSettings).validation.lint.path = + // "invalid-ansible-lint-path"; + (await docSettings).validation.enabled = false; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckNoErrors( + context, + validationManager, + textDoc, + false, + ); + }); after(async () => { // (await docSettings).validation.lint.enabled = true; // (await docSettings).validation.lint.path = "ansible-lint"; @@ -590,32 +627,7 @@ describe("doValidate()", () => { setFixtureAnsibleCollectionPathEnv(); await disableExecutionEnvironmentSettings(docSettings); }); - }); - - describe("With EE disabled", () => { - before(async () => { - // (await docSettings).validation.lint.enabled = false; - // (await docSettings).validation.lint.path = - // "invalid-ansible-lint-path"; - (await docSettings).validation.enabled = false; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testAnsibleSyntaxCheckNoErrors( - context, - validationManager, - textDoc, - false, - ); - }); - after(async () => { - // (await docSettings).validation.lint.enabled = true; - // (await docSettings).validation.lint.path = "ansible-lint"; - (await docSettings).validation.enabled = true; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); + } }); describe("no host", () => { @@ -624,27 +636,55 @@ describe("doValidate()", () => { context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); + expect(context).is.not.undefined; + if (context) { + docSettings = context.documentSettings.get(textDoc.uri); - describe("With EE enabled @ee", () => { - before(async () => { - // (await docSettings).validation.lint.enabled = false; - // (await docSettings).validation.lint.path = - // "invalid-ansible-lint-path"; - (await docSettings).validation.enabled = false; - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", + describe("With EE enabled @ee", () => { + before(async () => { + // (await docSettings).validation.lint.enabled = false; + // (await docSettings).validation.lint.path = + // "invalid-ansible-lint-path"; + (await docSettings).validation.enabled = false; + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckNoHost( + context, + validationManager, + textDoc, + false, ); - await enableExecutionEnvironmentSettings(docSettings); - }); - testAnsibleSyntaxCheckNoHost( - context, - validationManager, - textDoc, - false, - ); + after(async () => { + // (await docSettings).validation.lint.enabled = true; + // (await docSettings).validation.lint.path = "ansible-lint"; + (await docSettings).validation.enabled = true; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + }); + describe("With EE disabled", () => { + before(async () => { + // (await docSettings).validation.lint.enabled = false; + // (await docSettings).validation.lint.path = + // "invalid-ansible-lint-path"; + (await docSettings).validation.enabled = false; + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); + + testAnsibleSyntaxCheckNoHost( + context, + validationManager, + textDoc, + false, + ); + }); after(async () => { // (await docSettings).validation.lint.enabled = true; // (await docSettings).validation.lint.path = "ansible-lint"; @@ -652,32 +692,7 @@ describe("doValidate()", () => { setFixtureAnsibleCollectionPathEnv(); await disableExecutionEnvironmentSettings(docSettings); }); - }); - - describe("With EE disabled", () => { - before(async () => { - // (await docSettings).validation.lint.enabled = false; - // (await docSettings).validation.lint.path = - // "invalid-ansible-lint-path"; - (await docSettings).validation.enabled = false; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); - - testAnsibleSyntaxCheckNoHost( - context, - validationManager, - textDoc, - false, - ); - }); - after(async () => { - // (await docSettings).validation.lint.enabled = true; - // (await docSettings).validation.lint.path = "ansible-lint"; - (await docSettings).validation.enabled = true; - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); + } }); }); }); @@ -688,32 +703,34 @@ describe("doValidate()", () => { context = workspaceManager.getContext(fixtureFileUri); textDoc = getDoc(fixtureFilePath); - docSettings = context.documentSettings.get(textDoc.uri); - - describe("With EE enabled @ee", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv( - "/home/runner/.ansible/collections:/usr/share/ansible", - ); - await enableExecutionEnvironmentSettings(docSettings); - }); + expect(context).is.not.undefined; + if (context) { + docSettings = context.documentSettings.get(textDoc.uri); + describe("With EE enabled @ee", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv( + "/home/runner/.ansible/collections:/usr/share/ansible", + ); + await enableExecutionEnvironmentSettings(docSettings); + }); - testInvalidYamlFile(textDoc); + testInvalidYamlFile(textDoc); - after(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); + after(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); }); - }); - describe("With EE disabled", () => { - before(async () => { - setFixtureAnsibleCollectionPathEnv(); - await disableExecutionEnvironmentSettings(docSettings); - }); + describe("With EE disabled", () => { + before(async () => { + setFixtureAnsibleCollectionPathEnv(); + await disableExecutionEnvironmentSettings(docSettings); + }); - testInvalidYamlFile(textDoc); - }); + testInvalidYamlFile(textDoc); + }); + } }); } }); diff --git a/packages/ansible-language-server/test/utils/yaml.test.ts b/packages/ansible-language-server/test/utils/yaml.test.ts index 6fd9d1556..7293bd747 100644 --- a/packages/ansible-language-server/test/utils/yaml.test.ts +++ b/packages/ansible-language-server/test/utils/yaml.test.ts @@ -26,12 +26,12 @@ function getPathInFile(yamlFile: string, line: number, character: number) { } describe("yaml", () => { - beforeEach(function () { + beforeEach(function (this: Mocha.Context) { const brokenTests = new Map([ // ['', ''], ]); - const reason = brokenTests.get(this.currentTest.title); - if (isWindows() && reason) { + const reason = brokenTests.get(this.currentTest?.title); + if (isWindows() && reason && this.currentTest) { const msg = `Marked ${this.currentTest.title} as pending due to ${reason}`; if (process.env.GITHUB_ACTIONS) { console.log(`::warning file=${this.currentTest.file}:: ${msg}`); @@ -126,11 +126,13 @@ describe("yaml", () => { // otherwise empty lines; a workaround is implemented for completion // provider const path = await getPathInFile("ancestryBuilder.yml", 9, 9); - const node = new AncestryBuilder(path) - .parent(YAMLMap) - .parent(YAMLMap) - .getStringKey(); - expect(node).to.be.equal("lineinfile"); + if (path) { + const node = new AncestryBuilder(path) + .parent(YAMLMap) + .parent(YAMLMap) + .getStringKey(); + expect(node).to.be.equal("lineinfile"); + } }); it.skip("canGetIndentationParentAtEOF", async () => { @@ -390,8 +392,12 @@ describe("yaml", () => { const character = 26; const position = Position.create(line - 1, character - 1); const path = getPathInFile(file, line, character); - const test = isCursorInsideJinjaBrackets(document, position, path); - expect(test).to.be.eq(true); + if (path) { + const test = isCursorInsideJinjaBrackets(document, position, path); + expect(test).to.be.eq(true); + } else { + expect(false); + } }); it("can confirm cursor within jinja bracket in correct syntax", async () => { @@ -399,9 +405,12 @@ describe("yaml", () => { const character = 20; const position = Position.create(line - 1, character - 1); const path = getPathInFile(file, line, character); - const test = isCursorInsideJinjaBrackets(document, position, path); - - expect(test).to.be.eq(true); + if (path) { + const test = isCursorInsideJinjaBrackets(document, position, path); + expect(test).to.be.eq(true); + } else { + expect(false); + } }); it("can confirm cursor within jinja bracket in case of multiple bracket pairs", async () => { @@ -409,9 +418,12 @@ describe("yaml", () => { const character = 48; const position = Position.create(line - 1, character - 1); const path = getPathInFile(file, line, character); - const test = isCursorInsideJinjaBrackets(document, position, path); - - expect(test).to.be.eq(true); + if (path) { + const test = isCursorInsideJinjaBrackets(document, position, path); + expect(test).to.be.eq(true); + } else { + expect(false); + } }); it("can confirm cursor within jinja bracket even if text already present inside it", async () => { @@ -419,9 +431,12 @@ describe("yaml", () => { const character = 36; const position = Position.create(line - 1, character - 1); const path = getPathInFile(file, line, character); - const test = isCursorInsideJinjaBrackets(document, position, path); - - expect(test).to.be.eq(true); + if (path) { + const test = isCursorInsideJinjaBrackets(document, position, path); + expect(test).to.be.eq(true); + } else { + expect(false); + } }); it("can negate cursor outside jinja bracket", async () => {