Skip to content

Commit 6a6e8fd

Browse files
authored
Renderer; sort versions and changes sections (#4)
1 parent 7ee0066 commit 6a6e8fd

16 files changed

+234
-29
lines changed

.github/workflows/tests.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
name: Run tests
2-
3-
on: [push, pull_request]
2+
on:
3+
pull_request:
4+
push:
5+
branches:
6+
- master
7+
paths-ignore:
8+
- CHANGELOG.md
9+
- README.md
410

511
jobs:
612
build:
713
runs-on: ubuntu-latest
814
strategy:
915
matrix:
1016
python-version: ['3.7', '3.8', '3.9', '3.10']
11-
1217
steps:
1318
- uses: actions/checkout@v2
1419
- name: Set up Python

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## 0.2.0 - 2022-08-01
10+
11+
### Added
12+
13+
- Print output on success.
14+
15+
### Changed
16+
17+
- Sort version sections by version number.
18+
- Sort changes sections by type.
19+
20+
921
## 0.1.1 - 2022-08-01
1022

1123
### Fixed

ocdc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.1"
1+
__version__ = "0.2.0"

ocdc/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def format(args: argparse.Namespace) -> None:
6262
sys.exit(f"ERROR: {path} would be reformatted")
6363
else:
6464
path.write_text(result)
65+
print(f"{path} was reformatted")
66+
else:
67+
print(f"{path} is well-formatted")
6568

6669

6770
def create_new(args: argparse.Namespace) -> None:

ocdc/renderer.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import datetime as dt
2+
import sys
23
from textwrap import TextWrapper, indent
34

5+
from packaging.version import InvalidVersion, Version as ParsedVersion
6+
47
from . import ast
58

69
MAX_WIDTH = 90
@@ -18,6 +21,19 @@ def wrap_item(item: ast.ListItem) -> str:
1821
return indent(item_wrapper.fill(item.text), item.level * INDENT)
1922

2023

24+
MAX_VERSION = ParsedVersion(str(sys.maxsize))
25+
26+
27+
def parse_version(number: str) -> ParsedVersion:
28+
try:
29+
return ParsedVersion(number)
30+
except InvalidVersion:
31+
return MAX_VERSION
32+
33+
34+
change_type_order = {t.value: i for i, t in enumerate(ast.ChangeType)}
35+
36+
2137
def render_markdown(c: ast.Changelog) -> str:
2238
text = ""
2339

@@ -27,14 +43,17 @@ def render_markdown(c: ast.Changelog) -> str:
2743
if c.intro:
2844
text += wrap(c.intro) + "\n"
2945

30-
for v in c.versions:
46+
version_order = {v.number: parse_version(v.number) for v in c.versions}
47+
48+
for v in sorted(c.versions, reverse=True, key=lambda v: version_order[v.number]):
3149
text += f"\n\n## {v.number}"
3250
if v.date:
3351
text += f" - {v.date}"
3452
text += "\n"
3553

36-
for type_, changes in v.changes.items():
54+
for type_ in sorted(v.changes, key=lambda t: change_type_order[t]):
3755
text += f"\n### {type_}\n\n"
56+
changes = v.changes[type_]
3857
for item in changes.items:
3958
text += wrap_item(item) + "\n"
4059
if changes.footer:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "ocdc"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
description = 'A changelog formatter for "people", neat freaks, and sloppy typists.'
55
authors = ["Matteo De Wint <[email protected]>"]
66
packages = [{ include = "ocdc" }]

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.1.1
2+
current_version = 0.2.0
33
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)
44
serialize =
55
{major}.{minor}.{patch}

tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
import os
22
from contextlib import contextmanager
3+
from pathlib import Path
4+
from typing import Callable, Optional
35

46
import pytest
57

8+
DUMP_EXPECTED_OUTPUTS = "--dump-expected-outputs"
9+
10+
11+
def pytest_addoption(parser):
12+
g = parser.getgroup("ocdc")
13+
g.addoption(
14+
DUMP_EXPECTED_OUTPUTS,
15+
action="store_true",
16+
help="Dump expected outputs of test cases",
17+
)
18+
19+
20+
@pytest.fixture(scope="session")
21+
def dump_expected_output(request) -> Optional[Callable[[Path, str], None]]:
22+
if request.config.getoption(DUMP_EXPECTED_OUTPUTS):
23+
24+
def dump(path: Path, text: str) -> None:
25+
path.write_text(text)
26+
pytest.skip()
27+
28+
return dump
29+
return None
30+
631

732
@pytest.fixture(scope="session")
833
def chdir():

tests/data/basic.ast.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
"versions": [
55
{
66
"number": "Unreleased",
7-
"date": "",
87
"changes": {
98
"Added": {
109
"items": [
1110
{
12-
"text": "Not much, to be honest.",
13-
"level": 0
11+
"text": "Not much, to be honest."
1412
},
1513
{
1614
"text": "This item is indented.",
@@ -21,8 +19,7 @@
2119
"level": 1
2220
},
2321
{
24-
"text": "Boom! Back to the left. These are links: [one] and [two].",
25-
"level": 0
22+
"text": "Boom! Back to the left. These are links: [one] and [two]."
2623
}
2724
],
2825
"footer": "[one]: http://example.com/one\n[two]: http://example.com/two"
@@ -36,14 +33,15 @@
3633
"Added": {
3734
"items": [
3835
{
39-
"text": "The first version, obviously. This line is going to be a long one. Let's hope it gets split at 90 characters.",
40-
"level": 0
36+
"text": "The first version, obviously. This line is going to be a long one. Let's hope it gets split at 90 characters."
4137
}
4238
]
4339
},
4440
"Fixed": {
4541
"items": [
46-
{ "text": "Nothing at all. This line\nis split early.", "level": 0 }
42+
{
43+
"text": "Nothing at all. This line\nis split early."
44+
}
4745
]
4846
}
4947
}

tests/data/empty.ast.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{
2-
"title": "",
3-
"intro": "",
42
"versions": []
53
}

0 commit comments

Comments
 (0)