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/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", + ] + ) diff --git a/test/cli/test_diff.py b/test/cli/test_diff.py new file mode 100644 index 0000000..abbbdb9 --- /dev/null +++ b/test/cli/test_diff.py @@ -0,0 +1,119 @@ +# 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 + # Not is_relative_to not available in python 3.8 + 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() + dirpath.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