Files
setup-uv/__tests__/version/requirements-file.test.ts
T
9c7332190c fix: allow whitespace in uv requirement specifiers (#1112)
## Summary

When the uv version is read from dependencies (`requirements.txt`, or
`project.dependencies` / `optional-dependencies` / `dependency-groups`
in `pyproject.toml` via `version-file`), the specifier was matched with
`/^uv([=<>~!]+\S*)/`. PEP 508 allows whitespace around operators and
after commas, and those valid forms were misread:

| dependency | before | after |
|---|---|---|
| `uv >= 0.5.0` | not found, latest uv installed | `>=0.5.0` |
| `uv == 0.11.20` | not found, latest uv installed | `==0.11.20` |
| `uv>=0.5.0, <0.6.0` | `>=0.5.0,`, fails with `No version found for
>=0.5.0,` | `>=0.5.0,<0.6.0` |

The regex now allows whitespace around operators and between
comma-separated clauses, and strips it from the result. Inputs that
worked before give the same result, including inline `--hash=` options,
trailing ` # comments`, the `\` continuation lines from `uv pip compile
--generate-hashes`, and names that only start with `uv` (`uvicorn`,
`uv-build`).

## Test plan

- Added cases for spaced operators, spaced clauses, inline `--hash` and
trailing comments. The four whitespace cases failed before the change.
- `npm run all` on Ubuntu (WSL) with Node 24.19.0: build, Biome (no
fixes), packaging, and 165 tests across 15 suites pass. The rebuilt
`dist/setup/index.cjs` only differs in the changed line.

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 08:37:03 +01:00

49 lines
1.5 KiB
TypeScript

import { expect, test } from "@jest/globals";
import { getUvVersionFromFile } from "../../src/version/file-parser";
import {
getUvVersionFromPyprojectContent,
getUvVersionFromRequirementsText,
} from "../../src/version/requirements-file";
test("ignores dependencies starting with uv", async () => {
const parsedVersion = getUvVersionFromFile(
"__tests__/fixtures/uv-in-requirements-txt-project/requirements.txt",
);
expect(parsedVersion).toBe("0.6.17");
});
test.each([
["without space before marker", "uv==0.11.20; sys_platform != 'emscripten'"],
["with space before marker", "uv==0.11.20 ; sys_platform != 'emscripten'"],
])(
"strips PEP 508 markers from pyproject dependency groups %s",
(_, dependency) => {
const parsedVersion = getUvVersionFromPyprojectContent(`[dependency-groups]
test = [
"${dependency}",
]
`);
expect(parsedVersion).toBe("==0.11.20");
},
);
test("strips PEP 508 markers from requirements dependencies", () => {
const parsedVersion = getUvVersionFromRequirementsText(
"uv==0.11.20; sys_platform != 'emscripten'",
);
expect(parsedVersion).toBe("==0.11.20");
});
test.each([
["uv >= 0.5.0", ">=0.5.0"],
["uv == 0.11.20", "==0.11.20"],
["uv>=0.5.0, <0.6.0", ">=0.5.0,<0.6.0"],
["uv >= 0.5.0 , < 0.6.0", ">=0.5.0,<0.6.0"],
["uv==0.11.20 --hash=sha256:abc", "==0.11.20"],
["uv==0.11.20 # pinned for CI", "==0.11.20"],
])("allows whitespace in the uv specifier %s", (dependency, expected) => {
expect(getUvVersionFromRequirementsText(dependency)).toBe(expected);
});