From 7cf8e58925aded35f8a2402f0dbc2331c89606c9 Mon Sep 17 00:00:00 2001 From: Christopher Barber Date: Sat, 16 Sep 2023 19:07:49 -0400 Subject: [PATCH 1/3] Unit test for diff subcommand (#95) --- src/whl2conda/cli/diff.py | 4 +- test/cli/test_diff.py | 118 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 test/cli/test_diff.py diff --git a/src/whl2conda/cli/diff.py b/src/whl2conda/cli/diff.py index 6a4d5b1..2a2b645 100644 --- a/src/whl2conda/cli/diff.py +++ b/src/whl2conda/cli/diff.py @@ -72,14 +72,14 @@ def diff_main( parser.add_argument( "package1", - metavar="", + metavar="", type=existing_conda_package, help="First package to compare", ) parser.add_argument( "package2", - metavar="", + metavar="", type=existing_conda_package, help="Second package to compare", ) diff --git a/test/cli/test_diff.py b/test/cli/test_diff.py new file mode 100644 index 0000000..521ace0 --- /dev/null +++ b/test/cli/test_diff.py @@ -0,0 +1,118 @@ +# Copyright 2023 Christopher Barber +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +""" +Unit test for `whl2conda diff` subcommand +""" + +from __future__ import annotations + +import argparse +import re +from pathlib import Path + +import pytest + +from whl2conda.cli import main + +# pylint: disable=unused-import +from ..test_packages import simple_conda_package, simple_wheel + +# pylint: disable=redefined-outer-name + + +def test_diff_errors( + capsys: pytest.CaptureFixture, + tmp_path: Path, + simple_conda_package: Path, +) -> None: + """ + Unit test for `whl2conda diff` arg errors + """ + + with pytest.raises(SystemExit): + main(["diff"]) + _, err = capsys.readouterr() + assert re.search(r"are required:.*package1.*package2.*--diff-tool", err) + + with pytest.raises(SystemExit): + main(["diff", str(simple_conda_package)]) + _, err = capsys.readouterr() + assert re.search(r"are required:.*package2.*--diff-tool", err) + + with pytest.raises(SystemExit): + main(["diff", str(simple_conda_package), str(simple_conda_package)]) + _, err = capsys.readouterr() + assert re.search(r"are required:.*--diff-tool", err) + + with pytest.raises(SystemExit): + main(["diff", str(simple_conda_package), "does-not-exist"]) + _, err = capsys.readouterr() + assert "does not exist" in err + + not_a_package = tmp_path / "not-a-package.txt" + not_a_package.write_text("hi") + with pytest.raises(SystemExit): + main(["diff", str(simple_conda_package), str(not_a_package)]) + _, err = capsys.readouterr() + assert "is not a conda package" in err + + +def test_diff( + capsys: pytest.CaptureFixture, # pylint: disable=unused-argument + tmp_path: Path, + simple_conda_package: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """ + whitebox test for whl2conda diff + """ + monkeypatch.chdir(tmp_path) + + expected_diff = "diff" + expected_r = True + + def _fake_diff( + cmd: list[str], + **_kwargs, + ): + assert cmd[0] == expected_diff + parser = argparse.ArgumentParser(prog="diff") + parser.add_argument("dir1") + parser.add_argument("dir2") + parser.add_argument("-r", action="store_true") + parsed = parser.parse_args(cmd[1:]) + + assert parsed.r == expected_r + for d in [parsed.dir1, parsed.dir2]: + dirpath = Path(d) + assert dirpath.is_dir() + assert dirpath.is_relative_to(tmp_path) + info = dirpath / "info" + assert info.is_dir() + + monkeypatch.setattr("subprocess.run", _fake_diff) + + pkg = str(simple_conda_package) + + main(["diff", pkg, pkg, "-T", 'diff', "-A", "-r"]) + + expected_diff = "kdiff3" + expected_r = False + main(["diff", "--diff-tool", "kdiff3", pkg, pkg]) + + # make sure temp directories are gone + assert not list(tmp_path.glob("**/*")) + + # TODO test file normalization has happened From 39c0dc2b3a3021ae6b2c555544a2f2d58f9b9ab3 Mon Sep 17 00:00:00 2001 From: Christopher Barber Date: Sat, 16 Sep 2023 19:28:35 -0400 Subject: [PATCH 2/3] Fix python 3.8 issue with test_diff --- test/cli/test_diff.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/cli/test_diff.py b/test/cli/test_diff.py index 521ace0..abbbdb9 100644 --- a/test/cli/test_diff.py +++ b/test/cli/test_diff.py @@ -88,6 +88,7 @@ def _fake_diff( **_kwargs, ): assert cmd[0] == expected_diff + # Not is_relative_to not available in python 3.8 parser = argparse.ArgumentParser(prog="diff") parser.add_argument("dir1") parser.add_argument("dir2") @@ -98,7 +99,7 @@ def _fake_diff( for d in [parsed.dir1, parsed.dir2]: dirpath = Path(d) assert dirpath.is_dir() - assert dirpath.is_relative_to(tmp_path) + dirpath.relative_to(tmp_path) info = dirpath / "info" assert info.is_dir() From fcbce81c4016516f5c28055cb75693d9477ddfdb Mon Sep 17 00:00:00 2001 From: Christopher Barber Date: Sat, 16 Sep 2023 19:33:10 -0400 Subject: [PATCH 3/3] Move slow external tests to their own file --- test/api/test_converter.py | 69 --------------------------- test/api/test_external.py | 97 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 69 deletions(-) create mode 100644 test/api/test_external.py diff --git a/test/api/test_converter.py b/test/api/test_converter.py index 13c8eb5..7e38d67 100644 --- a/test/api/test_converter.py +++ b/test/api/test_converter.py @@ -30,7 +30,6 @@ # this package from whl2conda.api.converter import ( Wheel2CondaConverter, - Wheel2CondaError, CondaPackageFormat, DependencyRename, RequiresDistEntry, @@ -396,71 +395,3 @@ def test_poetry( pkg = test_case(wheel).build() # conda package name taken from project name assert pkg.name.startswith("poetry.example") - - -# -# External pypi tests -# - - -@pytest.mark.external -def test_pypi_tomlkit(test_case: ConverterTestCaseFactory): - """ - Test tomlkit package from pypi - """ - test_case("pypi:tomlkit").build() - - -@pytest.mark.external -def test_pypi_sphinx(test_case: ConverterTestCaseFactory): - """ - Test sphinx package from pypi - """ - test_case("pypi:sphinx").build() - - -@pytest.mark.external -def test_pypi_zstandard(test_case: ConverterTestCaseFactory): - """ - Test zstandard package - not pure python - """ - with pytest.raises(Wheel2CondaError, match="not pure python"): - test_case("pypi:zstandard").build() - - -@pytest.mark.external -def test_pypi_colorama(test_case: ConverterTestCaseFactory): - """ - Test colorama package - """ - test_case( - "pypi:colorama", - ).build() - - -@pytest.mark.external -def test_pypi_orix(test_case: ConverterTestCaseFactory) -> None: - """ - Test orix package - """ - case = test_case("pypi:orix") - orix_pkg = case.build() - assert orix_pkg.is_file() - - test_env = case.install(orix_pkg) - - subprocess.check_call(["conda", "install", "-p", str(test_env), "pytest", "--yes"]) - - subprocess.check_call( - [ - "conda", - "run", - "-p", - str(test_env), - "pytest", - "--pyargs", - "orix.tests", - "-k", - "not test_restrict_to_fundamental_sector", - ] - ) diff --git a/test/api/test_external.py b/test/api/test_external.py new file mode 100644 index 0000000..e4b1036 --- /dev/null +++ b/test/api/test_external.py @@ -0,0 +1,97 @@ +# Copyright 2023 Christopher Barber +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +""" +External pypi converter tests +""" + +from __future__ import annotations + +import subprocess + +import pytest + +from whl2conda.api.converter import Wheel2CondaError + +from .test_converter import ConverterTestCaseFactory +from .test_converter import test_case # pylint: disable=unused-import + +# pylint: disable=redefined-outer-name + +# +# External pypi tests +# + + +@pytest.mark.external +def test_pypi_tomlkit(test_case: ConverterTestCaseFactory): + """ + Test tomlkit package from pypi + """ + test_case("pypi:tomlkit").build() + + +@pytest.mark.external +def test_pypi_sphinx(test_case: ConverterTestCaseFactory): + """ + Test sphinx package from pypi + """ + test_case("pypi:sphinx").build() + + +@pytest.mark.external +def test_pypi_zstandard(test_case: ConverterTestCaseFactory): + """ + Test zstandard package - not pure python + """ + with pytest.raises(Wheel2CondaError, match="not pure python"): + test_case("pypi:zstandard").build() + + +@pytest.mark.external +def test_pypi_colorama(test_case: ConverterTestCaseFactory): + """ + Test colorama package + """ + test_case( + "pypi:colorama", + ).build() + + +@pytest.mark.external +def test_pypi_orix(test_case: ConverterTestCaseFactory) -> None: + """ + Test orix package + """ + case = test_case("pypi:orix") + orix_pkg = case.build() + assert orix_pkg.is_file() + + test_env = case.install(orix_pkg) + + subprocess.check_call(["conda", "install", "-p", str(test_env), "pytest", "--yes"]) + + subprocess.check_call( + [ + "conda", + "run", + "-p", + str(test_env), + "pytest", + "--pyargs", + "orix.tests", + "-k", + "not test_restrict_to_fundamental_sector", + ] + )