Skip to content

Commit

Permalink
add test requirements and pytest command
Browse files Browse the repository at this point in the history
  • Loading branch information
woutdenolf committed Apr 26, 2022
1 parent 78317fe commit f8cd4bc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
7 changes: 7 additions & 0 deletions grayskull/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ def main(args=None):
help="If sections are specified, grayskull will populate just the sections "
"informed.",
)
pypi_cmds.add_argument(
"--extras-require-test",
default="",
dest="extras_require_test",
help="Extra requirements to run tests.",
)

args = parser.parse_args(args)

Expand Down Expand Up @@ -175,6 +181,7 @@ def generate_recipes_from_list(list_pkgs, args):
url_pypi_metadata=args.url_pypi_metadata,
sections_populate=args.sections_populate,
from_local_sdist=from_local_sdist,
extras_require_test=args.extras_require_test,
)
except requests.exceptions.HTTPError as err:
print_msg(f"{Fore.RED}Package seems to be missing.\nException: {err}\n\n")
Expand Down
1 change: 1 addition & 0 deletions grayskull/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Configuration:
from_local_sdist: bool = False
local_sdist: Optional[str] = None
missing_deps: set = field(default_factory=set)
extras_require_test: Optional[str] = None

def get_oldest_py3_version(self, list_py_ver: List[PyVer]) -> PyVer:
list_py_ver = sorted(list_py_ver)
Expand Down
15 changes: 15 additions & 0 deletions grayskull/strategy/py_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ def get_test_imports(metadata: dict, default: Optional[str] = None) -> List:
return result


def get_test_requirements(metadata: Dict, extras_require_test: Optional[str]) -> List:
"""Extract test requirements from metadata
:param metadata:
:param extras_require_test: `extras_require` option for test requirements
:return: list of test requirements
"""
if not extras_require_test:
return list()
try:
return metadata["extras_require"][extras_require_test]
except KeyError:
return list()


def get_entry_points_from_sdist(sdist_metadata: dict) -> List:
"""Extract entry points from sdist metadata
Expand Down
15 changes: 12 additions & 3 deletions grayskull/strategy/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
get_sdist_metadata,
get_test_entry_points,
get_test_imports,
get_test_requirements,
parse_extra_metadata_to_selector,
py_version_to_limit_python,
py_version_to_selector,
Expand Down Expand Up @@ -385,16 +386,24 @@ def get_metadata(recipe, config) -> dict:

all_missing_deps = print_requirements(all_requirements)
config.missing_deps = all_missing_deps
test_entry_points = get_test_entry_points(metadata.get("entry_points", []))
test_imports = get_test_imports(metadata, metadata["name"])
test_commands = ["pip check"]
test_requirements = ["pip"]
test_requirements.extend(
get_test_requirements(metadata, config.extras_require_test)
)
if "pytest" in test_requirements:
for module in test_imports:
test_commands.append("pytest --pyargs " + module)
test_commands.extend(get_test_entry_points(metadata.get("entry_points", [])))
return {
"package": {"name": name, "version": metadata["version"]},
"build": {"entry_points": metadata.get("entry_points")},
"requirements": all_requirements,
"test": {
"imports": test_imports,
"commands": ["pip check"] + test_entry_points,
"requires": ["pip"],
"commands": test_commands,
"requires": test_requirements,
},
"about": {
"home": metadata["url"]
Expand Down
21 changes: 21 additions & 0 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,27 @@ def test_django_rest_framework_xml_license():
assert recipe["test"]["imports"][0] == "rest_framework_xml"


def test_get_test_requirements():
config = Configuration(name="ewokscore", version="0.1.0rc5")
recipe = GrayskullFactory.create_recipe("pypi", config)
assert "pytest" not in recipe["test"]["requires"]
assert "pytest --pyargs ewokscore" not in recipe["test"]["commands"]

config = Configuration(
name="ewokscore", version="0.1.0rc5", extras_require_test="wrongoption"
)
recipe = GrayskullFactory.create_recipe("pypi", config)
assert "pytest" not in recipe["test"]["requires"]
assert "pytest --pyargs ewokscore" not in recipe["test"]["commands"]

config = Configuration(
name="ewokscore", version="0.1.0rc5", extras_require_test="test"
)
recipe = GrayskullFactory.create_recipe("pypi", config)
assert "pytest" in recipe["test"]["requires"]
assert "pytest --pyargs ewokscore" in recipe["test"]["commands"]


def test_get_test_imports():
assert get_test_imports({"packages": ["pkg", "pkg.mod1", "pkg.mod2"]}) == ["pkg"]
assert get_test_imports({"packages": None}, default="pkg-mod") == ["pkg_mod"]
Expand Down

0 comments on commit f8cd4bc

Please sign in to comment.