Skip to content

Commit 7ca29ae

Browse files
committed
feat(version): add support for syncing Cargo.lock with Cargo.toml
1 parent c5d6495 commit 7ca29ae

5 files changed

Lines changed: 568 additions & 57 deletions

File tree

tests/integration/test_version_integration.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,110 @@ def test_update_version_files_ignore_node_modules(self, tmp_path):
305305
node_modules_content = json.loads(node_modules_package_json.read_text())
306306
assert node_modules_content["version"] == "1.0.0"
307307

308+
def _build_args(self, tmp_path, *, recursive: bool) -> VersionArgs:
309+
return VersionArgs(
310+
version="",
311+
verbose=0,
312+
no_commit=True,
313+
no_tag=True,
314+
no_push=True,
315+
patch=False,
316+
minor=False,
317+
major=False,
318+
prepatch="",
319+
preminor="",
320+
premajor="",
321+
recursive=recursive,
322+
custom="",
323+
path=str(tmp_path),
324+
)
325+
326+
def test_update_version_files_syncs_cargo_lock(self, tmp_path):
327+
"""End-to-end: bumping a crate's Cargo.toml also rewrites its
328+
Cargo.lock entry. The lockfile lives next to the manifest."""
329+
(tmp_path / "Cargo.toml").write_text(
330+
'[package]\nname = "demo"\nversion = "0.1.0"\n',
331+
encoding="utf-8",
332+
)
333+
(tmp_path / "Cargo.lock").write_text(
334+
'[[package]]\nname = "demo"\nversion = "0.1.0"\n',
335+
encoding="utf-8",
336+
)
337+
update_version_files(
338+
self._build_args(tmp_path, recursive=False),
339+
Version(major=0, minor=2, patch=0),
340+
verbose=0,
341+
recursive=False,
342+
)
343+
assert 'version = "0.2.0"' in (tmp_path / "Cargo.toml").read_text()
344+
assert 'version = "0.2.0"' in (tmp_path / "Cargo.lock").read_text()
345+
346+
def test_update_version_files_workspace_lockfile_dedups(self, tmp_path):
347+
"""Cargo workspace with two members and one shared lockfile —
348+
both [[package]] entries get rewritten, but the lockfile is
349+
not duplicated."""
350+
(tmp_path / ".git").mkdir()
351+
(tmp_path / "Cargo.toml").write_text(
352+
'[workspace]\nmembers = ["crates/alpha", "crates/beta"]\n',
353+
encoding="utf-8",
354+
)
355+
(tmp_path / "Cargo.lock").write_text(
356+
'[[package]]\nname = "alpha"\nversion = "0.1.0"\n\n'
357+
'[[package]]\nname = "beta"\nversion = "0.1.0"\n',
358+
encoding="utf-8",
359+
)
360+
alpha = tmp_path / "crates" / "alpha"
361+
alpha.mkdir(parents=True)
362+
(alpha / "Cargo.toml").write_text(
363+
'[package]\nname = "alpha"\nversion = "0.1.0"\n',
364+
encoding="utf-8",
365+
)
366+
beta = tmp_path / "crates" / "beta"
367+
beta.mkdir(parents=True)
368+
(beta / "Cargo.toml").write_text(
369+
'[package]\nname = "beta"\nversion = "0.1.0"\n',
370+
encoding="utf-8",
371+
)
372+
update_version_files(
373+
self._build_args(tmp_path, recursive=True),
374+
Version(major=0, minor=2, patch=0),
375+
verbose=0,
376+
recursive=True,
377+
)
378+
lockfile_content = (tmp_path / "Cargo.lock").read_text()
379+
assert lockfile_content.count('version = "0.2.0"') == 2
380+
assert 'version = "0.1.0"' not in lockfile_content
381+
382+
def test_update_version_files_pnpm_root_without_version(self, tmp_path):
383+
"""pnpm workspace root without `version`: bump must insert
384+
one in the root package.json and update children too."""
385+
(tmp_path / "package.json").write_text(
386+
'{\n "name": "monorepo",\n "private": true\n}\n',
387+
encoding="utf-8",
388+
)
389+
(tmp_path / "pnpm-workspace.yaml").write_text(
390+
'packages:\n - "packages/*"\n',
391+
encoding="utf-8",
392+
)
393+
alpha = tmp_path / "packages" / "alpha"
394+
alpha.mkdir(parents=True)
395+
(alpha / "package.json").write_text(
396+
'{"name": "@x/alpha", "version": "0.1.0"}\n',
397+
encoding="utf-8",
398+
)
399+
400+
update_version_files(
401+
self._build_args(tmp_path, recursive=True),
402+
Version(major=0, minor=2, patch=0),
403+
verbose=0,
404+
recursive=True,
405+
)
406+
root_data = json.loads((tmp_path / "package.json").read_text())
407+
assert root_data["version"] == "0.2.0"
408+
assert root_data["name"] == "monorepo"
409+
child_data = json.loads((alpha / "package.json").read_text())
410+
assert child_data["version"] == "0.2.0"
411+
308412
@patch("tgit.version.get_next_version")
309413
@patch("tgit.version.get_current_version")
310414
@patch("tgit.version.update_version_files")

0 commit comments

Comments
 (0)