Skip to content

Move from Click to docopt for announce.py #1806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ debug_coverage*/**
analysis/**
bin/**
obj/**
.pytest_cache
26 changes: 14 additions & 12 deletions news/announce.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
"""Generate the changelog."""
"""Generate the changelog.

Usage: announce [--dry_run | --interim | --final] [<directory>]

"""
import enum
import operator
import os
Expand All @@ -8,7 +12,7 @@
import sys
import types

import click
import docopt


FILENAME_RE = re.compile(r"(?P<issue>\d+)(?P<nonce>-\S+)?\.md")
Expand Down Expand Up @@ -117,15 +121,6 @@ class RunType(enum.Enum):
final = 2


@click.command()
@click.option('--dry-run', 'run_type', flag_value=RunType.dry_run,
help='validate input')
@click.option('--interim', 'run_type', flag_value=RunType.interim, default=True,
help='generate Markdown')
@click.option('--final', 'run_type', flag_value=RunType.final,
help='generate Markdown & `git rm` news files')
@click.argument('directory', default=pathlib.Path(__file__).parent,
type=click.Path(exists=True, file_okay=False))
def main(run_type, directory):
directory = pathlib.Path(directory)
data = gather(directory)
Expand All @@ -137,4 +132,11 @@ def main(run_type, directory):


if __name__ == '__main__':
main()
arguments = docopt.docopt(__doc__)
run_type = RunType.interim
for possible_run_type in RunType:
if arguments[f"--{possible_run_type.name}"]:
run_type = possible_run_type
break
directory = arguments["<directory>"] or pathlib.Path(__file__).parent
main(run_type, directory)
2 changes: 1 addition & 1 deletion news/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
click~=6.7.0
docopt==0.6.2
pytest~=3.4.1
17 changes: 13 additions & 4 deletions news/test_announce.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pathlib

import docopt
import pytest

import announce as ann
Expand Down Expand Up @@ -48,8 +49,7 @@ def test_sections_sorting(directory):

def test_sections_naming(directory):
(directory / 'Hello').mkdir()
with pytest.raises(ValueError):
list(ann.sections(directory))
assert not ann.sections(directory)


def test_gather(directory):
Expand Down Expand Up @@ -79,8 +79,6 @@ def test_gather(directory):
assert entries[1].description == 'Fix 2'




def test_entry_markdown():
markdown = ann.entry_markdown(ann.NewsEntry(42, 'Hello, world!', None))
assert '42' in markdown
Expand Down Expand Up @@ -126,3 +124,14 @@ def fake_git_rm(path):
section, entries = results.pop()
assert len(entries) == 1
assert rm_path == entries[0].path


def test_cli():
for option in ("--"+opt for opt in ["dry_run", "interim", "final"]):
args = docopt.docopt(ann.__doc__, [option])
assert args[option]
args = docopt.docopt(ann.__doc__, ["./news"])
assert args["<directory>"] == "./news"
args = docopt.docopt(ann.__doc__, ["--dry_run", "./news"])
assert args["--dry_run"]
assert args["<directory>"] == "./news"