From 6f31cc1c17682402adbe9e65d0e12fcd16cc7439 Mon Sep 17 00:00:00 2001 From: Axel H Date: Tue, 9 May 2023 13:15:56 +0200 Subject: [PATCH] chore: remove the unused `changelog_parser` module --- commitizen/changelog_parser.py | 135 ----------------------- tests/test_changelog_parser.py | 196 --------------------------------- 2 files changed, 331 deletions(-) delete mode 100644 commitizen/changelog_parser.py delete mode 100644 tests/test_changelog_parser.py diff --git a/commitizen/changelog_parser.py b/commitizen/changelog_parser.py deleted file mode 100644 index 51348d347c..0000000000 --- a/commitizen/changelog_parser.py +++ /dev/null @@ -1,135 +0,0 @@ -"""CHNAGLOG PARSER DESIGN - -## Parse CHANGELOG.md - -1. Get LATEST VERSION from CONFIG -1. Parse the file version to version -2. Build a dict (tree) of that particular version -3. Transform tree into markdown again -""" -from __future__ import annotations - -import re -from collections import defaultdict -from typing import Generator, Iterable - -from commitizen.defaults import encoding - -MD_VERSION_RE = r"^##\s(?P[a-zA-Z0-9.+]+)\s?\(?(?P[0-9-]+)?\)?" -MD_CHANGE_TYPE_RE = r"^###\s(?P[a-zA-Z0-9.+\s]+)" -MD_MESSAGE_RE = ( - r"^-\s(\*{2}(?P[a-zA-Z0-9]+)\*{2}:\s)?(?P.+)(?P!)?" -) -md_version_c = re.compile(MD_VERSION_RE) -md_change_type_c = re.compile(MD_CHANGE_TYPE_RE) -md_message_c = re.compile(MD_MESSAGE_RE) - - -CATEGORIES = [ - ("fix", "fix"), - ("breaking", "BREAKING CHANGES"), - ("feat", "feat"), - ("refactor", "refactor"), - ("perf", "perf"), - ("test", "test"), - ("build", "build"), - ("ci", "ci"), - ("chore", "chore"), -] - - -def find_version_blocks(filepath: str, encoding: str = encoding) -> Generator: - """Find version block (version block: contains all the information about a version.) - - E.g: - ``` - ## 1.2.1 (2019-07-20) - - ### Fix - - - username validation not working - - ### Feat - - - new login system - - ``` - """ - with open(filepath, "r", encoding=encoding) as f: - block: list = [] - for line in f: - line = line.strip("\n") - if not line: - continue - - if line.startswith("## "): - if len(block) > 0: - yield block - block = [line] - else: - block.append(line) - yield block - - -def parse_md_version(md_version: str) -> dict: - m = md_version_c.match(md_version) - if not m: - return {} - return m.groupdict() - - -def parse_md_change_type(md_change_type: str) -> dict: - m = md_change_type_c.match(md_change_type) - if not m: - return {} - return m.groupdict() - - -def parse_md_message(md_message: str) -> dict: - m = md_message_c.match(md_message) - if not m: - return {} - return m.groupdict() - - -def transform_change_type(change_type: str) -> str: - # TODO: Use again to parse, for this we have to wait until the maps get - # defined again. - _change_type_lower = change_type.lower() - for match_value, output in CATEGORIES: - if re.search(match_value, _change_type_lower): - return output - else: - raise ValueError(f"Could not match a change_type with {change_type}") - - -def generate_block_tree(block: list[str]) -> dict: - # tree: Dict = {"commits": []} - changes: dict = defaultdict(list) - tree: dict = {"changes": changes} - - change_type = None - for line in block: - if line.startswith("## "): - # version identified - change_type = None - tree = {**tree, **parse_md_version(line)} - elif line.startswith("### "): - # change_type identified - result = parse_md_change_type(line) - if not result: - continue - change_type = result.get("change_type", "").lower() - - elif line.startswith("- "): - # message identified - commit = parse_md_message(line) - changes[change_type].append(commit) - else: - print("it's something else: ", line) - return tree - - -def generate_full_tree(blocks: Iterable) -> Iterable[dict]: - for block in blocks: - yield generate_block_tree(block) diff --git a/tests/test_changelog_parser.py b/tests/test_changelog_parser.py deleted file mode 100644 index 9145934131..0000000000 --- a/tests/test_changelog_parser.py +++ /dev/null @@ -1,196 +0,0 @@ -import os - -import pytest - -from commitizen import changelog_parser - -CHANGELOG_TEMPLATE = """ -## 1.0.0 (2019-07-12) - -### Fix - -- issue in poetry add preventing the installation in py36 -- **users**: lorem ipsum apap - -### Feat - -- it is possible to specify a pattern to be matched in configuration files bump. - -## 0.9 (2019-07-11) - -### Fix - -- holis - -""" - - -@pytest.fixture -def changelog_content() -> str: - changelog_path = "tests/CHANGELOG_FOR_TEST.md" - with open(changelog_path, "r", encoding="utf-8") as f: - return f.read() - - -@pytest.fixture -def existing_changelog_file(tmpdir): - with tmpdir.as_cwd(): - changelog_path = os.path.join(os.getcwd(), "CHANGELOG.md") - # changelog_path = "tests/CHANGELOG.md" - - with open(changelog_path, "w", encoding="utf-8") as f: - f.write(CHANGELOG_TEMPLATE) - - yield changelog_path - - os.remove(changelog_path) - - -def test_read_changelog_blocks(existing_changelog_file): - blocks = changelog_parser.find_version_blocks( - existing_changelog_file, encoding="utf-8" - ) - blocks = list(blocks) - amount_of_blocks = len(blocks) - assert amount_of_blocks == 2 - - -VERSION_CASES: list = [ - ("## 1.0.0 (2019-07-12)", {"version": "1.0.0", "date": "2019-07-12"}), - ("## 2.3.0a0", {"version": "2.3.0a0", "date": None}), - ("## 0.10.0a0", {"version": "0.10.0a0", "date": None}), - ("## 1.0.0rc0", {"version": "1.0.0rc0", "date": None}), - ("## 1beta", {"version": "1beta", "date": None}), - ( - "## 1.0.0rc1+e20d7b57f3eb (2019-3-24)", - {"version": "1.0.0rc1+e20d7b57f3eb", "date": "2019-3-24"}, - ), - ("### Bug fixes", {}), - ("- issue in poetry add preventing the installation in py36", {}), -] - -CATEGORIES_CASES: list = [ - ("## 1.0.0 (2019-07-12)", {}), - ("## 2.3.0a0", {}), - ("### Bug fixes", {"change_type": "Bug fixes"}), - ("### Features", {"change_type": "Features"}), - ("- issue in poetry add preventing the installation in py36", {}), -] - -CATEGORIES_TRANSFORMATIONS: list = [ - ("Bug fixes", "fix"), - ("Features", "feat"), - ("BREAKING CHANGES", "BREAKING CHANGES"), -] - -MESSAGES_CASES: list = [ - ("## 1.0.0 (2019-07-12)", {}), - ("## 2.3.0a0", {}), - ("### Fix", {}), - ( - "- name no longer accept invalid chars", - { - "message": "name no longer accept invalid chars", - "scope": None, - "breaking": None, - }, - ), - ( - "- **users**: lorem ipsum apap", - {"message": "lorem ipsum apap", "scope": "users", "breaking": None}, - ), -] - - -@pytest.mark.parametrize("test_input,expected", VERSION_CASES) -def test_parse_md_version(test_input, expected): - assert changelog_parser.parse_md_version(test_input) == expected - - -@pytest.mark.parametrize("test_input,expected", CATEGORIES_CASES) -def test_parse_md_change_type(test_input, expected): - assert changelog_parser.parse_md_change_type(test_input) == expected - - -@pytest.mark.parametrize("test_input,expected", CATEGORIES_TRANSFORMATIONS) -def test_transform_change_type(test_input, expected): - assert changelog_parser.transform_change_type(test_input) == expected - - -@pytest.mark.parametrize("test_input,expected", MESSAGES_CASES) -def test_parse_md_message(test_input, expected): - assert changelog_parser.parse_md_message(test_input) == expected - - -def test_transform_change_type_fail(): - with pytest.raises(ValueError) as excinfo: - changelog_parser.transform_change_type("Bugs") - assert "Could not match a change_type" in str(excinfo.value) - - -def test_generate_block_tree(existing_changelog_file): - blocks = changelog_parser.find_version_blocks( - existing_changelog_file, encoding="utf-8" - ) - block = next(blocks) - tree = changelog_parser.generate_block_tree(block) - assert tree == { - "changes": { - "fix": [ - { - "scope": None, - "breaking": None, - "message": "issue in poetry add preventing the installation in py36", - }, - {"scope": "users", "breaking": None, "message": "lorem ipsum apap"}, - ], - "feat": [ - { - "scope": None, - "breaking": None, - "message": ( - "it is possible to specify a pattern to be matched " - "in configuration files bump." - ), - } - ], - }, - "version": "1.0.0", - "date": "2019-07-12", - } - - -def test_generate_full_tree(existing_changelog_file): - blocks = changelog_parser.find_version_blocks( - existing_changelog_file, encoding="utf-8" - ) - tree = list(changelog_parser.generate_full_tree(blocks)) - - assert tree == [ - { - "changes": { - "fix": [ - { - "scope": None, - "message": "issue in poetry add preventing the installation in py36", - "breaking": None, - }, - {"scope": "users", "message": "lorem ipsum apap", "breaking": None}, - ], - "feat": [ - { - "scope": None, - "message": "it is possible to specify a pattern to be matched in configuration files bump.", - "breaking": None, - } - ], - }, - "version": "1.0.0", - "date": "2019-07-12", - }, - { - "changes": {"fix": [{"scope": None, "message": "holis", "breaking": None}]}, - "version": "0.9", - "date": "2019-07-11", - }, - ]