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

1"""Version changing methods."""

2

3 import shlex

4 from pathlib import Path

5 from typing import TYPE_CHECKING, ChainMap, List, Optional

6

7 if TYPE_CHECKING: # pragma: no-coverage 7 ↛ 8line 7 didn't jump to line 8, because the condition on line 7 was never true

8 from bumpversion.files import ConfiguredFile

9 from bumpversion.versioning.models import Version

10

11 from bumpversion.config import Config

12 from bumpversion.config.files import update_config_file

13 from bumpversion.config.files_legacy import update_ini_config_file

14 from bumpversion.exceptions import ConfigurationError

15 from bumpversion.ui import get_indented_logger

16 from bumpversion.utils import get_context, key_val_string

17

18 logger = get_indented_logger(__name__)

19

20

21 def get_next_version(

22 current_version: "Version", config: Config, version_part: Optional[str], new_version: Optional[str]

23) -> "Version":

24 """

25 Bump the version_part to the next value.

26

27 Args:

28 current_version: The current version

29 config: The current configuration

30 version_part: Optional part of the version to bump

31 new_version: Optional specific version to bump to

32

33 Returns:

34 The new version

35

36 Raises:

37 ConfigurationError: If it can't generate the next version.

38 """

39 if new_version: 39 ↛ 40line 39 didn't jump to line 40, because the condition on line 39 was never true

40 logger.info("Attempting to set new version '%s'", new_version)

41 logger.indent()

42 next_version = config.version_config.parse(new_version)

43 elif version_part: 43 ↛ 48line 43 didn't jump to line 48, because the condition on line 43 was never false

44 logger.info("Attempting to increment part '%s'", version_part)

45 logger.indent()

46 next_version = current_version.bump(version_part)

47 else:

48 raise ConfigurationError("Unable to get the next version.")

49

50 logger.info("Values are now: %s", key_val_string(next_version.components))

51 logger.dedent()

52 return next_version

53

54

55 def do_bump(

56 version_part: Optional[str],

57 new_version: Optional[str],

58 config: Config,

59 config_file: Optional[Path] = None,

60 dry_run: bool = False,

61) -> None:

62 """

63 Bump the version_part to the next value or set the version to new_version.

64

65 Args:

66 version_part: The version part to bump

67 new_version: The explicit version to set

68 config: The configuration to use

69 config_file: The configuration file to update

70 dry_run: True if the operation should be a dry run

71 """

72 from bumpversion.files import modify_files, resolve_file_config

73

74 logger.indent()

75

76 ctx = get_context(config)

77 logger.info("Parsing current version '%s'", config.current_version)

78 logger.indent()

79 version = config.version_config.parse(config.current_version)

80 logger.dedent()

81 next_version = get_next_version(version, config, version_part, new_version)

82 next_version_str = config.version_config.serialize(next_version, ctx)

83 logger.info("New version will be '%s'", next_version_str)

84

85 if config.current_version == next_version_str: 85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true

86 logger.info("Version is already '%s'", next_version_str)

87 return

88

89 logger.dedent()

90

91 if dry_run: 91 ↛ 94line 91 didn't jump to line 94, because the condition on line 91 was never false

92 logger.info("Dry run active, won't touch any files.")

93

94 ctx = get_context(config, version, next_version)

95

96 configured_files = resolve_file_config(config.files_to_modify, config.version_config)

97 modify_files(configured_files, version, next_version, ctx, dry_run)

98 if config_file and config_file.suffix in {".cfg", ".ini"}: 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never true

99 update_ini_config_file(config_file, config.current_version, next_version_str, dry_run)

100 else:

101 update_config_file(config_file, config, version, next_version, ctx, dry_run)

102

103 ctx = get_context(config, version, next_version)

104 ctx["new_version"] = next_version_str

105 commit_and_tag(config, config_file, configured_files, ctx, dry_run)

106 logger.info("Done.")

107

108

109 def commit_and_tag(

110 config: Config,

111 config_file: Optional[Path],

112 configured_files: List["ConfiguredFile"],

113 ctx: ChainMap,

114 dry_run: bool = False,

115) -> None:

116 """

117 Commit and tag the changes if a tool is configured.

118

119 Args:

120 config: The configuration

121 config_file: The configuration file to include in the commit, if it exists

122 configured_files: A list of files to commit

123 ctx: The context used to render the tag and tag message

124 dry_run: True if the operation should be a dry run

125 """

126 if not config.scm_info.tool: 126 ↛ 127line 126 didn't jump to line 127, because the condition on line 126 was never true

127 return

128

129 extra_args = shlex.split(config.commit_args) if config.commit_args else []

130

131 commit_files = {f.file_change.filename for f in configured_files}

132 if config_file: 132 ↛ 135line 132 didn't jump to line 135, because the condition on line 132 was never false

133 commit_files |= {str(config_file)}

134

135 config.scm_info.tool.commit_to_scm(list(commit_files), config, ctx, extra_args, dry_run)

136 config.scm_info.tool.tag_in_scm(config, ctx, dry_run)