Skip to content

Commit

Permalink
feat(history): Add markdown changelog formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
relekang committed Aug 19, 2015
1 parent 2ca41d3 commit d77b58d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
25 changes: 25 additions & 0 deletions semantic_release/history/logs.py
Expand Up @@ -84,3 +84,28 @@ def generate_changelog(version):
pass

return changes


def markdown_changelog(version, changelog, header=False):
"""
Generates a markdown version of the changelog. Takes a parsed changelog dict from
generate_changelog.
:param version: A string with the version number.
:param changelog: A dict from generate_changelog.
:param header: A boolean that decides whether a header should be included or not.
:return: The markdown formatted changelog.
"""
output = ''
if header:
output += '## v{0}\n'.format(version)

for section in CHANGELOG_SECTIONS:
if len(changelog[section]) == 0:
continue

output += '\n### {0}\n'.format(section.capitalize())
for item in changelog[section]:
output += '* {0}\n'.format(item)

return output
58 changes: 56 additions & 2 deletions tests/test_history.py
Expand Up @@ -2,7 +2,7 @@

import semantic_release
from semantic_release.history import evaluate_version_bump, get_current_version, get_new_version
from semantic_release.history.logs import generate_changelog
from semantic_release.history.logs import generate_changelog, markdown_changelog

from . import mock

Expand Down Expand Up @@ -75,7 +75,6 @@ def test_should_return_none_without_commits(self):


class GenerateChangelogTests(TestCase):

def test_should_generate_all_sections(self):
with mock.patch('semantic_release.history.logs.get_commit_log',
lambda: ALL_KINDS_OF_COMMIT_MESSAGES + [MAJOR2, UNKNOWN_STYLE]):
Expand Down Expand Up @@ -135,3 +134,58 @@ def test_patch_bump(self):

def test_None_bump(self):
self.assertEqual(get_new_version('1.0.0', None), '1.0.0')


class MarkdownChangelogTests(TestCase):
def test_should_output_all_sections(self):
markdown = markdown_changelog('0', {
'refactor': ['Refactor super-feature'],
'breaking': ['Uses super-feature as default instead of dull-feature.'],
'feature': ['Add non-breaking super-feature', 'Add super-feature'],
'fix': ['Fix bug in super-feature'],
'documentation': ['Document super-feature']
})
self.assertEqual(
markdown,
'\n'
'### Feature\n'
'* Add non-breaking super-feature\n'
'* Add super-feature\n'
'\n'
'### Fix\n'
'* Fix bug in super-feature\n'
'\n'
'### Breaking\n'
'* Uses super-feature as default instead of dull-feature.\n'
'\n'
'### Documentation\n'
'* Document super-feature\n'
)

def test_should_not_include_empty_sections(self):
self.assertEqual(
markdown_changelog(
'1.0.1',
{'refactor': [], 'breaking': [], 'feature': [], 'fix': [], 'documentation': []},
),
''
)

def test_should_output_heading(self):
self.assertIn(
'## v1.0.1\n',
markdown_changelog(
'1.0.1',
{'refactor': [], 'breaking': [], 'feature': [], 'fix': [], 'documentation': []},
header=True
)
)

def test_should_not_output_heading(self):
self.assertNotIn(
'v1.0.1',
markdown_changelog(
'1.0.1',
{'refactor': [], 'breaking': [], 'feature': [], 'fix': [], 'documentation': []},
)
)

0 comments on commit d77b58d

Please sign in to comment.