Skip to content

Commit

Permalink
refactor(parse): Separate reading file from parsing ADR format
Browse files Browse the repository at this point in the history
  • Loading branch information
mrwilson committed Sep 23, 2023
1 parent bca1d8b commit 5728786
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions adr_viewer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def generate_content(path, template_dir_override=None, title=None) -> str:
}

for index, adr_file in enumerate(files):

adr_attributes = parse_adr(adr_file)
markdown = open(adr_file).read()
adr_attributes = parse_adr(markdown)

if adr_attributes:
adr_attributes.index = index
Expand Down
4 changes: 2 additions & 2 deletions adr_viewer/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def extract_statuses_from_adr(page_object) -> Iterator[str]:
continue


def parse_adr(path) -> Optional[Adr]:
adr_as_html = mistune.markdown(open(path).read())
def parse_adr(content: str) -> Optional[Adr]:
adr_as_html = mistune.markdown(content)

soup = BeautifulSoup(adr_as_html, features='html.parser')

Expand Down
29 changes: 19 additions & 10 deletions adr_viewer/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,63 @@


def test_should_extract_title_from_record():
adr = parse_adr('doc/adr/0001-record-architecture-decisions.md')
markdown = open('doc/adr/0001-record-architecture-decisions.md').read()
adr = parse_adr(markdown)

assert adr.title == '1. Record architecture decisions'


def test_should_extract_status_from_record():
adr = parse_adr('doc/adr/0001-record-architecture-decisions.md')
markdown = open('doc/adr/0001-record-architecture-decisions.md').read()
adr = parse_adr(markdown)

assert adr.status == 'accepted'


def test_should_include_adr_as_html():
adr = parse_adr('doc/adr/0001-record-architecture-decisions.md')
def test_should_include_adr_as_markdown():
markdown = open('doc/adr/0001-record-architecture-decisions.md').read()
adr = parse_adr(markdown)

assert '<h1>1. Record architecture decisions</h1>' in adr.body


def test_should_mark_superseded_records():
adr = parse_adr('doc/adr/0003-use-same-colour-for-all-headers.md')
markdown = open('doc/adr/0003-use-same-colour-for-all-headers.md').read()
adr = parse_adr(markdown)

assert adr.status == 'superseded'


def test_should_mark_amended_records():
adr = parse_adr('doc/adr/0004-distinguish-superseded-records-with-colour.md')
markdown = open('doc/adr/0004-distinguish-superseded-records-with-colour.md').read()
adr = parse_adr(markdown)

assert adr.status == 'amended'


def test_should_mark_unknown_records():
adr = parse_adr('test/adr/0001-unknown-status.md')
markdown = open('test/adr/0001-unknown-status.md').read()
adr = parse_adr(markdown)

assert adr.status == 'unknown'


def test_should_mark_pending_records():
adr = parse_adr('test/adr/0002-pending-status.md')
markdown = open('test/adr/0002-pending-status.md').read()
adr = parse_adr(markdown)

assert adr.status == 'pending'


def test_should_mark_pproposed_records():
adr = parse_adr('test/adr/0004-proposed-status.md')
markdown = open('test/adr/0004-proposed-status.md').read()
adr = parse_adr(markdown)

assert adr.status == 'pending'


def test_should_ignore_invalid_files():
adr = parse_adr('test/adr/0003-bad-formatting.md')
markdown = open('test/adr/0003-bad-formatting.md').read()
adr = parse_adr(markdown)

assert adr is None

0 comments on commit 5728786

Please sign in to comment.