Skip to content

Commit

Permalink
Change type of custom mapping files to a Optional[Set[Path]]
Browse files Browse the repository at this point in the history
Works for a single input
  • Loading branch information
mknorps committed Apr 17, 2023
1 parent 2204b74 commit 9cb1629
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
2 changes: 2 additions & 0 deletions fawltydeps/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def populate_parser_paths_options(parser: argparse._ActionsContainer) -> None:
)
parser.add_argument(
"--custom-mapping-file",
nargs="+",
action="union",
type=Path,
metavar="FILE_PATH",
help=(
Expand Down
31 changes: 17 additions & 14 deletions fawltydeps/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ class UserDefinedMapping(BasePackageResolver):

def __init__(
self,
mapping_path: Optional[Path] = None,
mapping_paths: Optional[Set[Path]] = None,
custom_mapping: Optional[CustomMapping] = None,
) -> None:
self.mapping_path = mapping_path
if self.mapping_path and not self.mapping_path.is_file():
raise UnparseablePathException(
ctx="Given mapping path is not a file.", path=self.mapping_path
)
self.mapping_paths = mapping_paths
if self.mapping_paths:
for path in self.mapping_paths:
if not path.is_file():
raise UnparseablePathException(
ctx="Given mapping path is not a file.", path=path
)
self.custom_mapping = custom_mapping
# We enumerate packages declared in the mapping _once_ and cache the result here:
self._packages: Optional[Dict[str, Package]] = None
Expand All @@ -134,18 +136,19 @@ def packages(self) -> Dict[str, Package]:
"""Gather a custom mapping given by a user.
Mapping may come from two sources:
* _mapping_path_ which is a file in a given path, which is parsed to a ditionary
* _mapping_paths_ which is a set of files in a given path, which is parsed to a ditionary
* _custom_mapping_ which is a dictionary of package to imports mapping.
This enumerates the available packages _once_, and caches the result for
the remainder of this object's life in _packages.
"""
custom_mapping_from_file: Dict[str, List[str]] = {}

if self.mapping_path is not None:
logger.debug(f"Loading user-defined mapping from {self.mapping_path}")
with open(self.mapping_path, "rb") as mapping_file:
custom_mapping_from_file = tomllib.load(mapping_file)
if self.mapping_paths is not None:
logger.debug(f"Loading user-defined mapping from {self.mapping_paths}")
for path in self.mapping_paths:
with open(path, "rb") as mapping_file:
custom_mapping_from_file = tomllib.load(mapping_file)

mapping = {
Package.normalize_name(name): Package(
Expand All @@ -166,7 +169,7 @@ def packages(self) -> Dict[str, Package]:
"from the configuration file are appended to ones "
"found in the mapping file.",
normalised_name,
self.mapping_path,
self.mapping_paths,
)
mapping[normalised_name].add_import_names(
*imports, mapping=DependenciesMapping.USER_DEFINED
Expand Down Expand Up @@ -354,7 +357,7 @@ def lookup_packages(self, package_names: Set[str]) -> Dict[str, Package]:

def resolve_dependencies(
dep_names: Iterable[str],
custom_mapping_file: Optional[Path] = None,
custom_mapping_file: Optional[Set[Path]] = None,
custom_mapping: Optional[CustomMapping] = None,
pyenv_path: Optional[Path] = None,
install_deps: bool = False,
Expand All @@ -378,7 +381,7 @@ def resolve_dependencies(
if custom_mapping_file or custom_mapping:
resolvers.append(
UserDefinedMapping(
mapping_path=custom_mapping_file, custom_mapping=custom_mapping
mapping_paths=custom_mapping_file, custom_mapping=custom_mapping
)
)

Expand Down
2 changes: 1 addition & 1 deletion fawltydeps/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Settings(BaseSettings): # type: ignore
code: Set[PathOrSpecial] = {Path(".")}
deps: Set[Path] = {Path(".")}
pyenv: Optional[Path] = None
custom_mapping_file: Optional[Path] = None
custom_mapping_file: Optional[Set[Path]] = None
custom_mapping: Optional[CustomMapping] = None
ignore_undeclared: Set[str] = set()
ignore_unused: Set[str] = set()
Expand Down
9 changes: 5 additions & 4 deletions tests/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ def test_user_defined_mapping__well_formated_input_file__parses_correctly(
)
)

udm = UserDefinedMapping(custom_mapping_file)
udm = UserDefinedMapping({custom_mapping_file})
mapped_packages = udm.packages
assert set(mapped_packages.keys()) == {"apache_airflow", "attrs"}


def test_user_defined_mapping__input_is_no_file__raises_unparsable_path_exeption():
with pytest.raises(UnparseablePathException):
UserDefinedMapping(SAMPLE_PROJECTS_DIR)
UserDefinedMapping({SAMPLE_PROJECTS_DIR})


def test_user_defined_mapping__no_input__returns_empty_mapping():
Expand Down Expand Up @@ -327,18 +327,19 @@ def test_LocalPackageResolver_lookup_packages(dep_name, expect_import_names):
def test_resolve_dependencies__focus_on_mappings(
dep_names, user_mapping, expected, tmp_path
):
custom_mapping_file = None
custom_mapping_files = None
custom_mapping = None
if user_mapping is not None:
custom_mapping = user_mapping.get("configuration")
if "file" in user_mapping:
custom_mapping_file = tmp_path / "mapping.toml"
custom_mapping_file.write_text(dedent(user_mapping["file"]))
custom_mapping_files = {custom_mapping_file}

assert (
resolve_dependencies(
dep_names,
custom_mapping_file=custom_mapping_file,
custom_mapping_file=custom_mapping_files,
custom_mapping=custom_mapping,
)
== expected
Expand Down
14 changes: 7 additions & 7 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ class SettingsTestVector:
config=dict(
actions=["list_deps"],
deps=["my_requirements.txt"],
custom_mapping_file="mapping.toml",
custom_mapping_file=["mapping.toml"],
),
expect=make_settings_dict(
actions={Action.LIST_DEPS},
deps={Path("my_requirements.txt")},
custom_mapping_file=Path("mapping.toml"),
custom_mapping_file={Path("mapping.toml")},
),
),
SettingsTestVector(
Expand All @@ -259,20 +259,20 @@ class SettingsTestVector:
deps=["my_requirements.txt"],
custom_mapping={"package": ["foo", "bar"]},
),
cmdline=dict(custom_mapping_file="mapping.toml"),
cmdline=dict(custom_mapping_file=["mapping.toml"]),
expect=make_settings_dict(
actions={Action.LIST_DEPS},
deps={Path("my_requirements.txt")},
custom_mapping={"package": ["foo", "bar"]},
custom_mapping_file=Path("mapping.toml"),
custom_mapping_file={Path("mapping.toml")},
),
),
SettingsTestVector(
"config_file_with_mapping_and_cli__cli_mapping_overrides_config",
config=dict(custom_mapping_file="foo.toml"),
cmdline=dict(custom_mapping_file="mapping.toml"),
config=dict(custom_mapping_file=["foo.toml"]),
cmdline=dict(custom_mapping_file=["mapping.toml"]),
expect=make_settings_dict(
custom_mapping_file=Path("mapping.toml"),
custom_mapping_file={Path("mapping.toml")},
),
),
SettingsTestVector(
Expand Down

0 comments on commit 9cb1629

Please sign in to comment.