Skip to content

Commit 04c5b69

Browse files
committed
Fix Settings.pyenvs to _actually_ use basepath by default
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.
1 parent be884e1 commit 04c5b69

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

fawltydeps/packages.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def pyenv_sources(*pyenv_paths: Path) -> Set[PyEnvSource]:
342342
for path in pyenv_paths:
343343
package_dirs = set(LocalPackageResolver.find_package_dirs(path))
344344
if not package_dirs:
345-
logger.warning(f"Could not find a Python env at {path}!")
345+
logger.debug(f"Could not find a Python env at {path}!")
346346
ret.update(PyEnvSource(d) for d in package_dirs)
347347
if pyenv_paths and not ret:
348348
raise ValueError(f"Could not find any Python env in {pyenv_paths}!")

fawltydeps/settings.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Settings(BaseSettings): # type: ignore
110110
output_format: OutputFormat = OutputFormat.HUMAN_SUMMARY
111111
code: Set[PathOrSpecial] = {Path(".")}
112112
deps: Set[Path] = {Path(".")}
113-
pyenvs: Set[Path] = set()
113+
pyenvs: Set[Path] = {Path(".")}
114114
custom_mapping: Optional[CustomMapping] = None
115115
ignore_undeclared: Set[str] = set()
116116
ignore_unused: Set[str] = set()
@@ -189,11 +189,17 @@ def create(cls, cmdline_args: argparse.Namespace) -> "Settings":
189189
if base_paths:
190190
code_paths = args_dict.setdefault("code", base_paths)
191191
deps_paths = args_dict.setdefault("deps", base_paths)
192-
if code_paths != base_paths and deps_paths != base_paths:
192+
pyenv_paths = args_dict.setdefault("pyenvs", base_paths)
193+
if (
194+
code_paths != base_paths
195+
and deps_paths != base_paths
196+
and pyenv_paths != base_paths
197+
):
193198
msg = (
194-
"All three path specifications (code, deps, and base)"
195-
f"have been used. Use at most 2. basepaths={base_paths}, "
196-
f"code_paths={code_paths}, deps_paths={deps_paths}"
199+
"All four path specifications (code, deps, pyenvs, and base)"
200+
f"have been used. Use at most 3. basepaths={base_paths}, "
201+
f"code_paths={code_paths}, deps_paths={deps_paths}, "
202+
f"pyenv_paths={pyenv_paths}"
197203
)
198204
raise argparse.ArgumentError(argument=None, message=msg)
199205

tests/test_cmdline.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def make_json_settings_dict(**kwargs):
3535
"actions": ["check_undeclared", "check_unused"],
3636
"code": ["."],
3737
"deps": ["."],
38-
"pyenvs": [],
38+
"pyenvs": ["."],
3939
"custom_mapping": None,
4040
"output_format": "human_summary",
4141
"ignore_undeclared": [],
@@ -850,7 +850,7 @@ def test_cmdline_on_ignored_undeclared_option(
850850
output_format = 'human_detailed'
851851
# code = ['.']
852852
deps = ['foobar']
853-
# pyenvs = []
853+
# pyenvs = ['.']
854854
# ignore_undeclared = []
855855
# ignore_unused = []
856856
# deps_parser_choice = ...

tests/test_settings.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
actions={Action.REPORT_UNDECLARED, Action.REPORT_UNUSED},
4040
code={Path(".")},
4141
deps={Path(".")},
42-
pyenvs=set(),
42+
pyenvs={Path(".")},
4343
custom_mapping_file=set(),
4444
custom_mapping=None,
4545
output_format=OutputFormat.HUMAN_SUMMARY,
@@ -81,22 +81,23 @@ def _inner(**kwargs: str):
8181

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

8888

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

9696

9797
path_options = { # options (-> settings members) that interact with basepath
9898
"--code": "code",
9999
"--deps": "deps",
100+
"--pyenv": "pyenvs",
100101
}
101102

102103
Item = TypeVar("Item")
@@ -151,13 +152,19 @@ def test_base_path_fills_path_options_when_other_path_settings_are_absent(basepa
151152
pytest.param(conf_sett, base, id=test_name)
152153
for conf_sett, base, test_name in [
153154
(None, {"single-base"}, "empty-config"),
154-
(dict(code=["test-code"]), {"base1", "base2"}, "only-code-set"),
155-
(dict(deps=["deps-test"]), {"single-base"}, "only-deps-set"),
155+
({"code": ["test-code"]}, {"base1", "base2"}, "only-code-set"),
156+
({"deps": ["deps-test"]}, {"single-base"}, "only-deps-set"),
157+
({"pyenvs": ["pyenvs-test"]}, {"single-base"}, "only-pyenvs-set"),
156158
(
157-
dict(code=["code-test"], deps=["test-deps"]),
159+
{"code": ["code-test"], "deps": ["test-deps"]},
158160
{"base1", "base2"},
159161
"code-and-deps-set",
160162
),
163+
(
164+
{"code": ["code-test"], "deps": ["test-deps"], "pyenvs": ["abc"]},
165+
{"base1", "base2"},
166+
"all-three-set",
167+
),
161168
]
162169
],
163170
)

0 commit comments

Comments
 (0)