Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Adapt plugin to use new language test API (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: svor <vsvydenk@redhat.com>

* Adapt plugin to use new API
  • Loading branch information
svor committed Sep 8, 2020
1 parent 2d535ea commit ec40ea6
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 137 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,3 @@ In order to do this you'll need a few things:

The idea is that when you start a workspace with your devfile the testWorkspace will be the root of the application
and the tests will be loaded into mocha and executed against the current running workspace.

#### Possible Improvements
Currently because of https://github.com/eclipse/che/issues/15826 we are restricted on some tests we could make. For instance,
once this feature resolves we can activate the extension from inside the tests and then check whether
[vscode-java extension api](https://github.com/redhat-developer/vscode-java/blob/master/src/extension.api.ts#L14) is set to
"Started" or "Error".
Binary file modified che_java_test_theia_plugin.theia
Binary file not shown.
9 changes: 2 additions & 7 deletions devfile.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
apiVersion: 1.0.0
metadata:
name: che-java-sample-tests
projects:
- name: che-java-test-theia-plugin
source:
location: 'https://github.com/jpinkney/che-java-test-theia-plugin.git'
type: git
components:
- id: redhat/java8/latest
- id: redhat/java11/latest
type: chePlugin
- reference: https://raw.githubusercontent.com/JPinkney/Che-Java-Tests/master/meta.yaml
- reference: https://raw.githubusercontent.com/che-incubator/che-java-tests/master/meta.yaml
type: chePlugin
- type: cheEditor
id: eclipse/che-theia/next
Expand Down
7 changes: 4 additions & 3 deletions meta.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

apiVersion: v2
publisher: Red Hat
name: Java-Smoke-Test-Plugin
Expand All @@ -7,9 +8,9 @@ displayName: Java Smoke Test Plugin
title: Java-Smoke-Test-Plugin
description: This plug-in runs Java tests against vscode-java inside of your workspace
icon: https://www.eclipse.org/che/images/logo-eclipseche.svg
repository: https://github.com/jpinkney/Che-Java-Tests
repository: https://github.com/che-incubator/che-java-tests
category: Language
firstPublicationDate: "2020-01-07"
firstPublicationDate: "2020-08-20"
spec:
extensions:
- https://github.com/JPinkney/Che-Java-Tests/raw/master/che_java_test_theia_plugin.theia
- https://github.com/che-incubator/che-java-tests/raw/master/che_java_test_theia_plugin.theia
63 changes: 41 additions & 22 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -25,33 +25,52 @@ export function start(context: theia.PluginContext): void {
mocha.useColors(true);

const e = (c: any) => console.log(c);

ncp(context.extensionPath, '/projects/Che-Java-Tests', (err: any) => {
ncp(context.extensionPath, '/projects/Che-Java-Tests', async (err: any) => {
if (err) {
return console.error(err);
}

theia.workspace.findFiles('**/tests/*.test.ts', undefined).then(files => {
console.log("Found: ");
console.log(files);

// Add files to the test suite
files.forEach(f => mocha.addFile(path.resolve(f.path)));

try {
// Run the mocha test
mocha.run((failures: any) => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
}
});
} catch (err) {
e(err);
}
});
});
await activateJavaLSPlugin();

const testFiles = await theia.workspace.findFiles('**/tests/*.test.ts', undefined)
console.log("Found: ");
console.log(testFiles);

// Add files to the test suite
testFiles.forEach(f => mocha.addFile(path.resolve(f.path)));

try {
// Run the mocha test
mocha.run((failures: any) => {
theia.window.showInformationMessage('Tests completed! See results in test.log file');
const resultFile = path.resolve('/projects', 'test.log');
theia.commands.executeCommand('file-search.openFile', resultFile)
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
}
});
} catch (err) {
e(err);
}
});
}

async function activateJavaLSPlugin(): Promise<void> {
const files = await theia.workspace.findFiles('MyHelloText.java', undefined, 1);
if (files.length != 1) {
throw new Error('Cannot find java file');
}
const file = files[0];
const textDocument = await theia.workspace.openTextDocument(file);
if (!textDocument) {
throw new Error('Cannot open java file');
}
let plugin = theia.plugins.getPlugin('redhat.java');
if (plugin) {
return await plugin.activate()
} else {
throw new Error('No redhat.java plugin');
}
}

export function stop() {
Expand Down
16 changes: 13 additions & 3 deletions src/test-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*********************************************************************
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import Mocha from 'mocha';
module.exports = StreamLogReporter;

Expand Down Expand Up @@ -25,12 +35,12 @@ function StreamLogReporter(runner: any) {
});

runner.on('end', function() {
if (failures === 0) {
if (failures > 0) {
console.log('FINISH: %d/%d', passes, passes + failures);
writeStream.write(`TESTS FAILED: ${passes}/${passes + failures}`);
writeStream.write(`TESTS FAILED: ${passes}/${passes + failures}\n`);
} else {
console.log('FINISH: %d/%d', passes, passes + failures);
writeStream.write(`TESTS PASSED: ${passes}/${passes + failures}`);
writeStream.write(`TESTS PASSED: ${passes}/${passes + failures}\n`);
}
});
}
35 changes: 17 additions & 18 deletions tests/commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -8,7 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import * as vscode from '@theia/plugin';
import * as theia from '@theia/plugin';
import { strict as assert } from 'assert';
import { closeAllOpenFiles, getSrcDocUri } from './helper';

Expand All @@ -20,40 +20,39 @@ describe('Test that vscode-java commands are working successfully', () => {

before('Make sure vscode-java is activated', async () => {
const myHelloTextURI = getSrcDocUri('MyHelloText.java');
await vscode.window.showTextDocument(myHelloTextURI);
await theia.window.showTextDocument(myHelloTextURI);
closeAllOpenFiles();
});

it('Test Java Open all log files', async () => {

// Ensure all files are closed
closeAllOpenFiles();

// Then check if the serverLog command opens the server log
await vscode.commands.executeCommand(serverLogCmd);
const isServerLogFileOpen = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === '.log');
// Then check if the serverLog command opens both the client log and the server log
await theia.commands.executeCommand(openAllLogs);
const isServerLogFileOpen = theia.window.visibleTextEditors.filter(editor => editor.document.fileName === '.log');
assert.ok(isServerLogFileOpen);
const isClientLogFileOpen = theia.window.visibleTextEditors.filter(editor => editor.document.fileName.startsWith('client.log'));
assert.ok(isClientLogFileOpen);
});

it('Test Java Open all log files', async () => {
it('Test Java Open server log file', async () => {
// Ensure all files are closed
closeAllOpenFiles();

// Then check if the serverLog command opens the client log
await vscode.commands.executeCommand(clientLogCmd);
const isClientLogFileOpen = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName.startsWith('client.log'));
assert.ok(isClientLogFileOpen);
// Then check if the serverLog command opens the server log
await theia.commands.executeCommand(serverLogCmd);
const isServerLogFileOpen = theia.window.visibleTextEditors.filter(editor => editor.document.fileName === '.log');
assert.ok(isServerLogFileOpen);
});

it('Test Java Open all log files', async () => {
it('Test Java Open client log file', async () => {
// Ensure all files are closed
closeAllOpenFiles();

// Then check if the serverLog command opens both the client log and the server log
await vscode.commands.executeCommand(openAllLogs);
const isServerLogFileOpen = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName === '.log');
assert.ok(isServerLogFileOpen);
const isClientLogFileOpen = vscode.window.visibleTextEditors.filter(editor => editor.document.fileName.startsWith('client.log'));
// Then check if the clientLog command opens the client log
await theia.commands.executeCommand(clientLogCmd);
const isClientLogFileOpen = theia.window.visibleTextEditors.filter(editor => editor.document.fileName.startsWith('client.log'));
assert.ok(isClientLogFileOpen);
});

Expand Down
13 changes: 5 additions & 8 deletions tests/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*********************************************************************
* Copyright (c) 2019 Red Hat, Inc.
* Copyright (c) 2020 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -9,15 +9,12 @@
**********************************************************************/

import * as path from 'path';
import * as vscode from '@theia/plugin';
import * as theia from '@theia/plugin';

export const extensionID = 'redhat.java';

export function closeAllOpenFiles() {
vscode.window.visibleTextEditors.map(file => vscode.commands.executeCommand('workbench.action.closeActiveEditor', file.document.uri));
theia.window.visibleTextEditors.map(file => theia.commands.executeCommand('workbench.action.closeActiveEditor', file.document.uri));
}
export async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export const getSrcDocPath = (p: string) => path.resolve(__dirname, '../testWorkspace/src/main/java/org/my/sample', p);
export const getSrcDocUri = (p: string) => vscode.Uri.file(getSrcDocPath(p));
export const getSrcDocPath = (p: string) => path.resolve('/projects/Che-Java-Tests/testWorkspace/src/main/java/org/my/sample', p);
export const getSrcDocUri = (p: string) => theia.Uri.file(getSrcDocPath(p));
Loading

0 comments on commit ec40ea6

Please sign in to comment.