Skip to content

Commit

Permalink
Merge pull request #127 from LeastAuthority/123.pyprojecttoml-option
Browse files Browse the repository at this point in the history
Add a --pyproject option to towncrier.check
  • Loading branch information
hawkowl authored Sep 6, 2018
2 parents 3d600a8 + baabb88 commit 9e1b11e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
11 changes: 7 additions & 4 deletions src/towncrier/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
_underlines = ["=", "-", "~"]


def load_config(from_dir):
fn = os.path.join(from_dir, "pyproject.toml")
if not os.path.exists(fn):
def load_config(directory):
return load_config_from_file(os.path.join(directory, "pyproject.toml"))


def load_config_from_file(from_file):
if not os.path.exists(from_file):
return None
with open(fn, "r") as conffile:
with open(from_file, "r") as conffile:
config = toml.load(conffile)

if "tool" not in config:
Expand Down
15 changes: 9 additions & 6 deletions src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from subprocess import check_output, STDOUT

from ._settings import load_config
from ._settings import load_config, load_config_from_file
from ._builder import find_fragments


Expand All @@ -22,14 +22,17 @@ def _run(args, **kwargs):
@click.command()
@click.option("--compare-with", default="origin/master")
@click.option("--dir", "directory", default=".")
def _main(compare_with, directory):
return __main(compare_with, directory)
@click.option("--pyproject", "pyproject", default=None)
def _main(compare_with, directory, pyproject):
return __main(compare_with, directory, pyproject)


def __main(comparewith, directory):

def __main(comparewith, directory, pyproject):
base_directory = os.path.abspath(directory)
config = load_config(directory)
if pyproject is None:
config = load_config(directory)
else:
config = load_config_from_file(pyproject)

files_changed = (
_run(["git", "diff", "--name-only", comparewith + "..."], cwd=base_directory)
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/123.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`python -m towncrier.check` now accepts an option to give the configuration file location.
29 changes: 25 additions & 4 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,26 @@ class TestChecker(TestCase):
maxDiff = None

def test_no_changes_made(self):
self._test_no_changes_made(
"pyproject.toml",
lambda runner, main, argv: runner.invoke(main, argv),
)

def test_no_changes_made_pyproject_path(self):
pyproject = "not-pyproject.toml"
self._test_no_changes_made(
pyproject,
lambda runner, main, argv: runner.invoke(
main,
argv + ["--pyproject", pyproject],
),
)

def _test_no_changes_made(self, pyproject_path, invoke):
runner = CliRunner()

with runner.isolated_filesystem():
with open("pyproject.toml", "w") as f:
with open(pyproject_path, "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
Expand All @@ -34,7 +50,7 @@ def test_no_changes_made(self):
call(["git", "commit", "-m", "Initial Commit"])
call(["git", "checkout", "-b", "otherbranch"])

result = runner.invoke(_main, ["--compare-with", "master"])
result = invoke(runner, _main, ["--compare-with", "master"])

self.assertEqual(0, result.exit_code)
self.assertEqual(
Expand Down Expand Up @@ -81,9 +97,14 @@ def test_fragment_exists(self):
self.assertTrue(
result.output.endswith(
"Found:\n1. " + os.path.abspath(fragment_path) + "\n"
)
),
result,
)
self.assertEqual(
0,
result.exit_code,
result,
)
self.assertEqual(0, result.exit_code)

def test_fragment_missing(self):
runner = CliRunner()
Expand Down

0 comments on commit 9e1b11e

Please sign in to comment.