Skip to content

Commit

Permalink
Fix Settings.pyenvs to _actually_ use basepath by default
Browse files Browse the repository at this point in the history
This is a rather embarrasing omission from PR #326 (Teach FawltyDeps to
automatically discover Python environments inside the project):

Although that PR did include code to traverse within a given --pyenv
path to find Python environments, the PR "forgot" to actually switch the
_default_ behavior of --pyenv:

The default value of settings.pyenvs remained an empty set, and there
were no changes to have basepath influence the value of settings.pyenvs.

This commit fixes that:

- When neither --pyenv nor basepath is given, settings.pyenvs should
  default to the current directory.
- When --pyenv is not given, but basepath is given, basepath should
  override the default settings.pyenvs.
- When --pyenv is given, it overrides basepath.

In short settings.pyenvs should behave exactly like .code and .deps.
  • Loading branch information
jherland committed Jul 28, 2023
1 parent 6462f8a commit 804a832
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion fawltydeps/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def pyenv_sources(*pyenv_paths: Path) -> Set[PyEnvSource]:
for path in pyenv_paths:
package_dirs = set(LocalPackageResolver.find_package_dirs(path))
if not package_dirs:
logger.warning(f"Could not find a Python env at {path}!")
logger.debug(f"Could not find a Python env at {path}!")
ret.update(PyEnvSource(d) for d in package_dirs)
if pyenv_paths and not ret:
raise ValueError(f"Could not find any Python env in {pyenv_paths}!")
Expand Down
16 changes: 11 additions & 5 deletions fawltydeps/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Settings(BaseSettings): # type: ignore
output_format: OutputFormat = OutputFormat.HUMAN_SUMMARY
code: Set[PathOrSpecial] = {Path(".")}
deps: Set[Path] = {Path(".")}
pyenvs: Set[Path] = set()
pyenvs: Set[Path] = {Path(".")}
custom_mapping: Optional[CustomMapping] = None
ignore_undeclared: Set[str] = set()
ignore_unused: Set[str] = set()
Expand Down Expand Up @@ -189,11 +189,17 @@ def create(cls, cmdline_args: argparse.Namespace) -> "Settings":
if base_paths:
code_paths = args_dict.setdefault("code", base_paths)
deps_paths = args_dict.setdefault("deps", base_paths)
if code_paths != base_paths and deps_paths != base_paths:
pyenv_paths = args_dict.setdefault("pyenvs", base_paths)
if (
code_paths != base_paths
and deps_paths != base_paths
and pyenv_paths != base_paths
):
msg = (
"All three path specifications (code, deps, and base)"
f"have been used. Use at most 2. basepaths={base_paths}, "
f"code_paths={code_paths}, deps_paths={deps_paths}"
"All four path specifications (code, deps, pyenvs, and base)"
f"have been used. Use at most 3. basepaths={base_paths}, "
f"code_paths={code_paths}, deps_paths={deps_paths}, "
f"pyenv_paths={pyenv_paths}"
)
raise argparse.ArgumentError(argument=None, message=msg)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def make_json_settings_dict(**kwargs):
"actions": ["check_undeclared", "check_unused"],
"code": ["."],
"deps": ["."],
"pyenvs": [],
"pyenvs": ["."],
"custom_mapping": None,
"output_format": "human_summary",
"ignore_undeclared": [],
Expand Down Expand Up @@ -850,7 +850,7 @@ def test_cmdline_on_ignored_undeclared_option(
output_format = 'human_detailed'
# code = ['.']
deps = ['foobar']
# pyenvs = []
# pyenvs = ['.']
# ignore_undeclared = []
# ignore_unused = []
# deps_parser_choice = ...
Expand Down
29 changes: 18 additions & 11 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
actions={Action.REPORT_UNDECLARED, Action.REPORT_UNUSED},
code={Path(".")},
deps={Path(".")},
pyenvs=set(),
pyenvs={Path(".")},
custom_mapping_file=set(),
custom_mapping=None,
output_format=OutputFormat.HUMAN_SUMMARY,
Expand Down Expand Up @@ -81,22 +81,23 @@ def _inner(**kwargs: str):

safe_string = strategies.text(alphabet=string.ascii_letters + string.digits, min_size=1)
nonempty_string_set = strategies.sets(safe_string, min_size=1)
three_different_string_groups = strategies.tuples(
nonempty_string_set, nonempty_string_set, nonempty_string_set
).filter(lambda ss: ss[0] != ss[1] and ss[0] != ss[2] and ss[1] != ss[2])
four_different_string_groups = strategies.tuples(
*([nonempty_string_set] * 4),
).filter(lambda ss: all(a != b for a, b in combinations(ss, 2)))


@given(code_deps_base=three_different_string_groups)
def test_code_deps_and_base_unequal__raises_error(code_deps_base):
code, deps, base = code_deps_base
args = list(base) + ["--code"] + list(code) + ["--deps"] + list(deps)
@given(code_deps_pyenvs_base=four_different_string_groups)
def test_code_deps_pyenvs_and_base_unequal__raises_error(code_deps_pyenvs_base):
code, deps, pyenvs, base = code_deps_pyenvs_base
args = [*base, "--code", *code, "--deps", *deps, "--pyenv", *pyenvs]
with pytest.raises(argparse.ArgumentError):
run_build_settings(args)


path_options = { # options (-> settings members) that interact with basepath
"--code": "code",
"--deps": "deps",
"--pyenv": "pyenvs",
}

Item = TypeVar("Item")
Expand Down Expand Up @@ -151,13 +152,19 @@ def test_base_path_fills_path_options_when_other_path_settings_are_absent(basepa
pytest.param(conf_sett, base, id=test_name)
for conf_sett, base, test_name in [
(None, {"single-base"}, "empty-config"),
(dict(code=["test-code"]), {"base1", "base2"}, "only-code-set"),
(dict(deps=["deps-test"]), {"single-base"}, "only-deps-set"),
({"code": ["test-code"]}, {"base1", "base2"}, "only-code-set"),
({"deps": ["deps-test"]}, {"single-base"}, "only-deps-set"),
({"pyenvs": ["pyenvs-test"]}, {"single-base"}, "only-pyenvs-set"),
(
dict(code=["code-test"], deps=["test-deps"]),
{"code": ["code-test"], "deps": ["test-deps"]},
{"base1", "base2"},
"code-and-deps-set",
),
(
{"code": ["code-test"], "deps": ["test-deps"], "pyenvs": ["abc"]},
{"base1", "base2"},
"all-three-set",
),
]
],
)
Expand Down

0 comments on commit 804a832

Please sign in to comment.