Skip to content

Commit 33d4229

Browse files
committed
Add tests and address review comments
Signed-off-by: Tushar Goel <[email protected]>
1 parent 6fa577c commit 33d4229

File tree

3 files changed

+37
-24
lines changed

3 files changed

+37
-24
lines changed

src/python_inspector/resolution.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,9 @@ def pdt_dfs(mapping, graph, src):
641641
return dict(
642642
key=src, package_name=src, installed_version=str(mapping[src].version), dependencies=[]
643643
)
644-
# recurse
645-
dependencies = [pdt_dfs(mapping, graph, c) for c in children]
646-
dependencies.sort(key=lambda d: d["key"])
644+
# recurse
645+
dependencies = [pdt_dfs(mapping, graph, c) for c in children]
646+
dependencies.sort(key=lambda d: d["key"])
647647
return dict(
648648
key=src,
649649
package_name=src,
@@ -671,7 +671,7 @@ def get_resolved_dependencies(
671671
repos: Sequence[PypiSimpleRepository] = tuple(),
672672
as_tree: bool = False,
673673
max_rounds: int = 200000,
674-
debug: bool = False,
674+
verbose: bool = False,
675675
pdt_output: bool = False,
676676
):
677677
"""
@@ -694,7 +694,7 @@ def get_resolved_dependencies(
694694
resolver_results, as_tree=as_tree, environment=environment, repos=repos
695695
)
696696
except Exception as e:
697-
if debug:
697+
if verbose:
698698
import click
699699

700700
click.secho(f"{e!r}", err=True)

src/python_inspector/resolve_cli.py

+21-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#
1111

1212
import json
13-
import sys
1413
from typing import List
1514

1615
import click
@@ -34,6 +33,7 @@
3433

3534

3635
@click.command()
36+
@click.pass_context
3737
@click.option(
3838
"-r",
3939
"--requirement",
@@ -133,13 +133,14 @@
133133
"--index-url are ignored when this option is active.",
134134
)
135135
@click.option(
136-
"--debug",
136+
"--verbose",
137137
is_flag=True,
138138
hidden=True,
139139
help="Enable debug output.",
140140
)
141141
@click.help_option("-h", "--help")
142142
def resolve_dependencies(
143+
ctx,
143144
requirement_files,
144145
netrc_file,
145146
specifiers,
@@ -151,7 +152,7 @@ def resolve_dependencies(
151152
max_rounds,
152153
use_cached_index=False,
153154
use_pypi_json_api=False,
154-
debug=TRACE,
155+
verbose=TRACE,
155156
):
156157
"""
157158
Resolve the dependencies of the packages listed in REQUIREMENT-FILE(s) file
@@ -172,11 +173,14 @@ def resolve_dependencies(
172173
dad --spec "flask==2.1.2" --json -
173174
"""
174175
if not (json_output or pdt_output):
175-
if debug:
176-
click.secho("No output file specified. Use --json or --json-pdt.", err=True)
177-
return
176+
click.secho("No output file specified. Use --json or --json-pdt.", err=True)
177+
ctx.exit(1)
178178

179-
if debug:
179+
if json_output and pdt_output:
180+
click.secho("Only one of --json or --json-pdt can be used.", err=True)
181+
ctx.exit(1)
182+
183+
if verbose:
180184
click.secho(f"Resolving dependencies...")
181185

182186
netrc = None
@@ -199,11 +203,10 @@ def resolve_dependencies(
199203
direct_dependencies.append(dep)
200204

201205
if not direct_dependencies:
202-
if debug:
203-
click.secho("Error: no requirements requested.")
204-
sys.exit(1)
206+
click.secho("Error: no requirements requested.")
207+
ctx.exit(1)
205208

206-
if debug:
209+
if verbose:
207210
click.secho("direct_dependencies:")
208211
for dep in direct_dependencies:
209212
click.secho(f" {dep}")
@@ -213,7 +216,7 @@ def resolve_dependencies(
213216
python_version=python_version, operating_system=operating_system
214217
)
215218

216-
if debug:
219+
if verbose:
217220
click.secho(f"environment: {environment}")
218221

219222
repos = []
@@ -239,7 +242,7 @@ def resolve_dependencies(
239242
)
240243
repos.append(repo)
241244

242-
if debug:
245+
if verbose:
243246
click.secho("repos:")
244247
for repo in repos:
245248
click.secho(f" {repo}")
@@ -251,7 +254,7 @@ def resolve_dependencies(
251254
repos=repos,
252255
as_tree=False,
253256
max_rounds=max_rounds,
254-
debug=debug,
257+
verbose=verbose,
255258
pdt_output=pdt_output,
256259
)
257260

@@ -286,7 +289,7 @@ def resolve_dependencies(
286289
json_output=json_output,
287290
)
288291

289-
if pdt_output:
292+
else:
290293
write_output(
291294
headers=headers,
292295
requirements=requirements,
@@ -295,7 +298,7 @@ def resolve_dependencies(
295298
pdt_output=True,
296299
)
297300

298-
if debug:
301+
if verbose:
299302
click.secho("done!")
300303

301304

@@ -305,7 +308,7 @@ def resolve(
305308
repos=tuple(),
306309
as_tree=False,
307310
max_rounds=200000,
308-
debug=False,
311+
verbose=False,
309312
pdt_output=False,
310313
):
311314
"""
@@ -324,11 +327,10 @@ def resolve(
324327
repos=repos,
325328
as_tree=as_tree,
326329
max_rounds=max_rounds,
327-
debug=debug,
330+
verbose=verbose,
328331
pdt_output=pdt_output,
329332
)
330333

331-
332334
initial_requirements = [d.to_dict() for d in direct_dependencies]
333335

334336
return initial_requirements, resolved_dependencies

tests/test_cli.py

+11
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,17 @@ def check_specs_resolution(
206206
)
207207

208208

209+
def test_passing_of_json_pdt_and_json_flags():
210+
result_file = test_env.get_temp_file("json")
211+
options = ["--specifier", "foo", "--json", result_file, "--json-pdt", result_file]
212+
run_cli(options=options, expected_rc=1)
213+
214+
215+
def test_passing_of_no_json_output_flag():
216+
options = ["--specifier", "foo"]
217+
run_cli(options=options, expected_rc=1)
218+
219+
209220
def check_requirements_resolution(
210221
requirements_file,
211222
expected_file,

0 commit comments

Comments
 (0)