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 3ea982f
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type IContainerEngine = "auto" | "podman" | "docker";

export type IPullPolicy = "always" | "missing" | "never" | "tag";

interface ExtensionSettingsWithDescriptionBase {
export interface ExtensionSettingsWithDescriptionBase {
[key: string]: SettingsEntry | string | boolean;
}

Expand Down
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
14 changes: 10 additions & 4 deletions packages/ansible-language-server/src/services/settingsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../interfaces/extensionSettings";

export class SettingsManager {
private connection: Connection;
private connection: Connection | null;
private clientSupportsConfigRequests;
private configurationChangeHandlers: Map<string, { (): void }> = new Map();

Expand Down Expand Up @@ -135,7 +135,10 @@ export class SettingsManager {

public globalSettings: ExtensionSettings = this.defaultSettings;

constructor(connection: Connection, clientSupportsConfigRequests: boolean) {
constructor(
connection: Connection | null,
clientSupportsConfigRequests: boolean,
) {
this.connection = connection;
this.clientSupportsConfigRequests = clientSupportsConfigRequests;
}
Expand All @@ -155,7 +158,7 @@ export class SettingsManager {
return Promise.resolve(this.globalSettings);
}
let result = this.documentSettings.get(uri);
if (!result) {
if (!result && this.connection) {
const clientSettings = await this.connection.workspace.getConfiguration({
scopeUri: uri,
section: "ansible",
Expand All @@ -167,6 +170,9 @@ export class SettingsManager {
result = Promise.resolve(mergedSettings);
this.documentSettings.set(uri, result);
}
if (!result) {
return {} as ExtensionSettings;
}
return result;
}

Expand All @@ -188,7 +194,7 @@ export class SettingsManager {

for (const [uri, handler] of this.configurationChangeHandlers) {
const config = await this.documentSettings.get(uri);
if (config) {
if (config && this.connection) {
// found cached values, now compare to the new ones

const newConfigPromise = this.connection.workspace.getConfiguration({
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 3ea982f

Please sign in to comment.