Skip to content

Make os and python version as mandatory input parameters #92 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Changelog
=========


v0.9.2
------

- Make os and python version as mandatory input parameters.


v0.9.1
------

Expand Down
25 changes: 23 additions & 2 deletions src/python_inspector/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
from python_inspector.resolution import get_package_list
from python_inspector.resolution import get_python_version_from_env_tag
from python_inspector.resolution import get_requirements_from_python_manifest
from python_inspector.utils_pypi import PLATFORMS_BY_OS
from python_inspector.utils_pypi import PYPI_SIMPLE_URL
from python_inspector.utils_pypi import PYTHON_DOT_VERSIONS_BY_VER
from python_inspector.utils_pypi import Environment


Expand Down Expand Up @@ -67,8 +69,8 @@ def resolve_dependencies(
requirement_files=tuple(),
setup_py_file=None,
specifiers=tuple(),
python_version=DEFAULT_PYTHON_VERSION,
operating_system="linux",
python_version=None,
operating_system=None,
index_urls=tuple([PYPI_SIMPLE_URL]),
pdt_output=None,
netrc_file=None,
Expand All @@ -93,6 +95,25 @@ def resolve_dependencies(
to PyPI.org
"""

if not operating_system:
raise Exception(f"No operating system provided.")
if operating_system not in PLATFORMS_BY_OS:
raise ValueError(
f"Invalid operating system: {operating_system}. "
f"Must be one of: {', '.join(PLATFORMS_BY_OS.keys())}"
)

valid_python_versions = list(PYTHON_DOT_VERSIONS_BY_VER.keys())
valid_python_versions.extend([dot_ver for pyver, dot_ver in PYTHON_DOT_VERSIONS_BY_VER.items()])

if not python_version:
raise Exception(f"No python version provided.")
if python_version not in valid_python_versions:
raise ValueError(
f"Invalid python version: {python_version}. "
f"Must be one of: {', '.join(valid_python_versions)}"
)

if verbose:
printer("Resolving dependencies...")

Expand Down
2 changes: 0 additions & 2 deletions src/python_inspector/resolve_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def print_version(ctx, param, value):
"python_version",
type=click.Choice(utils_pypi.PYTHON_VERSIONS),
metavar="PYVER",
default=DEFAULT_PYTHON_VERSION,
show_default=True,
help="Python version to use for dependency resolution.",
)
Expand All @@ -82,7 +81,6 @@ def print_version(ctx, param, value):
"operating_system",
type=click.Choice(utils_pypi.PLATFORMS_BY_OS),
metavar="OS",
default="linux",
show_default=True,
help="OS to use for dependency resolution.",
)
Expand Down
42 changes: 22 additions & 20 deletions tests/data/azure-devops.req-310-expected.json

Large diffs are not rendered by default.

42 changes: 22 additions & 20 deletions tests/data/azure-devops.req-38-expected.json

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import json
import os

import pytest
from commoncode.testcase import FileDrivenTesting

from python_inspector.resolve_cli import resolver_api
Expand Down Expand Up @@ -102,3 +103,37 @@ def test_api_with_prefer_source():
expected_file=expected_file,
clean=True,
)


def test_api_with_no_os():
with pytest.raises(Exception) as e:
resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
)


def test_api_with_no_pyver():
with pytest.raises(Exception) as e:
resolver_api(
specifiers=["flask==2.1.2"],
operating_system="linux",
)


def test_api_with_unsupported_os():
with pytest.raises(ValueError) as e:
resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.10",
operating_system="foo-bar",
)


def test_api_with_wrong_pyver():
with pytest.raises(ValueError) as e:
resolver_api(
specifiers=["flask==2.1.2"],
python_version="3.12",
operating_system="linux",
)
44 changes: 43 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ def check_specs_resolution(
)


def get_os_and_pyver(options):
if "--python-version" not in options:
options.extend(["--python-version", "38"])
if "--operating-system" not in options:
options.extend(["--operating-system", "linux"])
return options


def test_passing_of_json_pdt_and_json_flags():
result_file = test_env.get_temp_file("json")
options = ["--specifier", "foo", "--json", result_file, "--json-pdt", result_file]
Expand Down Expand Up @@ -389,6 +397,38 @@ def test_passing_of_no_json_output_flag():
run_cli(options=options, expected_rc=1)


def test_passing_of_no_os():
options = ["--specifier", "foo", "--json", "-", "--python-version", "38"]
message = "No operating system"
result = run_cli(options=options, expected_rc=1, get_env=False)
if message:
assert message in result.output


def test_passing_of_no_pyver():
options = ["--specifier", "foo", "--json", "-", "--operating-system", "linux"]
message = "No python version"
result = run_cli(options=options, expected_rc=1, get_env=False)
if message:
assert message in result.output


def test_passing_of_wrong_pyver():
options = ["--specifier", "foo", "--json", "-", "--python-version", "foo"]
message = "Invalid value for '-p' / '--python-version'"
result = run_cli(options=options, expected_rc=2, get_env=False)
if message:
assert message in result.output


def test_passing_of_unsupported_os():
options = ["--specifier", "foo", "--json", "-", "--operating-system", "bar"]
message = "Invalid value for '-o' / '--operating-system'"
result = run_cli(options=options, expected_rc=2, get_env=False)
if message:
assert message in result.output


def check_requirements_resolution(
requirements_file,
expected_file,
Expand Down Expand Up @@ -480,7 +520,7 @@ def clean_results(results):
return results


def run_cli(options, cli=resolve_dependencies, expected_rc=0, env=None):
def run_cli(options, cli=resolve_dependencies, expected_rc=0, env=None, get_env=True):
"""
Run a command line resolution. Return a click.testing.Result object.
"""
Expand All @@ -489,6 +529,8 @@ def run_cli(options, cli=resolve_dependencies, expected_rc=0, env=None):
env = dict(os.environ)

runner = CliRunner()
if get_env:
options = get_os_and_pyver(options)
result = runner.invoke(cli, options, catch_exceptions=False, env=env)

if result.exit_code != expected_rc:
Expand Down