Coverage for /Users/coordt/Documents/code/bump-my-version/bumpversion/config/create.py: 15%

31 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-06-11 14:29 -0500

1"""Module for creating a new config file.""" 

2 

3from pathlib import Path 

4from typing import Tuple 

5 

6import questionary 

7from tomlkit import TOMLDocument 

8 

9 

10def create_configuration(destination: str, prompt: bool) -> TOMLDocument: 

11 """ 

12 Create a new configuration as a TOMLDocument. 

13 

14 Args: 

15 destination: `stdout` or a path to a new or existing file. 

16 prompt: `True` if the user should be prompted for input. 

17 

18 Returns: 

19 The TOMLDocument structure with the updated configuration. 

20 """ 

21 config, destination_config = get_defaults_from_dest(destination) 

22 

23 if prompt: 

24 allow_dirty_default = "(Y/n)" if config["allow_dirty"] else "(y/N)" 

25 answers = questionary.form( 

26 current_version=questionary.text("What is the current version?", default=config["current_version"]), 

27 commit=questionary.confirm( 

28 "Commit changes made when bumping to version control?", default=config["commit"] 

29 ), 

30 allow_dirty=questionary.confirm( 

31 "Allow dirty working directory when bumping?", 

32 default=config["allow_dirty"], 

33 instruction=( 

34 "If you are also creating or modifying other files (e.g. a CHANGELOG), say Yes. " 

35 f"{allow_dirty_default} " 

36 ), 

37 ), 

38 tag=questionary.confirm("Tag changes made when bumping in version control?", default=config["tag"]), 

39 commit_args=questionary.text( 

40 "Any extra arguments to pass to the commit command?", 

41 default=config["commit_args"] or "", 

42 instruction="For example, `--no-verify` is useful if you have a pre-commit hook. ", 

43 ), 

44 ).ask() 

45 config.update(answers) 

46 

47 for key, val in config.items(): 

48 destination_config["tool"]["bumpversion"][key] = val if val is not None else "" 

49 

50 return destination_config 

51 

52 

53def get_defaults_from_dest(destination: str) -> Tuple[dict, TOMLDocument]: 

54 """Get the default configuration and the configuration from the destination.""" 

55 from tomlkit import document, parse 

56 

57 from bumpversion.config import DEFAULTS 

58 

59 config = DEFAULTS.copy() 

60 if Path(destination).exists(): 

61 destination_config = parse(Path(destination).read_text(encoding="utf-8")) 

62 else: 

63 destination_config = document() 

64 

65 destination_config.setdefault("tool", {}) 

66 destination_config["tool"].setdefault("bumpversion", {}) 

67 existing_config = destination_config["tool"]["bumpversion"] 

68 if existing_config: 

69 config.update(existing_config) 

70 

71 project_config = destination_config.get("project", {}).get("version") 

72 config["current_version"] = config["current_version"] or project_config or "0.1.0" 

73 del config["scm_info"] 

74 del config["parts"] 

75 del config["files"] 

76 

77 return config, destination_config