Skip to content

Commit

Permalink
Test CLI using click.testing.CliRunner (#530)
Browse files Browse the repository at this point in the history
* Test ‘build’ behaviour using a ‘click.testing.CliRunner’.

This invokes the command behaviour consistently, without assuming any
particular installed command name.

* Use CliRunner for testing CLI standard output streams.

* Document the improvement in a Towncrier news fragment.

* Test ‘--version’ behaviour using a ‘click.testing.CliRunner’.

This invokes the command behaviour consistently, without assuming any
particular installed command name.

* Test ‘--help’ behaviour using a ‘click.testing.CliRunner’.

This invokes the command behaviour consistently, without assuming any
particular installed command name.

* Remove unused imports.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Semantic linefeeds

* Markup

---------

Co-authored-by: Ben Finney <ben+python@benfinney.id.au>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 9, 2023
1 parent 2d47105 commit c0af1f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/towncrier/newsfragments/481.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``click.testing.CliRunner`` is now consistently used for testing CLI behaviour.

This invokes the command behaviour consistently, without assuming any particular installed command name.
22 changes: 9 additions & 13 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

import os
import os.path
import sys
import warnings

from pathlib import Path
from subprocess import PIPE, Popen, call
from subprocess import call

from click.testing import CliRunner
from twisted.trial.unittest import TestCase

from towncrier import check
from towncrier.build import _main as towncrier_build
from towncrier.check import _main as towncrier_check

from .helpers import setup_simple_project, with_isolated_runner, write
Expand Down Expand Up @@ -182,15 +182,11 @@ def test_none_stdout_encoding_works(self):
call(["git", "add", fragment_path])
call(["git", "commit", "-m", "add a newsfragment"])

proc = Popen(
[sys.executable, "-m", "towncrier.check", "--compare-with", "master"],
stdout=PIPE,
stderr=PIPE,
)
stdout, stderr = proc.communicate()
runner = CliRunner(mix_stderr=False)
result = runner.invoke(towncrier_check, ["--compare-with", "master"])

self.assertEqual(0, proc.returncode)
self.assertEqual(b"", stderr)
self.assertEqual(0, result.exit_code)
self.assertEqual(0, len(result.stderr))

def test_first_release(self):
"""
Expand All @@ -207,7 +203,7 @@ def test_first_release(self):
# Before any release, the NEWS file might no exist.
self.assertNotIn("NEWS.rst", os.listdir("."))

call(["towncrier", "build", "--yes", "--version", "1.0"])
runner.invoke(towncrier_build, ["--yes", "--version", "1.0"])
commit("Prepare a release")
# When missing,
# the news file is automatically created with a new release.
Expand Down Expand Up @@ -235,7 +231,7 @@ def test_release_branch(self):

# Do a first release without any checks.
# And merge the release branch back into the main branch.
call(["towncrier", "build", "--yes", "--version", "1.0"])
runner.invoke(towncrier_build, ["--yes", "--version", "1.0"])
commit("First release")
# The news file is now created.
self.assertIn("NEWS.rst", os.listdir("."))
Expand All @@ -260,7 +256,7 @@ def test_release_branch(self):

# We now have the new release branch.
call(["git", "checkout", "-b", "next-release"])
call(["towncrier", "build", "--yes", "--version", "2.0"])
runner.invoke(towncrier_build, ["--yes", "--version", "2.0"])
commit("Second release")

# Act
Expand Down
18 changes: 12 additions & 6 deletions src/towncrier/test/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import os
import sys

from subprocess import check_output
from unittest import skipIf

from click.testing import CliRunner
from twisted.trial.unittest import TestCase

from .._project import get_project_name, get_version
from .._shell import cli as towncrier_cli
from .helpers import write


Expand All @@ -19,6 +20,9 @@
metadata_version = None


towncrier_cli.name = "towncrier"


class VersionFetchingTests(TestCase):
def test_str(self):
"""
Expand Down Expand Up @@ -174,6 +178,7 @@ def test_dash_m(self):
"""
`python -m towncrier` invokes the main entrypoint.
"""
runner = CliRunner()
temp = self.mktemp()
new_dir = os.path.join(temp, "dashm")
os.makedirs(new_dir)
Expand All @@ -183,15 +188,16 @@ def test_dash_m(self):
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'directory = "news"\n')
os.makedirs("news")
out = check_output([sys.executable, "-m", "towncrier", "--help"])
self.assertIn(b"[OPTIONS] COMMAND [ARGS]...", out)
self.assertRegex(out, rb".*--help\s+Show this message and exit.")
result = runner.invoke(towncrier_cli, ["--help"])
self.assertIn("[OPTIONS] COMMAND [ARGS]...", result.stdout)
self.assertRegex(result.stdout, r".*--help\s+Show this message and exit.")
finally:
os.chdir(orig_dir)

def test_version(self):
"""
`--version` command line option is available to show the current production version.
"""
out = check_output(["towncrier", "--version"])
self.assertTrue(out.startswith(b"towncrier, version 2"))
runner = CliRunner()
result = runner.invoke(towncrier_cli, ["--version"])
self.assertTrue(result.output.startswith("towncrier, version 2"))

0 comments on commit c0af1f8

Please sign in to comment.