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

New release with more features #2

Merged
merged 3 commits into from
May 20, 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
7 changes: 3 additions & 4 deletions .github/workflows/release.yml → .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: Release
name: CI/CD pipeline
on:
pull_request:
types: [synchronize]
push:
branches:
- main
workflow_dispatch:

jobs:
build:
name: "Build and release"
name: "Build, test and release"
runs-on: ubuntu-latest

steps:
Expand All @@ -23,7 +22,7 @@ jobs:
run: npm install

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

- name: Publish to VSCode Marketplace
if: ${{ github.ref == 'refs/heads/main' }}
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"type": "extensionHost",
"request": "launch",
"args": [
"${workspaceFolder}/sampleWorkspace",
"${workspaceFolder}/src/test/workspace1",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [0.0.5]

- Added `custom.workspaceFile` and `custom.workspaceFolders` to retrieve the workspace file (if any) and folders currently opened in the workspace (if any)

- Added `custom.getCommands` to retrieve the full list of commands registered within vscode

## [0.0.4]

- Removed npm vulnerabilities
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.4",
"version": "0.0.5",
"engines": {
"vscode": "^1.55.0"
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"test-watch": "tsc -watch -p ./",
"pretest": "npm run test-compile && npm run lint",
"lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js"
"test": "xvfb-run -a node ./out/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^7.1.6",
Expand Down
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const startHttpServer = async (
host: string,
port: number,
fallbackPorts: number[],
showNotification: boolean = false
): Promise<void> => {
let isInUse = false;
if (port) {
Expand All @@ -39,7 +38,7 @@ const startHttpServer = async (
if (fallbackPorts.length > 0) {
const nextPort = fallbackPorts.shift();
if (nextPort) {
startHttpServer(context, host, nextPort, fallbackPorts, true);
startHttpServer(context, host, nextPort, fallbackPorts);
return;
} else {
isInUse = true;
Expand All @@ -61,7 +60,7 @@ const startHttpServer = async (
processRemoteControlRequest(reqData as ControlRequest)
.then((data) => {
res.setHeader("Content-Type", "application/json");
res.write(JSON.stringify(data || {}));
res.write(JSON.stringify(data || null));
res.end();
})
.catch((err) => {
Expand Down
18 changes: 18 additions & 0 deletions src/services/requestProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ export async function processRemoteControlRequest(requestObject: ControlRequest)
return vscode.extensions.all.map((e) => e.id);
}

if (command === "custom.workspaceFile") {
return vscode.workspace.workspaceFile?.toString();
}

if (command === "custom.getCommands") {
return await vscode.commands.getCommands();
}

if (command === "custom.workspaceFolders") {
return vscode.workspace.workspaceFolders?.map(ws => {
return {
name: ws.name,
index: ws.index,
uri: ws.uri.toString(),
};
});
}

if (command === "custom.showInformationMessage") {
return await vscode.window.showInformationMessage(args[0], ...args.slice(1));
}
Expand Down
7 changes: 6 additions & 1 deletion src/test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ async function main() {
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, "./suite/index");

// Get the directory path to a sample workspace
const workspace1 = path.resolve(extensionDevelopmentPath, "src/test/workspace1");

// VSCode launch arguments
const launchArgs = [workspace1];
// Download VS Code, unzip it and run the integration test
await runTests({ extensionDevelopmentPath, extensionTestsPath});
await runTests({ extensionDevelopmentPath, extensionTestsPath, launchArgs });
} catch (err) {
console.error("Failed to run tests");
process.exit(1);
Expand Down
28 changes: 22 additions & 6 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from "assert";
import { EXTENSION_ID, getListeningPort } from "../../extension";
import { EXTENSION_ID } from "../../extension";

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
Expand All @@ -8,13 +8,29 @@ import { makeRequest } from "./sendPostRequest";
suite("Extension Test Suite", () => {
suiteSetup(async () => { });

suiteTeardown(() => { });
suiteTeardown(async () => { });

test("check can list extensions", async () => {
const extensionIds: string[] = (await makeRequest(
"custom.listInstalledExtensions",
[], getListeningPort()
)) as string[];
const extensionIds: string[] = (await makeRequest("custom.listInstalledExtensions")) as string[];
assert(extensionIds.includes(EXTENSION_ID));
}).timeout(5000);

test("get workspace folders", async () => {
const workspaceFolders = (await makeRequest("custom.workspaceFolders")) as string[];
assert(workspaceFolders.length === 1);
const ws = workspaceFolders[0] as any;
assert(ws.name === 'workspace1');
assert(ws.index === 0);
assert(ws.uri.startsWith("file://"));
assert(ws.uri.endsWith("/workspace1"));

const workspaceFile = await makeRequest("custom.workspaceFile") as any;
assert(workspaceFile === null); // no workspace file
});

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

});
5 changes: 3 additions & 2 deletions src/test/suite/sendPostRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as http from "http";
import { getListeningPort } from "../../extension";

export async function makeRequest(command: string, args: any[], port: number = 37100) {
export async function makeRequest(command: string, args: any[] = [], port: number = 0) {
return new Promise((resolve, reject) => {
const req = http.request(
{
method: "POST",
hostname: "localhost",
port: port,
port: port || getListeningPort(),
path: "/",
},
(res) => {
Expand Down
4 changes: 3 additions & 1 deletion sampleWorkspace/demo.py → src/test/workspace1/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from time import sleep


def post_data(data, url="http://localhost:37100"):
def post_data(data):
port = os.environ.get("REMOTE_CONTROL_PORT", "37100")
url = f"http://localhost:{port}"
body = json.dumps(data).encode("utf-8")
headers = {"Content-Type": "application/json"}
req = request.Request(url, body, headers)
Expand Down
73 changes: 57 additions & 16 deletions sampleWorkspace/samples.http → src/test/workspace1/samples.http
Original file line number Diff line number Diff line change
@@ -1,31 +1,45 @@
### send a message to vscode
POST http://localhost:37100 HTTP/1.1
@endpoint = http://localhost:37100

### show a warning message in vscode and get a choice selection
POST {{endpoint}} HTTP/1.1
content-type: application/json

{"command": "custom.showErrorMessage", "args": ["Make a choice", "Choice 1", "Choice 2"]}
{
"command": "custom.showWarningMessage",
"args": ["Make a choice", "Choice 1", "Choice 2"]
}

### list installed extensions
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{ "command": "custom.listInstalledExtensions" }

### get current workspace
POST {{endpoint}} HTTP/1.1
content-type: application/json

{"command": "custom.listInstalledExtensions", "args": []}
{ "command": "custom.workspaceFolders" }

### execute command in terminal
POST http://localhost:37100 HTTP/1.1
### execute a command in active terminal
POST {{endpoint}} HTTP/1.1
content-type: application/json

{"command": "custom.runInTerminal", "args": "ls -la"}
{ "command": "custom.runInTerminal", "args": ["ls -la"] }


### Open file at line
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{"command": "custom.goToFileLineCharacter", "args": ["demo.py", 5, 0]}
{
"command": "custom.goToFileLineCharacter",
"args": ["src/test/workspace1/demo.py", 5, 0]
}


### Start debugging with a custom configuration
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
Expand All @@ -44,7 +58,7 @@ content-type: application/json
}

### Reveal line in current file
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
Expand All @@ -53,7 +67,7 @@ content-type: application/json
}

### Go to location (the hard way)
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
Expand All @@ -62,7 +76,7 @@ content-type: application/json
{
"__type__": "Uri",
"args": [
"/home/ddiaz/vscode-rest-control/src/demo.py"
"/abs/path/to/src/test/workspace1/demo.py"
]
},
{
Expand Down Expand Up @@ -90,7 +104,7 @@ content-type: application/json
}

### Quick pick between 3 choices
POST http://localhost:37100 HTTP/1.1
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
Expand All @@ -115,4 +129,31 @@ content-type: application/json
}
]
}]
}
}

### install an extension
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
"command": "workbench.extensions.installExtension",
"args": ["dpar39.vscode-taef-test-adapter"]
}

### uninstall an extension
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
"command": "workbench.extensions.uninstallExtension",
"args": ["dpar39.vscode-taef-test-adapter"]
}

### developer: reload window
POST {{endpoint}} HTTP/1.1
content-type: application/json

{
"command": "workbench.action.reloadWindow",
"args": []
}
Loading