Skip to content

Commit

Permalink
More types
Browse files Browse the repository at this point in the history
Related: #1225
  • Loading branch information
ssbarnea committed Apr 24, 2024
1 parent 1c73bee commit 145dd90
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 119 deletions.
17 changes: 9 additions & 8 deletions packages/ansible-language-server/src/services/ansiblePlaybook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,15 @@ export class AnsiblePlaybook {
execError.stderr,
);

diagnostics = filteredErrorMessage
? this.processReport(
execError.message,
filteredErrorMessage.groups.filename,
parseInt(filteredErrorMessage.groups.line),
parseInt(filteredErrorMessage.groups.column),
)
: this.processReport(execError.message, docPath, 1, 1);
diagnostics =
filteredErrorMessage && filteredErrorMessage.groups
? this.processReport(
execError.message,
filteredErrorMessage.groups.filename,
parseInt(filteredErrorMessage.groups.line),
parseInt(filteredErrorMessage.groups.column),
)
: this.processReport(execError.message, docPath, 1, 1);

if (execError.stderr) {
this.connection.console.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import { IVolumeMounts } from "../interfaces/extensionSettings";

export class ExecutionEnvironment {
public isServiceInitialized: boolean = false;
private settings: ExtensionSettings;
private settings: ExtensionSettings | undefined = undefined;
private connection: Connection;
private context: WorkspaceFolderContext;
private useProgressTracker = false;
private successFileMarker = "SUCCESS";
private settingsVolumeMounts: string[] = [];
private settingsContainerOptions: string;
private _container_engine: IContainerEngine;
private _container_image: string;
private _container_image_id: string;
private _container_volume_mounts: Array<IVolumeMounts>;
private settingsContainerOptions: string | undefined = undefined;
private _container_engine: IContainerEngine | undefined = undefined;
private _container_image: string | undefined = undefined;
private _container_image_id: string | undefined = undefined;
private _container_volume_mounts: Array<IVolumeMounts> | undefined =
undefined;

constructor(connection: Connection, context: WorkspaceFolderContext) {
this.connection = connection;
Expand Down Expand Up @@ -78,7 +79,7 @@ export class ExecutionEnvironment {
}

public async fetchPluginDocs(ansibleConfig: AnsibleConfig): Promise<void> {
if (!this.isServiceInitialized) {
if (!this.isServiceInitialized || !this._container_image) {
this.connection.console.error(
`ExecutionEnvironment service not correctly initialized. Failed to fetch plugin docs`,
);
Expand Down Expand Up @@ -196,7 +197,11 @@ export class ExecutionEnvironment {
command: string,
mountPaths?: Set<string>,
): string | undefined {
if (!this.isServiceInitialized) {
if (
!this.isServiceInitialized ||
!this._container_engine ||
!this._container_image
) {
this.connection.console.error(
"ExecutionEnvironment service not correctly initialized.",
);
Expand Down Expand Up @@ -257,7 +262,9 @@ export class ExecutionEnvironment {
// docker does not support this option
containerCommand.push("--quiet");
} else {
containerCommand.push(`--user=${process.getuid()}`);
if (process.getuid) {
containerCommand.push(`--user=${process.getuid()}`);
}
}

// handle container options setting from client
Expand All @@ -284,6 +291,12 @@ export class ExecutionEnvironment {
}

private async pullContainerImage(): Promise<boolean> {
if (!this._container_engine || !this._container_image || !this.settings) {
this.connection.window.showErrorMessage(
"Execution environment not properly initialized.",
);
return false;
}
const imagePuller = new ImagePuller(
this.connection,
this.context,
Expand All @@ -304,6 +317,13 @@ export class ExecutionEnvironment {
}

private setContainerEngine(): boolean {
if (!this._container_engine) {
this.connection.window.showErrorMessage(
"Unable to setContainerEnginer with incompletly initialized settings.",
);
return false;
}

if (this._container_engine === "auto") {
for (const ce of ["podman", "docker"]) {
try {
Expand Down Expand Up @@ -422,7 +442,13 @@ export class ExecutionEnvironment {
.trim();
return result.trim() !== "";
} catch (error) {
this.connection.console.error(error);
let message: string;
if (error instanceof Error) {
message = error.message;
} else {
message = JSON.stringify(error);
}
this.connection.console.error(message);
return false;
}
}
Expand Down
10 changes: 5 additions & 5 deletions packages/ansible-language-server/test/consoleOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* by modifying their abilities and redirects them to suppress and release them appropriately
*/
export class ConsoleOutput {
private logOutput: string;
private debugOutput: string;
private infoOutput: string;
private warnOutput: string;
private errorOutput: string;
private logOutput: string = "";
private debugOutput: string = "";
private infoOutput: string = "";
private warnOutput: string = "";
private errorOutput: string = "";

private originalConsoleLog = console.log;
private originalConsoleDebug = console.debug;
Expand Down
4 changes: 3 additions & 1 deletion packages/ansible-language-server/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export function smartFilter(
}

// Sort completion list based on `sortText` property of the completion item
completionList.sort((a, b) => a.sortText.localeCompare(b.sortText));
completionList.sort((a: CompletionItem, b: CompletionItem) =>
a.sortText && b.sortText ? a.sortText.localeCompare(b?.sortText) : 0,
);

// Construct a new Fuse object to do fuzzy search with key as `filterText` property of the completion item
const searcher = new Fuse(completionList, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ function testModuleNamesForDefinition(
}

expect(actualDefinition).to.have.length(1);
if (actualDefinition) {
const definition = actualDefinition[0];
// file uri check
expect(definition.targetUri.startsWith("file:///")).to.be.true;
expect(definition.targetUri).satisfy((fileUri: string) =>
fileExists(URI.parse(fileUri).path),
);

const definition = actualDefinition[0];

// file uri check
expect(definition.targetUri.startsWith("file:///")).to.be.true;
expect(definition.targetUri).satisfy((fileUri: string) =>
fileExists(URI.parse(fileUri).path),
);

// nodule name range check in the playbook
expect(definition.originSelectionRange).to.deep.equal(selectionRange);
// nodule name range check in the playbook
expect(definition.originSelectionRange).to.deep.equal(selectionRange);

// original document range checks
expect(definition).to.haveOwnProperty("targetRange");
expect(definition).to.haveOwnProperty("targetSelectionRange");
// original document range checks
expect(definition).to.haveOwnProperty("targetRange");
expect(definition).to.haveOwnProperty("targetSelectionRange");
}
});
});
}
Expand All @@ -88,32 +88,41 @@ describe("getDefinition()", function () {
const context = workspaceManager.getContext(fixtureFileUri);

const textDoc = getDoc(fixtureFilePath);
const docSettings = context.documentSettings.get(textDoc.uri);
const docSettings = context?.documentSettings.get(textDoc.uri);

describe("Module name definitions", function () {
describe("With EE enabled @ee", function () {
before(async function () {
setFixtureAnsibleCollectionPathEnv(
"/home/runner/.ansible/collections:/usr/share/ansible",
);
await enableExecutionEnvironmentSettings(docSettings);
if (docSettings) {
await enableExecutionEnvironmentSettings(docSettings);
}
});

testModuleNamesForDefinition(context, textDoc);
if (context) {
testModuleNamesForDefinition(context, textDoc);
}

after(async function () {
setFixtureAnsibleCollectionPathEnv();
await disableExecutionEnvironmentSettings(docSettings);
if (docSettings) {
await disableExecutionEnvironmentSettings(docSettings);
}
});
});

describe("With EE disabled", function () {
before(async function () {
setFixtureAnsibleCollectionPathEnv();
await disableExecutionEnvironmentSettings(docSettings);
if (docSettings) {
await disableExecutionEnvironmentSettings(docSettings);
}
});

testModuleNamesForDefinition(context, textDoc);
if (context) {
testModuleNamesForDefinition(context, textDoc);
}
});
});
});
Loading

0 comments on commit 145dd90

Please sign in to comment.