Skip to content

Commit

Permalink
test: test uninstall command
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Mar 2, 2024
1 parent 27957c7 commit 61df946
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 32 deletions.
41 changes: 41 additions & 0 deletions src/commands/uninstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as vscode from "vscode";
import * as fsp from "fs/promises";
import * as path from "path";
import * as os from "os";

export const run = async (cacheDir: string) => {
if (cacheDir[0] === "~") {
cacheDir = path.join(os.homedir(), cacheDir.slice(1));
}
const bin = path.join(cacheDir, "nextls");
await fsp
.rm(bin)
.then(
async () =>
await vscode.window.showInformationMessage(
`Uninstalled Next LS from ${bin}`
)
)
.catch(
async (error) =>
await vscode.window.showErrorMessage(
`Failed to uninstall Next LS from ${bin} due to ${error}`
)
);
};

const registerUninstallCommand = (
config: vscode.WorkspaceConfiguration,
context: vscode.ExtensionContext
) => {
const uninstallCommand = "elixir-tools.uninstall-nextls";

const uninstall = async () =>
run(<string>config.get("installationDirectory"));

context.subscriptions.push(
vscode.commands.registerCommand(uninstallCommand, uninstall)
);
};

export default registerUninstallCommand;
30 changes: 3 additions & 27 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
StreamInfo,
} from "vscode-languageclient/node";

import registerUninstallCommand from "./commands/uninstall";

let credoClient: LanguageClient;
let nextLSClient: LanguageClient;

Expand Down Expand Up @@ -99,33 +101,7 @@ async function activateNextLS(
) {
let config = vscode.workspace.getConfiguration("elixir-tools.nextLS");

const command = "elixir-tools.uninstall-nextls";

const uninstallNextLS = async () => {
let cacheDir: string = config.get("installationDirectory")!;
if (cacheDir[0] === "~") {
cacheDir = path.join(os.homedir(), cacheDir.slice(1));
}
const bin = path.join(cacheDir, "nextls");
await fsp
.rm(bin)
.then(
async () =>
await vscode.window.showInformationMessage(
`Uninstalled Next LS from ${bin}`
)
)
.catch(
async () =>
await vscode.window.showErrorMessage(
`Failed to uninstall Next LS from ${bin}`
)
);
};

context.subscriptions.push(
vscode.commands.registerCommand(command, uninstallNextLS)
);
registerUninstallCommand(config, context);

if (config.get("enable")) {
let serverOptions: ServerOptions;
Expand Down
26 changes: 21 additions & 5 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import * as path from "path";
// as well as import your extension to test it
import * as vscode from "vscode";
import * as myExtension from "../../extension.js";
import * as uninstall from "../../commands/uninstall.js";
import * as sinon from "sinon";

suite("Extension Test Suite", () => {
vscode.window.showInformationMessage("Start all tests.");
let showInformationMessage;

setup(function () {
fs.rmSync("./test-bin", { recursive: true, force: true });
sinon.stub(vscode.window, "showInformationMessage").returns(
new Promise((resolve) => {
return resolve({ title: "Yes" });
})
);
showInformationMessage = sinon
.stub(vscode.window, "showInformationMessage")
.returns(
new Promise((resolve) => {
return resolve({ title: "Yes" });
})
);
});

teardown(function () {
Expand All @@ -30,4 +34,16 @@ suite("Extension Test Suite", () => {
let result = await myExtension.ensureNextLSDownloaded("test-bin");
assert.equal(path.normalize(result), path.normalize("test-bin/nextls"));
});

test("uninstalls Next LS", async function () {
fs.mkdirSync("./test-bin", { recursive: true });
fs.writeFileSync("./test-bin/nextls", "hello word");

await uninstall.run("./test-bin");

assert.equal(
showInformationMessage.getCall(0).args[0],
`Uninstalled Next LS from ${path.normalize("test-bin/nextls")}`
);
});
});

0 comments on commit 61df946

Please sign in to comment.