Skip to content

Commit

Permalink
feat: add acceptance test for running language server integrated in C…
Browse files Browse the repository at this point in the history
…LI [HEAD-687] (#4843)

* feat: add acceptance test for language server extension

Co-authored-by: Peter Schäfer <peter.schafer@snyk.io>

* fix: actually check for diagnostics

(it was early in the morning)

---------

Co-authored-by: Peter Schäfer <peter.schafer@snyk.io>
  • Loading branch information
bastiandoetsch and PeterSchafer committed Sep 8, 2023
1 parent 18895a9 commit 168ea03
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Makefile
Expand Up @@ -255,3 +255,8 @@ release-pre:
@echo "-- Publishing to S3 /version"
@./release-scripts/upload-artifacts.sh version

.PHONY: format
format:
@echo "-- Formatting code"
@npm run format
@pushd cliv2; $(MAKE) format; popd
16 changes: 16 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -174,6 +174,7 @@
"ts-loader": "^9.0.2",
"ts-node": "^8.0.0",
"typescript": "^3.9.9",
"vscode-jsonrpc": "8.1.0",
"webpack": "^5.54.0",
"webpack-cli": "^4.6.0",
"webpack-license-plugin": "^4.2.0",
Expand Down
112 changes: 112 additions & 0 deletions test/jest/acceptance/language-server-extension.spec.ts
@@ -0,0 +1,112 @@
import { runSnykCLI } from '../util/runSnykCLI';
import { pathToFileURL } from 'url';
import { sleep } from '../../../src/lib/common';
import * as cp from 'child_process';
import * as rpc from 'vscode-jsonrpc/node';

jest.setTimeout(1000 * 60);

describe('Language Server Extension', () => {
it('get ls licenses', async () => {
const result = await runSnykCLI('language-server --licenses -d');
if (result.code != 0) {
console.debug(result.stderr);
console.debug(result.stdout);
}
expect(result.code).toBe(0);
});

it('get ls version', async () => {
const cliResult = await runSnykCLI('-v');
const result = await runSnykCLI('language-server -v -d');
if (result.code != 0) {
console.debug(result.stderr);
console.debug(result.stdout);
}
expect(result.code).toBe(0);
expect(cliResult.code).toBe(0);
expect(result.stdout).not.toEqual(cliResult.stdout);
});

it('run and wait for diagnostics', async () => {
let cmd = '';
if (process.env.TEST_SNYK_COMMAND !== undefined) {
cmd = process.env.TEST_SNYK_COMMAND;
}

const cli = cp.spawn(cmd, ['language-server'], { stdio: 'pipe' }); // Use stdin and stdout for communication:

const connection = rpc.createMessageConnection(
new rpc.StreamMessageReader(cli.stdout),
new rpc.StreamMessageWriter(cli.stdin),
);

await sleep(3000);

// create an RPC endpoint for the process
connection.listen();

await connection.sendRequest('initialize', {
processId: process.pid,
capabilities: {
window: {
workDoneProgress: true,
},
},
clientInfo: {
name: 'FakeIDE',
version: '4.5.6',
},
workspaceFolders: [
{
name: 'workspace',
uri: pathToFileURL('.').href,
},
],
rootUri: null,
initializationOptions: {
activateSnykCodeSecurity: 'false',
activateSnykCodeQuality: 'false',
activateSnykOpenSource: 'true',
activateSnykIac: 'false',
token: process.env.TEST_SNYK_TOKEN,
manageBinariesAutomatically: 'false',
enableTrustedFoldersFeature: 'false',
integrationName: 'MyFakePlugin',
integrationVersion: '1.2.3',
enableTelemetry: 'false',
cliPath: cmd,
},
});

let diagnosticCount = 0;
connection.onNotification(
'textDocument/publishDiagnostics',
(param: string) => {
console.debug('Received notification: ' + param);
diagnosticCount++;
},
);

// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
connection.onNotification('window/logMessage', (_: string) => {});

// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
connection.onNotification((_: string) => {});

// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
connection.onRequest((_: string) => {});

await connection.sendRequest('initialized', {});

for (let i = 0; i < 45; i++) {
console.debug('Waiting for diagnostics...');
if (diagnosticCount > 0) {
break;
}
await sleep(1000);
}

cli.kill(9);
});
});
9 changes: 9 additions & 0 deletions test/setup.js
Expand Up @@ -13,6 +13,11 @@ module.exports = async function() {
process.env.TEST_SNYK_COMMAND = getCliBinaryPath();
}

let token = 'UNSET';
if (process.env.TEST_SNYK_TOKEN !== undefined) {
token = '***';
}

console.info(
'\n------------------------------------------------------------------------------------------------------' +
'\n Binary under test [TEST_SNYK_COMMAND] .............. ' +
Expand All @@ -21,6 +26,10 @@ module.exports = async function() {
!isDontSkipTestsEnabled() +
'\n Run FIPS tests [TEST_SNYK_FIPS] ................. ' +
fipsTestsEnabled() +
'\n Organization [TEST_SNYK_ORG_SLUGNAME] ......... ' +
process.env.TEST_SNYK_ORG_SLUGNAME +
'\n Token [TEST_SNYK_TOKEN] ................ ' +
token +
'\n------------------------------------------------------------------------------------------------------',
);

Expand Down

0 comments on commit 168ea03

Please sign in to comment.