Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: test uninstall command #78

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")}`
);
});
});
Loading