Skip to content

Commit

Permalink
Added linter check to CICD
Browse files Browse the repository at this point in the history
Added one unit test for custom.eval
  • Loading branch information
dpar39 committed Dec 2, 2024
1 parent aa3bec4 commit d95cfb9
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 191 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
- name: Install npm dependencies
run: npm install

- name: Run compile, formatting and linting checks
run: npm run pretest

- name: Build and run automation tests
run: npm run test

Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## [0.0.15]

- Added custom error message when registering an external formatter via `custom.registerExternalFormatter`.
- Improved unit tests and linter checks (added code formatting check)

## [0.0.14]

- Added `custom.registerExternalFormatter` to support external formatters that send code snippets to a HTTP endpoint.
Expand Down
260 changes: 101 additions & 159 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "This extension allows you to remotely control Visual Studio Code via a REST endpoint, taking automation to the next level.",
"publisher": "dpar39",
"license": "MIT",
"version": "0.0.14",
"version": "0.0.15",
"engines": {
"vscode": "^1.55.0"
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"test-compile": "tsc -p ./",
"test-watch": "tsc -watch -p ./",
"pretest": "npm run test-compile && npm run lint",
"lint": "eslint src --ext ts",
"lint": "eslint src --ext ts && npx prettier --check './**/*.{js,ts}'",
"test": "xvfb-run -a node ./out/test/runTest.js",
"format": "npx prettier --write './**/*.{js,jsx,mjs,cjs,ts,tsx}'"
},
Expand Down
47 changes: 23 additions & 24 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,51 +60,50 @@ const startHttpServer = async (
}
}

const endBadRequest = (err: any, res: ServerResponse) => {
res.statusCode = 400;
const errStringJson = JSON.stringify(err, Object.getOwnPropertyNames(err));
const sendResponse = (res: ServerResponse, body: string, statusCode: number = 200) => {
res.statusCode = statusCode;
res.setHeader("Access-Control-Allow-Origin", "*");
res.write(errStringJson);
res.setHeader("Content-Type", "application/json");
res.write(body);
res.end();
};

const endBadRequest = (err: any, res: ServerResponse) => {
const errStringJson = JSON.stringify(err, Object.getOwnPropertyNames(err));
sendResponse(res, errStringJson, 400);
Logger.error(errStringJson);
};

const processRequest = (cmd: string, args: string[], res: ServerResponse) => {
processRemoteControlRequest(cmd, args)
.then((data) => {
res.setHeader("Content-Type", "application/json");
res.setHeader("Access-Control-Allow-Origin", "*");
res.write(JSON.stringify(data || null));
res.end();
})
.then((data) => sendResponse(res, JSON.stringify(data || null)))
.catch((err) => endBadRequest(err, res));
};

const requestHandler = (req: IncomingMessage, res: ServerResponse) => {
let body = "";
let controlCommand: any = {};
let command: string | null;
let args: any[] = [];
if (req.url && req.url.indexOf("?") >= 0) {
const url = new URL(req.url, `http://${req.headers.host}/`);
const queryParams = new URLSearchParams(url.search);
try {
const cmd = queryParams.get("command");
const args = queryParams.has("args")
? JSON.parse(decodeURIComponent(queryParams.get("args")!))
: [];
Logger.info(`Remote request command=${cmd}, args=${args}`);
processRequest(cmd!, args, res);
} catch (err) {
endBadRequest(err, res);
command = queryParams.get("command");
if (queryParams.has("args")) {
args = JSON.parse(decodeURIComponent(queryParams.get("args")!));
}
}

let body = "";
req.on("data", (chunk) => {
body += chunk;
});
req.on("end", () => {
Logger.info(`Remote request payload: ${body}`);
const reqData = body ? JSON.parse(body) : controlCommand;
processRequest(reqData.command, reqData.args || [], res);
if (body) {
const reqData = JSON.parse(body);
command = reqData.command || command;
args = reqData.args || args;
}
Logger.info(`Remote Control request with command=${command}, args=${args}`);
processRequest(command!, args, res);
});
};
// Start the HTTP server
Expand Down
2 changes: 2 additions & 0 deletions src/services/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export async function registerExternalFormatter(
path: url.pathname,
method: httpMethod,
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
"Content-Type": "application/json",
// eslint-disable-next-line @typescript-eslint/naming-convention
"Content-Length": Buffer.byteLength(payload),
},
};
Expand Down
5 changes: 2 additions & 3 deletions src/services/requestProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export async function processRemoteControlRequest(command: string, args: any[]):
}

if (command === "custom.eval") {
eval(args[0]);
return;
return eval(args[0]);
}

if (command === "custom.listInstalledExtensions") {
Expand Down Expand Up @@ -125,7 +124,7 @@ export async function processRemoteControlRequest(command: string, args: any[]):
}
let uri = null;
if (!path.isAbsolute(filePath)) {
let candidates = await vscode.workspace.findFiles(args[0]);
let candidates = await vscode.workspace.findFiles(filePath);
if (candidates.length === 1) {
uri = candidates[0];
}
Expand Down
20 changes: 17 additions & 3 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as assert from "assert";
import { EXTENSION_ID } from "../../extension";
import * as fs from "fs";

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
Expand All @@ -18,9 +19,9 @@ suite("Extension Test Suite", () => {
}).timeout(5000);

test("get workspace folders", async () => {
const workspaceFolders = (await makeRequest("custom.workspaceFolders")) as string[];
const workspaceFolders = (await makeRequest("custom.workspaceFolders")) as any[];
assert(workspaceFolders.length === 1);
const ws = workspaceFolders[0] as any;
const ws = workspaceFolders[0];
assert(ws.name === "workspace1");
assert(ws.index === 0);
assert(ws.uri.startsWith("file://"));
Expand All @@ -35,8 +36,21 @@ suite("Extension Test Suite", () => {
assert(workspaceFile === null); // no workspace file
});

test("get all commands registred in vscode", async () => {
test("get all commands registered in vscode", async () => {
const commands: string[] = (await makeRequest("custom.getCommands")) as string[];
assert(commands.length > 100);
});

test("test can open document and get its content", async () => {
const xx = await makeRequest("custom.goToFileLineCharacter", ["demo.py:17:28"]);
const content: string = (await makeRequest("custom.eval", [
"vscode.window.activeTextEditor?.document.getText()",
])) as string;
const workspaceFolders = (await makeRequest("custom.workspaceFolders")) as any[];
const workspaceAbsPath = workspaceFolders[0].uri.slice("file://".length);
const expectedContent = fs.readFileSync(workspaceAbsPath + "/demo.py", {
encoding: "utf-8",
});
assert(content === expectedContent);
});
});

0 comments on commit d95cfb9

Please sign in to comment.