Skip to content

Commit

Permalink
Fix updating all matching version numbers
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ayyess committed Nov 11, 2024
1 parent 304bf08 commit f9a9b42
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions nix_update/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
57 changes: 57 additions & 0 deletions tests/test_update.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@
pnpm = pkgs.callPackage ./pnpm.nix { };
maven = pkgs.callPackage ./maven.nix { };
mix = pkgs.callPackage ./mix.nix { };
set = pkgs.callPackage ./set.nix { };
}
26 changes: 26 additions & 0 deletions tests/testpkgs/set.nix
Original file line number Diff line number Diff line change
@@ -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=";
};
};
}

0 comments on commit f9a9b42

Please sign in to comment.