feat:添加测试程序(未添加测试用例)

This commit is contained in:
li1553770945 2025-06-15 00:18:17 +08:00
parent 7e203905de
commit 0560fda4bb
7 changed files with 1470 additions and 14 deletions

1375
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -223,15 +223,15 @@
"scripts": { "scripts": {
"setup": "npm i && npm run prepare:ocr", "setup": "npm i && npm run prepare:ocr",
"serve": "turbo serve", "serve": "turbo serve",
"build": "turbo build", "build": "turbo build && tsc -p ./",
"build:electron": "turbo build --filter=@openmcp/electron", "build:electron": "turbo build --filter=@openmcp/electron",
"build:all": "turbo build", "build:all": "turbo build",
"vscode:prepublish": "rollup --config rollup.config.js", "vscode:prepublish": "rollup --config rollup.config.js",
"compile": "tsc -p ./", "compile": "tsc -p ./",
"watch": "tsc -watch -p ./", "watch": "tsc -watch -p ./",
"pretest": "npm run compile && npm run lint", "pretest": "npm run build",
"lint": "eslint src --ext ts", "lint": "eslint src --ext ts",
"test": "node ./out/test/runTest.js", "test": "node ./dist/test/e2e/runTest.js",
"prepare:ocr": "rollup --config rollup.tesseract.js", "prepare:ocr": "rollup --config rollup.tesseract.js",
"build:task-loop": "npx vite build --config webpack/vite.config.task-loop.mjs && node renderer/scripts/task-loop.build.mjs" "build:task-loop": "npx vite build --config webpack/vite.config.task-loop.mjs && node renderer/scripts/task-loop.build.mjs"
}, },
@ -258,6 +258,8 @@
"@types/pako": "^2.0.3", "@types/pako": "^2.0.3",
"@types/showdown": "^2.0.0", "@types/showdown": "^2.0.0",
"@types/vscode": "^1.72.0", "@types/vscode": "^1.72.0",
"@vscode/test-cli": "^0.0.11",
"@vscode/test-electron": "^2.5.2",
"copy-webpack-plugin": "^13.0.0", "copy-webpack-plugin": "^13.0.0",
"fork-ts-checker-webpack-plugin": "^9.1.0", "fork-ts-checker-webpack-plugin": "^9.1.0",
"null-loader": "^4.0.1", "null-loader": "^4.0.1",

View File

@ -10,12 +10,15 @@ export function activate(context: vscode.ExtensionContext) {
// 获取当前打开的项目的路径 // 获取当前打开的项目的路径
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const workspace = workspaceFolder?.uri.fsPath || ''; const workspace = workspaceFolder?.uri.fsPath || '';
console.log("aaa")
setVscodeWorkspace(workspace); setVscodeWorkspace(workspace);
console.log("bbb")
setRunningCWD(context.extensionPath); setRunningCWD(context.extensionPath);
console.log("ccc")
initialiseI18n(context); initialiseI18n(context);
console.log("ddd")
launch(context); launch(context);
console.log("eee")
} }

View File

@ -96,6 +96,9 @@ export function getConnectionConfig() {
export function getWorkspaceConnectionConfigPath() { export function getWorkspaceConnectionConfigPath() {
const workspace = getWorkspacePath(); const workspace = getWorkspacePath();
const configDir = fspath.join(workspace, '.openmcp'); const configDir = fspath.join(workspace, '.openmcp');
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true }); // 递归创建目录
}
const connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME); const connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME);
return connectionConfig; return connectionConfig;
} }
@ -296,6 +299,7 @@ function normaliseConnectionFilePath(item: McpOptions, workspace: string) {
export function getWorkspacePath() { export function getWorkspacePath() {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
console.log('getWorkspacePath: ', vscode.workspace.workspaceFolders);
return (workspaceFolder?.uri.fsPath || '').replace(/\\/g, '/'); return (workspaceFolder?.uri.fsPath || '').replace(/\\/g, '/');
} }

32
src/test/e2e/runTest.ts Normal file
View File

@ -0,0 +1,32 @@
import * as path from 'path';
import { runTests } from '@vscode/test-electron';
import { fileURLToPath } from 'url';
// 将 import.meta.url 转换为文件路径
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../../');
console.log('Extension Path:', extensionDevelopmentPath); // 添加日志验证路径
// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index.js');
// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath: extensionDevelopmentPath,
extensionTestsPath: extensionTestsPath,
});
} catch (err) {
console.error('Failed to run tests');
console.error(err);
process.exit(1);
}
}
main();

View File

@ -0,0 +1,15 @@
import * as assert from 'assert';
// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
import * as vscode from 'vscode';
// import * as myExtension from '../../extension';
suite('Extension Test Suite', () => {
// vscode.window.showInformationMessage('Start all tests.');
console.log("Running sample test")
test('Sample test', () => {
assert.strictEqual([1, 2, 3].indexOf(5), -1);
assert.strictEqual([1, 2, 3].indexOf(0), -1);
});
});

View File

@ -0,0 +1,43 @@
import * as path from 'path';
import Mocha from 'mocha';
import {glob} from 'glob';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function run(): Promise<void> {
// Create the mocha test
const mocha = new Mocha({
ui: 'tdd'
});
const testsRoot = path.resolve(__dirname, '..');
return new Promise((c, e) => {
glob('**/**.test.js', { cwd: testsRoot }).then((files: string[]) => {
// Add files to the test suite
console.log('Test files found:', files); // Log the found test files
files.forEach((f: string) => mocha.addFile(path.resolve(testsRoot, f)));
try {
// Run the mocha test
console.log("Running test:", files)
mocha.run(failures => {
if (failures > 0) {
e(new Error(`${failures} tests failed.`));
} else {
c();
}
});
} catch (err) {
console.error(err);
e(err);
}
}).catch(err => {
e(err);
});
});
}