Coverage for /Users/OORDCOR/Documents/code/bump-my-version/bumpversion/config/utils.py: 67%

1"""Helper functions for the config module."""

2 from __future__ import annotations

3

4 import glob

5 import itertools

6 from typing import Dict, List

7

8 from bumpversion.config.models import FileChange, VersionPartConfig

9 from bumpversion.utils import labels_for_format

10

11

12 def get_all_file_configs(config_dict: dict) -> List[FileChange]:

13 """Make sure all version parts are included."""

14 defaults = {

15 "parse": config_dict["parse"],

16 "serialize": config_dict["serialize"],

17 "search": config_dict["search"],

18 "replace": config_dict["replace"],

19 "ignore_missing_version": config_dict["ignore_missing_version"],

20 "regex": config_dict["regex"],

21 }

22 files = [{k: v for k, v in filecfg.items() if v is not None} for filecfg in config_dict["files"]]

23 for f in files: 23 ↛ 24line 23 didn't jump to line 24, because the loop on line 23 never started

24 f.update({k: v for k, v in defaults.items() if k not in f})

25 return [FileChange(**f) for f in files]

26

27

28 def get_all_part_configs(config_dict: dict) -> Dict[str, VersionPartConfig]:

29 """Make sure all version parts are included."""

30 serialize = config_dict["serialize"]

31 parts = config_dict["parts"]

32 all_labels = set(itertools.chain.from_iterable([labels_for_format(fmt) for fmt in serialize]))

33 return {

34 label: VersionPartConfig(**parts[label]) if label in parts else VersionPartConfig() # type: ignore[call-arg]

35 for label in all_labels

36 }

37

38

39 def resolve_glob_files(file_cfg: FileChange) -> List[FileChange]:

40 """

41 Return a list of file configurations that match the glob pattern.

42

43 Args:

44 file_cfg: The file configuration containing the glob pattern

45

46 Returns:

47 A list of resolved file configurations according to the pattern.

48 """

49 files = []

50 for filename_glob in glob.glob(file_cfg.glob, recursive=True):

51 new_file_cfg = file_cfg.model_copy()

52 new_file_cfg.filename = filename_glob

53 new_file_cfg.glob = None

54 files.append(new_file_cfg)

55 return files