diff --git a/environment.yaml b/environment.yaml index 64a8af71..e134f0a8 100644 --- a/environment.yaml +++ b/environment.yaml @@ -8,6 +8,7 @@ dependencies: - pytest-cov - pytest-xdist - pytest-forked + - pytest-mock - requests - ruamel.yaml >=0.16.10 - ruamel.yaml.jinja2 diff --git a/grayskull/__main__.py b/grayskull/__main__.py index e275e22e..29e823e3 100644 --- a/grayskull/__main__.py +++ b/grayskull/__main__.py @@ -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) @@ -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") @@ -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 diff --git a/grayskull/config.py b/grayskull/config.py index da5378d1..594d6d7e 100644 --- a/grayskull/config.py +++ b/grayskull/config.py @@ -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" diff --git a/setup.py b/setup.py index 2d3d7196..c89e5542 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tests/cli/test_cli_cmds.py b/tests/cli/test_cli_cmds.py index c9f36f5f..50305616 100644 --- a/tests/cli/test_cli_cmds.py +++ b/tests/cli/test_cli_cmds.py @@ -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" @@ -28,7 +29,7 @@ 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() @@ -36,14 +37,14 @@ def test_easter(capsys, option): 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." @@ -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" diff --git a/tests/test_pypi.py b/tests/test_pypi.py index 9887bcb5..abf412b2 100644 --- a/tests/test_pypi.py +++ b/tests/test_pypi.py @@ -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"] ) @@ -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", @@ -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"]