Skip to content

Commit

Permalink
Merge pull request #36 from Mic92/fix-hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Jan 9, 2021
2 parents b061abc + 9e09664 commit 4c85815
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v12
- run: nix-build -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixpkgs-unstable.tar.gz
- name: build
run: NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixpkgs-unstable.tar.gz nix-build
- name: Run tests
run: NIX_PATH=nixpkgs=https://github.com/NixOS/nixpkgs/archive/nixpkgs-unstable.tar.gz nix-shell --command "py.test -s ."
3 changes: 1 addition & 2 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ python3.pkgs.buildPythonApplication rec {
mypy
# technically not a test input, but we need it for development in PATH
nixFlakes
nix-prefetch
];
checkPhase = ''
echo -e "\x1b[32m## run black\x1b[0m"
LC_ALL=en_US.utf-8 black --check .
echo -e "\x1b[32m## run pytest\x1b[0m"
py.test -s .
echo -e "\x1b[32m## run flake8\x1b[0m"
flake8 nix_update
echo -e "\x1b[32m## run mypy\x1b[0m"
Expand Down
22 changes: 12 additions & 10 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
description = "Swiss-knife for updating nix packages.";

inputs.nixpkgs.url = "github:NixOS/nixpkgs";
inputs.flake-utils.url = "github:numtide/flake-utils";

outputs = { self, nixpkgs, flake-utils }:
Expand Down
16 changes: 8 additions & 8 deletions nix_update/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

@dataclass
class Options:
version: str
import_path: str
commit: bool
attribute: str
shell: bool
run: bool
build: bool
test: bool
version_regex: str
version: str = "auto"
version_regex: str = "(.*)"
import_path: str = "./."
commit: bool = False
shell: bool = False
run: bool = False
build: bool = False
test: bool = False
15 changes: 13 additions & 2 deletions nix_update/update.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fileinput
from typing import List
from typing import List, Optional, Dict
import subprocess
import tempfile

from .errors import UpdateError
from .eval import Package, eval_attr
Expand Down Expand Up @@ -48,6 +49,7 @@ def to_sri(hashstr: str) -> str:
"nix",
"--experimental-features",
"nix-command",
"hash",
"to-sri",
f"{prefix}{hashstr}",
]
Expand All @@ -65,7 +67,16 @@ def replace_hash(filename: str, current: str, target: str) -> None:


def nix_prefetch(cmd: List[str]) -> str:
res = run(["nix-prefetch"] + cmd)
extra_env: Dict[str, str] = {}
tempdir: Optional[tempfile.TemporaryDirectory[str]] = None
if extra_env.get("XDG_RUNTIME_DIR") is None:
tempdir = tempfile.TemporaryDirectory()
extra_env["XDG_RUNTIME_DIR"] = tempdir.name
try:
res = run(["nix-prefetch"] + cmd, extra_env=extra_env)
finally:
if tempdir:
tempdir.cleanup()
return res.stdout.strip()


Expand Down
9 changes: 7 additions & 2 deletions nix_update/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import subprocess
import sys
from pathlib import Path
from typing import IO, Any, Callable, List, Optional, Union
from typing import IO, Any, Callable, List, Optional, Union, Dict

HAS_TTY = sys.stdout.isatty()
ROOT = Path(os.path.dirname(os.path.realpath(__file__)))
Expand All @@ -28,9 +28,14 @@ def run(
cwd: Optional[Union[Path, str]] = None,
stdout: Union[None, int, IO[Any]] = subprocess.PIPE,
check: bool = True,
extra_env: Dict[str, str] = {},
) -> "subprocess.CompletedProcess[str]":
info("$ " + " ".join(command))
return subprocess.run(command, cwd=cwd, check=check, text=True, stdout=stdout)
env = os.environ.copy()
env.update(extra_env)
return subprocess.run(
command, cwd=cwd, check=check, text=True, stdout=stdout, env=env
)


def extract_version(version: str, version_regex: str) -> Optional[str]:
Expand Down
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env python3

import pytest
import sys
from pathlib import Path
from typing import Type, Iterator, Any
import shutil
import tempfile
from contextlib import contextmanager


TEST_ROOT = Path(__file__).parent.resolve()
sys.path.append(str(TEST_ROOT.parent))


class Helpers:
@staticmethod
def root() -> Path:
return TEST_ROOT

@staticmethod
@contextmanager
def testpkgs() -> Iterator[Path]:
with tempfile.TemporaryDirectory() as tmpdirname:
shutil.copytree(
Helpers.root().joinpath("testpkgs"), tmpdirname, dirs_exist_ok=True
)
yield Path(tmpdirname)


@pytest.fixture
def helpers() -> Type[Helpers]:
return Helpers
12 changes: 5 additions & 7 deletions tests/test_git.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#!/usr/bin/env python3

from pathlib import Path
from unittest import TestCase
from nix_update.git import old_version_from_diff

import conftest

TEST_ROOT = Path(__file__).parent.resolve()


class WordDiff(TestCase):
def test_worddiff(self) -> None:
with open(TEST_ROOT.joinpath("consul.patch")) as f:
diff = f.read()
def test_worddiff(helpers: conftest.Helpers) -> None:
with open(helpers.root().joinpath("consul.patch")) as f:
diff = f.read()
s = old_version_from_diff(diff, 5, "1.9.0")
self.assertEqual(s, "1.8.6")
assert s == "1.8.6"
27 changes: 27 additions & 0 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env python3

from nix_update.options import Options
from nix_update.update import update
import subprocess
import conftest


def test_update(helpers: conftest.Helpers) -> None:
with helpers.testpkgs() as path:
opts = Options(attribute="pypi", import_path=path)
update(opts)
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--experimental-features",
"nix-command",
"-f",
path,
"pypi.version",
],
text=True,
stdout=subprocess.PIPE,
)
assert version.stdout.strip() >= "3.0.1"
4 changes: 4 additions & 0 deletions tests/testpkgs/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{ pkgs ? import <nixpkgs> {} }:
{
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix {};
}
16 changes: 16 additions & 0 deletions tests/testpkgs/pypi.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{ buildPythonPackage, fetchPypi, twisted, mock, pytestCheckHook }:

buildPythonPackage rec {
pname = "python-mpd2";
version = "1.0.0";
src = fetchPypi {
inherit pname version;
sha256 = "0000000000000000000000000000000000000000000000000000";
};
checkInputs = [
pytestCheckHook
twisted
mock
];
pytestFlagsArray = [ "mpd/tests.py" ];
}

0 comments on commit 4c85815

Please sign in to comment.