Skip to content

Commit 61ea828

Browse files
authored
Merge pull request #97 from nexB/fail_api_without_os_pyver
Make os and python version as mandatory input parameters #92
2 parents 0da2e84 + 9a8a92a commit 61ea828

File tree

7 files changed

+151
-45
lines changed

7 files changed

+151
-45
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ Changelog
22
=========
33

44

5+
v0.9.2
6+
------
7+
8+
- Make os and python version as mandatory input parameters.
9+
10+
511
v0.9.1
612
------
713

src/python_inspector/api.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
from python_inspector.resolution import get_package_list
3939
from python_inspector.resolution import get_python_version_from_env_tag
4040
from python_inspector.resolution import get_requirements_from_python_manifest
41+
from python_inspector.utils_pypi import PLATFORMS_BY_OS
4142
from python_inspector.utils_pypi import PYPI_SIMPLE_URL
43+
from python_inspector.utils_pypi import PYTHON_DOT_VERSIONS_BY_VER
4244
from python_inspector.utils_pypi import Environment
4345

4446

@@ -67,8 +69,8 @@ def resolve_dependencies(
6769
requirement_files=tuple(),
6870
setup_py_file=None,
6971
specifiers=tuple(),
70-
python_version=DEFAULT_PYTHON_VERSION,
71-
operating_system="linux",
72+
python_version=None,
73+
operating_system=None,
7274
index_urls=tuple([PYPI_SIMPLE_URL]),
7375
pdt_output=None,
7476
netrc_file=None,
@@ -93,6 +95,25 @@ def resolve_dependencies(
9395
to PyPI.org
9496
"""
9597

98+
if not operating_system:
99+
raise Exception(f"No operating system provided.")
100+
if operating_system not in PLATFORMS_BY_OS:
101+
raise ValueError(
102+
f"Invalid operating system: {operating_system}. "
103+
f"Must be one of: {', '.join(PLATFORMS_BY_OS.keys())}"
104+
)
105+
106+
valid_python_versions = list(PYTHON_DOT_VERSIONS_BY_VER.keys())
107+
valid_python_versions.extend([dot_ver for pyver, dot_ver in PYTHON_DOT_VERSIONS_BY_VER.items()])
108+
109+
if not python_version:
110+
raise Exception(f"No python version provided.")
111+
if python_version not in valid_python_versions:
112+
raise ValueError(
113+
f"Invalid python version: {python_version}. "
114+
f"Must be one of: {', '.join(valid_python_versions)}"
115+
)
116+
96117
if verbose:
97118
printer("Resolving dependencies...")
98119

src/python_inspector/resolve_cli.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def print_version(ctx, param, value):
7272
"python_version",
7373
type=click.Choice(utils_pypi.PYTHON_VERSIONS),
7474
metavar="PYVER",
75-
default=DEFAULT_PYTHON_VERSION,
7675
show_default=True,
7776
help="Python version to use for dependency resolution.",
7877
)
@@ -82,7 +81,6 @@ def print_version(ctx, param, value):
8281
"operating_system",
8382
type=click.Choice(utils_pypi.PLATFORMS_BY_OS),
8483
metavar="OS",
85-
default="linux",
8684
show_default=True,
8785
help="OS to use for dependency resolution.",
8886
)

tests/data/azure-devops.req-310-expected.json

Lines changed: 22 additions & 20 deletions
Large diffs are not rendered by default.

tests/data/azure-devops.req-38-expected.json

Lines changed: 22 additions & 20 deletions
Large diffs are not rendered by default.

tests/test_api.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import json
1313
import os
1414

15+
import pytest
1516
from commoncode.testcase import FileDrivenTesting
1617

1718
from python_inspector.resolve_cli import resolver_api
@@ -102,3 +103,37 @@ def test_api_with_prefer_source():
102103
expected_file=expected_file,
103104
clean=True,
104105
)
106+
107+
108+
def test_api_with_no_os():
109+
with pytest.raises(Exception) as e:
110+
resolver_api(
111+
specifiers=["flask==2.1.2"],
112+
python_version="3.10",
113+
)
114+
115+
116+
def test_api_with_no_pyver():
117+
with pytest.raises(Exception) as e:
118+
resolver_api(
119+
specifiers=["flask==2.1.2"],
120+
operating_system="linux",
121+
)
122+
123+
124+
def test_api_with_unsupported_os():
125+
with pytest.raises(ValueError) as e:
126+
resolver_api(
127+
specifiers=["flask==2.1.2"],
128+
python_version="3.10",
129+
operating_system="foo-bar",
130+
)
131+
132+
133+
def test_api_with_wrong_pyver():
134+
with pytest.raises(ValueError) as e:
135+
resolver_api(
136+
specifiers=["flask==2.1.2"],
137+
python_version="3.12",
138+
operating_system="linux",
139+
)

tests/test_cli.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,14 @@ def check_specs_resolution(
356356
)
357357

358358

359+
def get_os_and_pyver(options):
360+
if "--python-version" not in options:
361+
options.extend(["--python-version", "38"])
362+
if "--operating-system" not in options:
363+
options.extend(["--operating-system", "linux"])
364+
return options
365+
366+
359367
def test_passing_of_json_pdt_and_json_flags():
360368
result_file = test_env.get_temp_file("json")
361369
options = ["--specifier", "foo", "--json", result_file, "--json-pdt", result_file]
@@ -389,6 +397,38 @@ def test_passing_of_no_json_output_flag():
389397
run_cli(options=options, expected_rc=1)
390398

391399

400+
def test_passing_of_no_os():
401+
options = ["--specifier", "foo", "--json", "-", "--python-version", "38"]
402+
message = "No operating system"
403+
result = run_cli(options=options, expected_rc=1, get_env=False)
404+
if message:
405+
assert message in result.output
406+
407+
408+
def test_passing_of_no_pyver():
409+
options = ["--specifier", "foo", "--json", "-", "--operating-system", "linux"]
410+
message = "No python version"
411+
result = run_cli(options=options, expected_rc=1, get_env=False)
412+
if message:
413+
assert message in result.output
414+
415+
416+
def test_passing_of_wrong_pyver():
417+
options = ["--specifier", "foo", "--json", "-", "--python-version", "foo"]
418+
message = "Invalid value for '-p' / '--python-version'"
419+
result = run_cli(options=options, expected_rc=2, get_env=False)
420+
if message:
421+
assert message in result.output
422+
423+
424+
def test_passing_of_unsupported_os():
425+
options = ["--specifier", "foo", "--json", "-", "--operating-system", "bar"]
426+
message = "Invalid value for '-o' / '--operating-system'"
427+
result = run_cli(options=options, expected_rc=2, get_env=False)
428+
if message:
429+
assert message in result.output
430+
431+
392432
def check_requirements_resolution(
393433
requirements_file,
394434
expected_file,
@@ -480,7 +520,7 @@ def clean_results(results):
480520
return results
481521

482522

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

491531
runner = CliRunner()
532+
if get_env:
533+
options = get_os_and_pyver(options)
492534
result = runner.invoke(cli, options, catch_exceptions=False, env=env)
493535

494536
if result.exit_code != expected_rc:

0 commit comments

Comments
 (0)