Repository: mc706/changelog-cli

Issue:
`ChangelogUtils.get_changes` doesn't return all changes
 it would just return the last line of each section.
This worked fine, since nothing else in the code was actually reading the contents, just the existence of a section.

IMO, we could:

* Change it to return all changes, as I have done in #22
* Change it to `get_unreleased_change_types` and just returned the change types, as I have done in #24 

I have a preference for the latter, since the code gets simpler.

------

Aside: I noticed this while adding the typing information in #21

Discussion:

Patch:
diff --git a/src/changelog/utils.py b/src/changelog/utils.py
index ea09efb..61cb730 100644
--- a/src/changelog/utils.py
+++ b/src/changelog/utils.py
@@ -1,7 +1,7 @@
 import os
 import re
 from datetime import date
-from typing import cast, Dict, List, Optional
+from typing import cast, Dict, List, Optional, Set
 
 from changelog.templates import BASE, RELEASE_LINE, DEFAULT_VERSION, RELEASE_LINE_REGEXES
 from changelog.exceptions import ChangelogDoesNotExistError
@@ -66,10 +66,10 @@ class ChangelogUtils:
                 return match
         return DEFAULT_VERSION
 
-    def get_changes(self) -> Dict[str, str]:
+    def get_unreleased_change_types(self) -> Set[str]:
         """Get the list of chances since the last release"""
         data = self.get_changelog_data()
-        changes = {}
+        change_types = set()
         reading = False
         section = None
         for line in data:
@@ -79,25 +79,25 @@ class ChangelogUtils:
                 break
             if reading:
                 if line in self.REVERSE_SECTIONS:
-                    section = self.REVERSE_SECTIONS.get(line)
+                    section = self.REVERSE_SECTIONS[line]
                     continue
                 elif line in self.BETA_REVERSE_SECTIONS:
                     section = self.BETA_REVERSE_SECTIONS[line]
                     continue
-                changes[cast(str, section)] = line.strip().lstrip("* ")
+                change_types.add(section)
                 continue
             if line == "## Unreleased\n":
                 reading = True
                 continue
 
-        return changes
+        return change_types
 
     def get_release_suggestion(self) -> str:
         """Suggests a release type"""
-        changes = self.get_changes()
-        if 'removed' in changes or 'breaks' in changes:
+        change_types = self.get_unreleased_change_types()
+        if 'removed' in change_types or 'breaks' in change_types:
             return "major"
-        if 'added' in changes or 'new' in changes:
+        if 'added' in change_types or 'new' in change_types:
             return "minor"
         return "patch"
 
@@ -113,7 +113,7 @@ class ChangelogUtils:
     def cut_release(self, release_type: str = "suggest") -> None:
         """Cuts a release and updates changelog"""
         new_version = self.get_new_release_version(release_type)
-        changes = self.get_changes()
+        change_types = self.get_unreleased_change_types()
         data = self.get_changelog_data()
         output = []
         unreleased_position = 0
@@ -124,7 +124,7 @@ class ChangelogUtils:
             if line == "## Unreleased\n":
                 unreleased_position = i
                 line = RELEASE_LINE.format(new_version, date.today().isoformat())
-            if reading and line in self.REVERSE_SECTIONS and self.REVERSE_SECTIONS[line] not in changes:
+            if reading and line in self.REVERSE_SECTIONS and self.REVERSE_SECTIONS[line] not in change_types:
                 continue
             output.append(line)
         output.insert(unreleased_position, self.UNRELEASED)


Test patch:
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index 98f61fe..9195dd9 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -47,31 +47,31 @@ class UtilsTestCase(unittest.TestCase):
         self.assertEqual(self.cl.crunch_lines(document), ['this\n', '---\n', '\n', 'that\n'])
 
     def test_get_release_suggestion_patch(self):
-        with patch.object(ChangelogUtils, 'get_changes', return_value={'changed': ''}):
+        with patch.object(ChangelogUtils, 'get_unreleased_change_types', return_value={'changed': ''}):
             CL = ChangelogUtils()
             result = CL.get_release_suggestion()
             self.assertEqual(result, 'patch')
 
     def test_get_release_suggestion_minor(self):
-        with patch.object(ChangelogUtils, 'get_changes', return_value={'added': 'stuff'}):
+        with patch.object(ChangelogUtils, 'get_unreleased_change_types', return_value={'added': 'stuff'}):
             CL = ChangelogUtils()
             result = CL.get_release_suggestion()
             self.assertEqual(result, 'minor')
 
     def test_get_release_suggestion_major(self):
-        with patch.object(ChangelogUtils, 'get_changes', return_value={'removed': 'stuff'}):
+        with patch.object(ChangelogUtils, 'get_unreleased_change_types', return_value={'removed': 'stuff'}):
             CL = ChangelogUtils()
             result = CL.get_release_suggestion()
             self.assertEqual(result, 'major')
 
     def test_get_release_suggestion_minor_with_beta_headers(self):
-        with patch.object(ChangelogUtils, 'get_changes', return_value={'new': 'stuff'}):
+        with patch.object(ChangelogUtils, 'get_unreleased_change_types', return_value={'new': 'stuff'}):
             CL = ChangelogUtils()
             result = CL.get_release_suggestion()
             self.assertEqual(result, 'minor')
 
     def test_get_release_suggestion_major_with_beta_headers(self):
-        with patch.object(ChangelogUtils, 'get_changes', return_value={'breaks': 'stuff'}):
+        with patch.object(ChangelogUtils, 'get_unreleased_change_types', return_value={'breaks': 'stuff'}):
             CL = ChangelogUtils()
             result = CL.get_release_suggestion()
             self.assertEqual(result, 'major')
@@ -149,7 +149,7 @@ class UtilsTestCase(unittest.TestCase):
         ]
         with patch.object(ChangelogUtils, 'get_changelog_data', return_value=sample_data) as mock_read:
             CL = ChangelogUtils()
-            result = CL.get_changes()
+            result = CL.get_unreleased_change_types()
         self.assertTrue('added' in result)
         self.assertTrue('fixed' in result)
 
@@ -175,7 +175,7 @@ class UtilsTestCase(unittest.TestCase):
         ]
         with patch.object(ChangelogUtils, 'get_changelog_data', return_value=sample_data) as mock_read:
             CL = ChangelogUtils()
-            result = CL.get_changes()
+            result = CL.get_unreleased_change_types()
         self.assertTrue('new' in result)
         self.assertTrue('changes' in result)
         self.assertTrue('fixes' in result)
@@ -282,7 +282,6 @@ class ChangelogFileOperationTestCase(unittest.TestCase):
         self.assertTrue('### Removed\n' in data)
         self.assertTrue('### Fixed\n' in data)
         self.assertTrue('### Security\n' in data)
-#
 
     def test_match_version_canonical(self):
         line = "## 0.2.1 - (2017-06-09)"