test: Add tests for enabling tests (#1358)

Signed-off-by: Sheng Chen <sheche@microsoft.com>
This commit is contained in:
Sheng Chen 2022-01-20 10:49:22 +08:00 коммит произвёл GitHub
Родитель ec66d35dc7
Коммит a23b4b8914
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
9 изменённых файлов: 130 добавлений и 9 удалений

12
.vscode/launch.json поставляемый
Просмотреть файл

@ -25,7 +25,7 @@
"projectName": "com.microsoft.java.test.plugin",
},
{
"name": "Launch Tests",
"name": "Launch Tests (maven-junit)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
@ -33,6 +33,16 @@
"sourceMaps": true,
"outFiles": [ "${workspaceFolder}/dist/**/*.js" ],
"preLaunchTask": "npm: compile"
},
{
"name": "Launch Tests (unmanaged-folder)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["${workspaceFolder}/test/test-projects/simple", "--extensionDevelopmentPath=${workspaceFolder}", "--extensionTestsPath=${workspaceFolder}/out/test/unmanaged-folder-suite" ],
"sourceMaps": true,
"outFiles": [ "${workspaceFolder}/dist/**/*.js" ],
"preLaunchTask": "npm: compile"
}
]
}

Просмотреть файл

@ -15,7 +15,7 @@ import { URL } from 'url';
import { ClientRequest, IncomingMessage } from 'http';
import { sendError } from 'vscode-extension-telemetry-wrapper';
export async function enableTests(): Promise<void> {
export async function enableTests(testKind?: TestKind): Promise<void> {
const project: IJavaTestItem | undefined = await getTargetProject();
if (!project) {
return;
@ -24,7 +24,7 @@ export async function enableTests(): Promise<void> {
const projectType: ProjectType = await getProjectType(project);
switch (projectType) {
case ProjectType.UnmanagedFolder:
await setupUnmanagedFolder(Uri.parse(project.uri!));
await setupUnmanagedFolder(Uri.parse(project.uri!), testKind);
return;
default:
// currently other typed projects are not supported.
@ -52,8 +52,8 @@ async function getTargetProject(): Promise<IJavaTestItem | undefined> {
return testProjects[0];
}
async function setupUnmanagedFolder(projectUri: Uri): Promise<void> {
const testKind: TestKind | undefined = await getTestKind();
async function setupUnmanagedFolder(projectUri: Uri, testKind?: TestKind): Promise<void> {
testKind ??= await getTestKind();
if (testKind === undefined) {
return;
}
@ -68,7 +68,7 @@ async function setupUnmanagedFolder(projectUri: Uri): Promise<void> {
location: ProgressLocation.Notification,
cancellable: true
}, async (progress: Progress<{message?: string; increment?: number}>, token: CancellationToken) => {
const metadata: IArtifactMetadata[] = getJarIds(testKind);
const metadata: IArtifactMetadata[] = getJarIds(testKind!);
for (const jar of metadata) {
if (token.isCancellationRequested) {
throw new Error('User cancelled');

Просмотреть файл

@ -26,7 +26,7 @@ async function main(): Promise<void> {
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath: string = path.resolve(__dirname, '..', '..');
// Run general test
// Run maven test
await runTests({
vscodeExecutablePath,
extensionDevelopmentPath,
@ -37,6 +37,17 @@ async function main(): Promise<void> {
],
});
// Run unmanaged folder test
await runTests({
vscodeExecutablePath,
extensionDevelopmentPath,
extensionTestsPath: path.resolve(__dirname, 'unmanaged-folder-suite'),
launchArgs: [
'--disable-workspace-trust',
path.join(__dirname, '..', '..', 'test', 'test-projects', 'simple'),
],
});
process.exit(0);
} catch (err) {

Просмотреть файл

@ -3,7 +3,7 @@
'use strict';
import assert = require('assert');
import * as assert from 'assert';
import { TestController, TestRunRequest, tests, workspace } from 'vscode';
import { JUnitRunner } from '../../src/runners/junitRunner/JunitRunner';
import { IRunTestContext, TestKind } from '../../src/types';

Просмотреть файл

@ -3,7 +3,7 @@
'use strict';
import assert = require('assert');
import * as assert from 'assert';
import * as path from 'path';
import { Uri, window } from 'vscode';
import { ITestNavigationResult } from '../../src/commands/navigation/navigationCommands';

7
test/test-projects/simple/.vscode/settings.json поставляемый Normal file
Просмотреть файл

@ -0,0 +1,7 @@
{
"java.server.launchMode": "Standard",
"java.project.sourcePaths": ["src"],
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

Просмотреть файл

@ -0,0 +1,3 @@
public class App {
public static void main(String[] args) {}
}

Просмотреть файл

@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
'use strict';
import * as assert from 'assert';
import * as path from 'path';
import * as fse from 'fs-extra';
import { Uri, window } from 'vscode';
import { enableTests } from '../../src/commands/testDependenciesCommands';
import { TestKind } from '../../src/types';
import { setupTestEnv, sleep } from '../suite/utils';
// tslint:disable: only-arrow-functions
// tslint:disable: no-object-literal-type-assertion
const PROJECT_PATH: string = path.join(__dirname, '../../..', 'test', 'test-projects','simple');
const LIB_PATH: string = path.join(PROJECT_PATH, 'lib');
suite('Test Enable Tests', () => {
suiteSetup(async function() {
const filePath: string = path.join(PROJECT_PATH, 'src', 'App.java');
await window.showTextDocument(Uri.file(filePath));
await setupTestEnv();
});
test('test enable tests for unmanaged folder', async () => {
await enableTests(TestKind.JUnit5);
for (let i = 0; i < 5; i++) {
if (await fse.pathExists(LIB_PATH)) {
const files: string[] = await fse.readdir(LIB_PATH);
const downloaded: boolean = files.some((file) => {
return file.includes('junit-platform-console-standalone');
});
if (downloaded) {
return;
}
}
await sleep(1000 /*ms*/);
}
assert.fail('Failed to download test dependencies for unmanaged folder.');
});
suiteTeardown(async function() {
await fse.remove(LIB_PATH);
})
});

Просмотреть файл

@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as glob from 'glob';
import * as Mocha from 'mocha';
import * as path from 'path';
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd',
color: true,
timeout: 1 * 60 * 1000,
});
const testsRoot = __dirname;
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return e(err);
}
// Add files to the test suite
files.forEach((f) => mocha.addFile(path.resolve(testsRoot, f)));
try {
// Run the mocha test
mocha.run((failures) => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
e(err);
}
});
});
}