fix: 修复 OpenCode 运行时启动与本地打包

This commit is contained in:
2026-08-09 00:02:49 +08:00
parent d092132d86
commit a6fe14a8d6
20 changed files with 1847 additions and 154 deletions

View File

@@ -2,7 +2,12 @@ import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { delimiter, dirname, join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { prependPythonToPath, resolvePythonRuntime } from '@electron/utils/python-runtime';
import {
prependManagedRuntimesToPath,
prependPythonToPath,
resolvePythonRuntime,
resolveUvRuntime,
} from '@electron/utils/python-runtime';
const temporaryDirectories: string[] = [];
@@ -54,6 +59,21 @@ describe('managed Python runtime resolution', () => {
})).toEqual({ executable, binDir: dirname(executable), source: 'bundled' });
});
it('does not allow a packaged build to override Python outside the installation', async () => {
const resourcesPath = await temporaryDirectory('niancode-python-packaged-local-');
const executable = await createFile(join(resourcesPath, 'python', 'python.exe'));
const configuredPath = await createFile(join(resourcesPath, 'external', 'python.exe'));
expect(resolvePythonRuntime({
isPackaged: true,
resourcesPath,
appPath: resourcesPath,
platform: 'win32',
arch: 'x64',
configuredPath,
})).toEqual({ executable, binDir: dirname(executable), source: 'bundled' });
});
it('resolves the packaged Unix interpreter', async () => {
const resourcesPath = await temporaryDirectory('niancode-python-packaged-unix-');
const executable = await createFile(join(resourcesPath, 'python', 'bin', 'python3'));
@@ -110,4 +130,45 @@ describe('managed Python runtime resolution', () => {
expect(result.Path).toBe(`${runtime.binDir}${delimiter}existing-bin`);
expect(result).not.toHaveProperty('PATH');
});
it('resolves packaged uv and prepends both managed runtime directories', async () => {
const resourcesPath = await temporaryDirectory('niancode-uv-packaged-');
const uvExecutable = await createFile(join(resourcesPath, 'bin', 'uv.exe'));
const pythonExecutable = await createFile(join(resourcesPath, 'python', 'python.exe'));
const uvRuntime = resolveUvRuntime({
isPackaged: true,
resourcesPath,
appPath: resourcesPath,
platform: 'win32',
arch: 'x64',
});
expect(uvRuntime).toEqual({
executable: uvExecutable,
binDir: dirname(uvExecutable),
source: 'bundled',
});
expect(prependManagedRuntimesToPath(
{ Path: 'C:\\Windows\\System32' },
{ executable: pythonExecutable, binDir: dirname(pythonExecutable), source: 'bundled' },
uvRuntime,
'win32',
)).toEqual({
Path: `${dirname(uvExecutable)}${delimiter}${dirname(pythonExecutable)}${delimiter}C:\\Windows\\System32`,
NIANCODE_PYTHON_PATH: pythonExecutable,
NIANCODE_UV_PATH: uvExecutable,
});
});
it('fails closed when packaged uv is missing', async () => {
const resourcesPath = await temporaryDirectory('niancode-uv-packaged-missing-');
expect(() => resolveUvRuntime({
isPackaged: true,
resourcesPath,
appPath: resourcesPath,
platform: 'win32',
arch: 'x64',
})).toThrow(`Bundled uv runtime is missing: ${join(resourcesPath, 'bin', 'uv.exe')}`);
});
});