Skip to content

Commit

Permalink
Test the option value derivation details. (#21628)
Browse files Browse the repository at this point in the history
Previously we only checked that the detail text was as expected
for cli flags, now we check for env vars and config too.

Also, fix the implementation so it actually passes the test as expected.
  • Loading branch information
benjyw authored Nov 12, 2024
1 parent 89e8819 commit dc3f0ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/python/pants/help/help_info_extracter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,11 @@ def get_option_scope_help_info(
# Again, distinguishing between '' vs None in the details field
# does not matter, but we want to be consistent with the idiosyncratic
# behavior of the legacy parser, until we get rid of it.
details = rank.description() or empty_details
details = (
", ".join(filter(None, [r.description() for r in ranks])) or empty_details
)
ranked_values.append(RankedValue(rank, value, details))
history = OptionValueHistory(tuple(ranked_values))
# history = parser.history(kwargs["dest"])
ohi = self.get_option_help_info(args, kwargs)
ohi = dataclasses.replace(ohi, value_history=history)
if ohi.deprecation_active:
Expand Down
47 changes: 29 additions & 18 deletions src/python/pants/help/help_info_extracter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from enum import Enum
from types import SimpleNamespace
from typing import Any, Iterable, List, Optional, Tuple, Union

from pants.base.build_environment import get_buildroot
Expand All @@ -15,7 +16,7 @@
from pants.option.config import Config
from pants.option.global_options import GlobalOptions, LogLevelOption
from pants.option.native_options import NativeOptionParser
from pants.option.option_types import BoolOption, IntOption, StrListOption
from pants.option.option_types import BoolOption, IntListOption, StrListOption
from pants.option.options import Options
from pants.option.parser import Parser
from pants.option.ranked_value import Rank
Expand Down Expand Up @@ -217,12 +218,12 @@ def exp_to_len(exp):
do_test({"advanced": True}, expected_advanced=True)


def test_get_all_help_info():
def test_get_all_help_info(tmp_path) -> None:
class Global(Subsystem):
options_scope = GLOBAL_SCOPE
help = help_text("Global options.")

opt1 = IntOption(default=42, help="Option 1")
opt1 = IntListOption(default=[42], help="Option 1")
# This is special in having a short option `-l`. Make sure it works.
level = LogLevelOption()

Expand Down Expand Up @@ -256,11 +257,13 @@ class BazLibrary(Target):
alias = "baz_library"
help = "A library of baz-es.\n\nUse it however you like."

core_fields = [QuxField, QuuxField]
core_fields = (QuxField, QuuxField)

config_path = "pants.test.toml"
config_source = SimpleNamespace(path=config_path, content=b"[GLOBAL]\nopt1 = '+[99]'")
options = Options.create(
env={},
config=Config.load([]),
env={"PANTS_OPT1": "88"},
config=Config.load([config_source]),
native_options_config_discovery=False,
known_scope_infos=[Global.get_scope_info(), Foo.get_scope_info(), Bar.get_scope_info()],
args=["./pants", "--backend-packages=['internal_plugins.releases']"],
Expand Down Expand Up @@ -309,20 +312,24 @@ def fake_consumed_scopes_mapper(scope: str) -> Tuple[str, ...]:
"deprecated_scope": None,
"basic": (
{
"display_args": ("--opt1=<int>",),
"comma_separated_display_args": "--opt1=<int>",
"display_args": ('--opt1="[<int>, <int>, ...]"',),
"comma_separated_display_args": '--opt1="[<int>, <int>, ...]"',
"scoped_cmd_line_args": ("--opt1",),
"unscoped_cmd_line_args": ("--opt1",),
"config_key": "opt1",
"env_var": "PANTS_OPT1",
"value_history": {
"ranked_values": (
{"rank": Rank.NONE, "value": None, "details": None},
{"rank": Rank.HARDCODED, "value": 42, "details": None},
{"rank": Rank.NONE, "value": [], "details": ""},
{
"rank": Rank.ENVIRONMENT,
"value": [42, 99, 88],
"details": "from config, from an env var",
},
),
},
"typ": int,
"default": 42,
"typ": list,
"default": [42],
"fromfile": False,
"help": "Option 1",
"deprecation_active": False,
Expand Down Expand Up @@ -628,20 +635,24 @@ def fake_consumed_scopes_mapper(scope: str) -> Tuple[str, ...]:
},
"env_var_to_help_info": {
"PANTS_OPT1": {
"display_args": ("--opt1=<int>",),
"comma_separated_display_args": "--opt1=<int>",
"display_args": ('--opt1="[<int>, <int>, ...]"',),
"comma_separated_display_args": '--opt1="[<int>, <int>, ...]"',
"scoped_cmd_line_args": ("--opt1",),
"unscoped_cmd_line_args": ("--opt1",),
"config_key": "opt1",
"env_var": "PANTS_OPT1",
"value_history": {
"ranked_values": (
{"rank": Rank.NONE, "value": None, "details": None},
{"rank": Rank.HARDCODED, "value": 42, "details": None},
{"rank": Rank.NONE, "value": [], "details": ""},
{
"rank": Rank.ENVIRONMENT,
"value": [42, 99, 88],
"details": "from config, from an env var",
},
),
},
"typ": int,
"default": 42,
"typ": list,
"default": [42],
"fromfile": False,
"help": "Option 1",
"deprecation_active": False,
Expand Down

0 comments on commit dc3f0ac

Please sign in to comment.