Skip to content

Commit

Permalink
Merge pull request #96 from zuzukin/diff-test
Browse files Browse the repository at this point in the history
Unit test for diff subcommand (#95)
  • Loading branch information
analog-cbarber authored Sep 17, 2023
2 parents a7aeb82 + fcbce81 commit b37740d
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 71 deletions.
4 changes: 2 additions & 2 deletions src/whl2conda/cli/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ def diff_main(

parser.add_argument(
"package1",
metavar="<conda-package>",
metavar="<package1>",
type=existing_conda_package,
help="First package to compare",
)

parser.add_argument(
"package2",
metavar="<conda-package>",
metavar="<package2>",
type=existing_conda_package,
help="Second package to compare",
)
Expand Down
69 changes: 0 additions & 69 deletions test/api/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
# this package
from whl2conda.api.converter import (
Wheel2CondaConverter,
Wheel2CondaError,
CondaPackageFormat,
DependencyRename,
RequiresDistEntry,
Expand Down Expand Up @@ -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",
]
)
97 changes: 97 additions & 0 deletions test/api/test_external.py
Original file line number Diff line number Diff line change
@@ -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",
]
)
119 changes: 119 additions & 0 deletions test/cli/test_diff.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b37740d

Please sign in to comment.