Skip to content

Commit 6ee7b6e

Browse files
committed
test(python): use native Windows path and socket semantics
1 parent b9626df commit 6ee7b6e

2 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎scripts/test-rpc.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,14 @@ def run():
100100
token.cancel()
101101
worker.join(6)
102102
self.assertFalse(worker.is_alive(), "subprocess cleanup did not finish")
103-
self.assertEqual(ready.read(1), b"", "descendant still owns its connection")
104-
pid = None
103+
try:
104+
self.assertEqual(ready.read(1), b"", "descendant still owns its connection")
105+
except ConnectionResetError:
106+
# Job termination can close a Windows socket with
107+
# a reset instead of an orderly end-of-stream.
108+
if os.name != "nt":
109+
raise
110+
pid = None
105111
if mode == "cancel":
106112
self.assertIsInstance(result[0], CancelledError)
107113
else:

‎scripts/test.mjs‎

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
writeFileSync,
2626
} from "node:fs";
2727
import { homedir, tmpdir } from "node:os";
28-
import { dirname, join, resolve } from "node:path";
28+
import { dirname, join, normalize, resolve } from "node:path";
2929
import { fileURLToPath } from "node:url";
3030
import { readZipEntries } from "./lib/archive.mjs";
3131
import { ASSET_PREFIX, manifestForPlatform, PACKAGE_ID, packagePlatforms, sha256File } from "./package.mjs";
@@ -1070,7 +1070,7 @@ try {
10701070
const wsLocalVenv = join(wsRoot, ".venv");
10711071

10721072
const makeFakePythonHome = (dir, ver, isConda = false) => {
1073-
const binDir = process.platform === "win32" ? join(dir, "Scripts") : join(dir, "bin");
1073+
const binDir = process.platform === "win32" ? (isConda ? dir : join(dir, "Scripts")) : join(dir, "bin");
10741074
mkdirSync(binDir, { recursive: true });
10751075
const exeName = process.platform === "win32" ? "python.exe" : "python3";
10761076
const exePath = join(binDir, exeName);
@@ -1093,7 +1093,8 @@ try {
10931093
makeFakePythonHome(wsLocalVenv, "3.12.1", false);
10941094

10951095
mkdirSync(join(fakeHome, ".conda"), { recursive: true });
1096-
writeFileSync(join(fakeHome, ".conda", "environments.txt"), `# comment\n${condaEnvDir}\n`);
1096+
const condaRegistry = join(fakeHome, ".conda", "environments.txt");
1097+
writeFileSync(condaRegistry, `# comment\n${condaEnvDir}\n`);
10971098
writeFileSync(join(wsRoot, ".python-version"), "3.11\n");
10981099
writeFileSync(
10991100
join(wsRoot, "pyproject.toml"),
@@ -1112,16 +1113,23 @@ try {
11121113
const pipenvDir = join(workonRoot, pipenvName);
11131114
makeFakePythonHome(pipenvDir, "3.12.3", false);
11141115

1116+
const discoveryEnv = {
1117+
HOME: fakeHome,
1118+
USERPROFILE: fakeHome,
1119+
UV_PYTHON_INSTALL_DIR: dirname(uvPyDir),
1120+
PYENV_ROOT: join(scratch, "pyenv-root"),
1121+
WORKON_HOME: workonRoot,
1122+
CONDA_ENVS_PATH: "",
1123+
CONDA_PREFIX: "",
1124+
};
1125+
// Windows environment variable names are case-insensitive. Ensure inherited
1126+
// entries with different capitalization cannot shadow the fixture values.
1127+
const inherited = Object.fromEntries(
1128+
Object.entries(process.env).filter(([key]) => !(key.toUpperCase() in discoveryEnv)),
1129+
);
11151130
const child = spawn(pythonCmd, ["-I", "-B", backendScript, "--state", join(scratch, "tc-state")], {
11161131
cwd: scratch,
1117-
env: {
1118-
...process.env,
1119-
HOME: fakeHome,
1120-
USERPROFILE: fakeHome,
1121-
UV_PYTHON_INSTALL_DIR: dirname(uvPyDir),
1122-
PYENV_ROOT: join(scratch, "pyenv-root"),
1123-
WORKON_HOME: workonRoot,
1124-
},
1132+
env: { ...inherited, ...discoveryEnv },
11251133
stdio: ["pipe", "pipe", "pipe"],
11261134
windowsHide: true,
11271135
});
@@ -1161,24 +1169,35 @@ try {
11611169
assert.equal(reply.result.generation, 3);
11621170
assert.equal(reply.result.providerID, "python");
11631171

1164-
const homes = reply.result.installations.map((i) => i.home);
1165-
assert.ok(homes.includes(realpathSync(condaEnvDir)), "Expected ~/.conda/environments.txt installation");
1166-
assert.ok(homes.includes(realpathSync(uvPyDir)), "Expected UV_PYTHON_INSTALL_DIR installation");
1167-
assert.ok(homes.includes(realpathSync(pyenvVerDir)), "Expected PYENV_ROOT installation");
1168-
assert.ok(homes.includes(realpathSync(pipenvDir)), "Expected WORKON_HOME Pipenv installation");
1169-
assert.equal(homes.includes(realpathSync(wsLocalVenv)), false, "Must NEVER return workspace-local .venv");
1170-
1171-
const bySource = Object.fromEntries(reply.result.statements.map((s) => [s.source, s]));
1172-
const pyVerStmt = bySource[realpathSync(join(wsRoot, ".python-version"))];
1172+
// Python and Node may preserve different drive letter and directory
1173+
// casing for a real path on Windows. Compare native path identities.
1174+
const pathKey = (path) => {
1175+
const nativePath = normalize(path);
1176+
return process.platform === "win32" ? nativePath.toLowerCase() : nativePath;
1177+
};
1178+
const expectedPath = (path) => pathKey(realpathSync.native(path));
1179+
assert.ok(
1180+
reply.result.watch.map(pathKey).includes(expectedPath(condaRegistry)),
1181+
"Expected toolchain discovery to read the fixture home",
1182+
);
1183+
const homes = reply.result.installations.map((installation) => pathKey(installation.home));
1184+
assert.ok(homes.includes(expectedPath(condaEnvDir)), "Expected ~/.conda/environments.txt installation");
1185+
assert.ok(homes.includes(expectedPath(uvPyDir)), "Expected UV_PYTHON_INSTALL_DIR installation");
1186+
assert.ok(homes.includes(expectedPath(pyenvVerDir)), "Expected PYENV_ROOT installation");
1187+
assert.ok(homes.includes(expectedPath(pipenvDir)), "Expected WORKON_HOME Pipenv installation");
1188+
assert.equal(homes.includes(expectedPath(wsLocalVenv)), false, "Must NEVER return workspace-local .venv");
1189+
1190+
const bySource = new Map(reply.result.statements.map((statement) => [pathKey(statement.source), statement]));
1191+
const pyVerStmt = bySource.get(expectedPath(join(wsRoot, ".python-version")));
11731192
assert.ok(pyVerStmt);
1174-
assert.equal(pyVerStmt.installation, realpathSync(condaEnvDir));
1175-
const pyprojStmt = bySource[realpathSync(join(wsRoot, "pyproject.toml"))];
1193+
assert.equal(pathKey(pyVerStmt.installation), expectedPath(condaEnvDir));
1194+
const pyprojStmt = bySource.get(expectedPath(join(wsRoot, "pyproject.toml")));
11761195
assert.ok(pyprojStmt);
1177-
assert.equal(pyprojStmt.installation, realpathSync(uvPyDir));
1178-
const pipfileStmt = bySource[realpathSync(join(wsRoot, "Pipfile"))];
1196+
assert.equal(pathKey(pyprojStmt.installation), expectedPath(uvPyDir));
1197+
const pipfileStmt = bySource.get(expectedPath(join(wsRoot, "Pipfile")));
11791198
assert.ok(pipfileStmt);
11801199
assert.equal(pipfileStmt.kind, "environment");
1181-
assert.equal(pipfileStmt.installation, realpathSync(pipenvDir));
1200+
assert.equal(pathKey(pipfileStmt.installation), expectedPath(pipenvDir));
11821201

11831202
// Remote authority must return -32071 plugin.toolchain.authorityUnsupported.
11841203
peer.send({

0 commit comments

Comments
 (0)