Skip to content

Commit

Permalink
Add pytest-mock as a dependency for testing (sagemath#274)
Browse files Browse the repository at this point in the history
Add cli option to change the url that grayskull will remover the pypi metadata
  • Loading branch information
marcelotrevisani authored Jan 15, 2022
1 parent 04d046a commit 7acbee7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dependencies:
- pytest-cov
- pytest-xdist
- pytest-forked
- pytest-mock
- requests
- ruamel.yaml >=0.16.10
- ruamel.yaml.jinja2
Expand Down
20 changes: 12 additions & 8 deletions grayskull/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def main(args=None):
dest="is_strict_conda_forge",
help="It will generate the recipes strict for the conda-forge channel.",
)
pypi_cmds.add_argument(
"--pypi-url",
default="https://pypi.org/pypi/",
dest="url_pypi_metadata",
help="Pypi url server",
)

args = parser.parse_args(args)

Expand Down Expand Up @@ -121,7 +127,10 @@ def main(args=None):
)
try:
recipe, config = create_python_recipe(
pkg_name, args.is_strict_conda_forge, args.download
pkg_name,
is_strict_cf=args.is_strict_conda_forge,
download=args.download,
url_pypi_metadata=args.url_pypi_metadata,
)
except requests.exceptions.HTTPError as err:
print_msg(f"{Fore.RED}Package seems to be missing.\nException: {err}\n\n")
Expand All @@ -139,14 +148,9 @@ def main(args=None):
)


def create_python_recipe(pkg_name, is_strict_conda_forge=True, download=False):
def create_python_recipe(pkg_name, **kwargs):
pkg_origin, pkg_name, pkg_version = parse_pkg_name_version(pkg_name)
config = Configuration(
name=pkg_name,
version=pkg_version,
is_strict_cf=is_strict_conda_forge,
download=download,
)
config = Configuration(name=pkg_name, version=pkg_version, **kwargs)
return GrayskullFactory.create_recipe("pypi", config, pkg_name), config


Expand Down
5 changes: 5 additions & 0 deletions grayskull/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ def get_py_version_available(
f"sup_py {op} PyVer(int({major}), int({minor}))"
)
return py_ver_enabled

def __post_init__(self):
if self.url_pypi_metadata != "https://pypi.org/pypi/{pkg_name}/json":
preffix = "/" if not self.url_pypi_metadata.endswith("/") else ""
self.url_pypi_metadata += f"{preffix}{{pkg_name}}/json"
8 changes: 7 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
"conda-souschef >=2.1.0",
],
extras_require={
"testing": ["pytest", "mock", "pytest-cov", "pytest-console-scripts"]
"testing": [
"pytest",
"mock",
"pytest-cov",
"pytest-console-scripts",
"pytest-mock",
]
},
url="https://github.com/conda-incubator/grayskull",
license="MIT",
Expand Down
36 changes: 29 additions & 7 deletions tests/cli/test_cli_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
import pytest

import grayskull
from grayskull.__main__ import main
from grayskull import __main__ as cli
from grayskull.config import Configuration


def test_version(capsys):
main(["--version"])
cli.main(["--version"])
captured = capsys.readouterr()
assert captured.out.strip() == grayskull.__version__


def test_pypi_cmd(tmpdir):
out_folder = tmpdir.mkdir("out")
main(
cli.main(
["pypi", "pytest=5.3.2", "-o", str(out_folder), "-m", "m1", "m2", "--download"]
)
pytest_folder = out_folder / "pytest"
Expand All @@ -28,22 +29,22 @@ def test_pypi_cmd(tmpdir):

@pytest.mark.parametrize("option", ["--heman", "--shera"])
def test_easter(capsys, option):
main([option])
cli.main([option])
captured = capsys.readouterr()
assert "By the power of Grayskull..." in captured.out.strip()


def test_grayskull_without_options(capsys, monkeypatch):
monkeypatch.setattr(sys, "argv", ["foo"])
with pytest.raises(SystemExit) as pytest_wrapped_e:
main([])
cli.main([])
assert pytest_wrapped_e.type == SystemExit
captured = capsys.readouterr()
assert "Grayskull - Conda recipe generator" in captured.out


def test_msg_missing_pkg_pypi(capsys):
main(["pypi", "NOT_A_PACKAGE_123123123"])
cli.main(["pypi", "NOT_A_PACKAGE_123123123"])
captured = capsys.readouterr()
assert (
"Package seems to be missing."
Expand All @@ -55,5 +56,26 @@ def test_msg_missing_pkg_pypi(capsys):

def test_license_discovery(tmpdir):
out_folder = tmpdir.mkdir("out-license")
main(["pypi", "httplib2shim=0.0.3", "-o", str(out_folder)])
cli.main(["pypi", "httplib2shim=0.0.3", "-o", str(out_folder)])
assert (out_folder / "httplib2shim" / "LICENSE").exists()


def test_change_pypi_url(mocker):
mocker.patch("grayskull.__main__.generate_recipe", return_value=None)
mocker.patch(
"grayskull.__main__.create_python_recipe", return_value=({"extra": {}}, None)
)
spy = mocker.spy(cli, "create_python_recipe")

cli.main(["pypi", "pytest=5.3.2", "--pypi-url=http://url_pypi.com/abc"])
spy.assert_called_once_with(
"pytest=5.3.2",
is_strict_cf=False,
download=False,
url_pypi_metadata="http://url_pypi.com/abc",
)


def test_config_url_pypi_metadata():
config = Configuration("pytest", url_pypi_metadata="http://url_pypi.com/abc")
assert config.url_pypi_metadata == "http://url_pypi.com/abc/{pkg_name}/json"
6 changes: 3 additions & 3 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def test_pymc_recipe_fortran():


def test_pytest_recipe_entry_points():
recipe = create_python_recipe("pytest=5.3.5", is_strict_conda_forge=False)[0]
recipe = create_python_recipe("pytest=5.3.5", is_strict_cf=False)[0]
assert sorted(recipe["build"]["entry_points"]) == sorted(
["pytest=pytest:main", "py.test=pytest:main"]
)
Expand Down Expand Up @@ -678,7 +678,7 @@ def test_multiples_exit_setup():


def test_sequence_inside_another_in_dependencies():
recipe = create_python_recipe("unittest2=1.1.0")[0]
recipe = create_python_recipe("unittest2=1.1.0", is_strict_cf=True)[0]
assert recipe["requirements"]["host"] == [
"argparse",
"pip",
Expand Down Expand Up @@ -800,7 +800,7 @@ def test_replace_slash_in_imports():


def test_add_python_min_to_strict_conda_forge():
recipe = create_python_recipe("dgllife=0.2.8")[0]
recipe = create_python_recipe("dgllife=0.2.8", is_strict_cf=True)[0]
assert recipe["build"]["noarch"] == "python"
assert recipe["requirements"]["host"][1] == "python >=3.6"
assert "python >=3.6" in recipe["requirements"]["run"]

0 comments on commit 7acbee7

Please sign in to comment.