diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1ae6e51..5c5db27 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 ." diff --git a/default.nix b/default.nix index 1f32712..6f38f1c 100644 --- a/default.nix +++ b/default.nix @@ -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" diff --git a/nix_update/options.py b/nix_update/options.py index 9b42a6c..0202aa5 100644 --- a/nix_update/options.py +++ b/nix_update/options.py @@ -3,12 +3,12 @@ from dataclasses import dataclass @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 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..f204d31 --- /dev/null +++ b/tests/conftest.py @@ -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 diff --git a/tests/test_git.py b/tests/test_git.py index a0a10e5..dcc118c 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -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" diff --git a/tests/test_pypi.py b/tests/test_pypi.py new file mode 100644 index 0000000..354a7d9 --- /dev/null +++ b/tests/test_pypi.py @@ -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" diff --git a/tests/testpkgs/default.nix b/tests/testpkgs/default.nix new file mode 100644 index 0000000..2416a5d --- /dev/null +++ b/tests/testpkgs/default.nix @@ -0,0 +1,4 @@ +{ pkgs ? import {} }: +{ + pypi = pkgs.python3.pkgs.callPackage ./pypi.nix {}; +} diff --git a/tests/testpkgs/pypi.nix b/tests/testpkgs/pypi.nix new file mode 100644 index 0000000..077e0ba --- /dev/null +++ b/tests/testpkgs/pypi.nix @@ -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" ]; +}