From f9a9b423fcbc521da62d5d5d9b744475cbc78f5a Mon Sep 17 00:00:00 2001 From: Andrew Scott <3648487+ayyess@users.noreply.github.com> Date: Sun, 10 Nov 2024 19:31:56 +0000 Subject: [PATCH] Fix updating all matching version numbers Prior to this change, nix-update would update all version strings which were equal to the specified package to update. Now, only the version line of the specified package will be updated. --- README.md | 1 - nix_update/update.py | 9 ++++-- tests/test_update.py | 57 ++++++++++++++++++++++++++++++++++++++ tests/testpkgs/default.nix | 1 + tests/testpkgs/set.nix | 26 +++++++++++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 tests/test_update.py create mode 100644 tests/testpkgs/set.nix diff --git a/README.md b/README.md index 6cf61f0..ea7313c 100644 --- a/README.md +++ b/README.md @@ -237,7 +237,6 @@ nix-update might not work correctly if a file contain multiple packages as it performs naive search and replace to update version numbers. This might be a problem if: -- A file contains the same version string for multiple packages. - `name` is used instead of `pname` and/or `${version}` is injected into `name`. Related discussions: diff --git a/nix_update/update.py b/nix_update/update.py index 31eb781..3548704 100644 --- a/nix_update/update.py +++ b/nix_update/update.py @@ -36,10 +36,15 @@ def replace_version(package: Package) -> bool: if changed: info(f"Update {old_version} -> {new_version} in {package.filename}") with fileinput.FileInput(package.filename, inplace=True) as f: - for line in f: + for i, line in enumerate(f, 1): if package.new_version.rev: line = line.replace(package.rev, package.new_version.rev) - print(line.replace(f'"{old_version}"', f'"{new_version}"'), end="") + if ( + package.version_position is None + or package.version_position.line == i + ): + line = line.replace(f'"{old_version}"', f'"{new_version}"') + print(line, end="") else: info(f"Not updating version, already {old_version}") diff --git a/tests/test_update.py b/tests/test_update.py new file mode 100644 index 0000000..d915237 --- /dev/null +++ b/tests/test_update.py @@ -0,0 +1,57 @@ +import os +import subprocess + +import conftest +import pytest + +from nix_update import main + + +@pytest.mark.skipif( + "GITHUB_TOKEN" not in os.environ, reason="No GITHUB_TOKEN environment variable set" +) +def test_multiple_sources(helpers: conftest.Helpers) -> None: + with helpers.testpkgs(init_git=True) as path: + main(["--file", str(path), "--commit", "set.fd"]) + fd_version = subprocess.run( + [ + "nix", + "eval", + "--raw", + "--extra-experimental-features", + "nix-command", + "-f", + path, + "set.fd.version", + ], + check=True, + text=True, + stdout=subprocess.PIPE, + ).stdout.strip() + skim_version = subprocess.run( + [ + "nix", + "eval", + "--raw", + "--extra-experimental-features", + "nix-command", + "-f", + path, + "set.skim.version", + ], + check=True, + text=True, + stdout=subprocess.PIPE, + ).stdout.strip() + assert tuple(map(int, fd_version.split("."))) >= (4, 4, 3) + assert tuple(map(int, skim_version.split("."))) == (0, 0, 0) + commit = subprocess.run( + ["git", "-C", path, "log", "-1"], + text=True, + stdout=subprocess.PIPE, + check=True, + ).stdout.strip() + print(commit) + assert fd_version in commit + assert "sharkdp/fd/compare/v0.0.0..." in commit + assert "skim" not in commit diff --git a/tests/testpkgs/default.nix b/tests/testpkgs/default.nix index f3fa54c..ff89070 100644 --- a/tests/testpkgs/default.nix +++ b/tests/testpkgs/default.nix @@ -28,4 +28,5 @@ pnpm = pkgs.callPackage ./pnpm.nix { }; maven = pkgs.callPackage ./maven.nix { }; mix = pkgs.callPackage ./mix.nix { }; + set = pkgs.callPackage ./set.nix { }; } diff --git a/tests/testpkgs/set.nix b/tests/testpkgs/set.nix new file mode 100644 index 0000000..5aeead6 --- /dev/null +++ b/tests/testpkgs/set.nix @@ -0,0 +1,26 @@ +{ stdenv, fetchFromGitHub }: +{ + fd = stdenv.mkDerivation rec { + pname = "fd"; + version = "0.0.0"; + + src = fetchFromGitHub { + owner = "sharkdp"; + repo = pname; + rev = "v${version}"; + sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; + }; + }; + + skim = stdenv.mkDerivation rec { + pname = "skim"; + version = "0.0.0"; + + src = fetchFromGitHub { + owner = "skim-rs"; + repo = pname; + rev = "v${version}"; + sha256 = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; + }; + }; +}