Skip to content

Commit ee6d1ea

Browse files
committed
feat: --check-fail-msg
1 parent a53a8e4 commit ee6d1ea

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ These are changes to Cog over time.
2121
Unreleased
2222
----------
2323

24+
- Added a ``--check-fail-msg`` option for providing a message as part of the
25+
output of a ``--check`` failure.
26+
2427
- Added support for Python 3.14.
2528

2629

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ cogdoc: ## Run cog to keep the docs correct.
4141
python -m cogapp -r $(COGARGS)
4242

4343
lintdoc: ## Check that the docs are up-to-date.
44-
@python -m cogapp --check --diff $(COGARGS); \
45-
if [ $$? -ne 0 ]; then \
46-
echo 'Docs need to be updated: `make cogdoc`'; \
47-
exit 1; \
48-
fi
44+
@python -m cogapp --check --check-fail-msg='Docs need to be updated: `make cogdoc`' --diff $(COGARGS)
4945

5046
dochtml: ## Build local docs.
5147
$(MAKE) -C docs html

cogapp/cogapp.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
-z The end-output marker can be omitted, and is assumed at eof.
5353
-v Print the version of cog and exit.
5454
--check Check that the files would not change if run again.
55+
--check-fail-msg='MSG'
56+
If --check fails, include MSG in the output to help devs
57+
understand how to run cog in your project.
5558
--diff With --check, show a diff of what failed the check.
5659
--markers='START END END-OUTPUT'
5760
The patterns surrounding cog inline instructions. Should
@@ -243,6 +246,7 @@ def __init__(self):
243246
self.prologue = ""
244247
self.print_output = False
245248
self.check = False
249+
self.check_fail_msg = None
246250
self.diff = False
247251

248252
def __eq__(self, other):
@@ -266,6 +270,7 @@ def parse_args(self, argv):
266270
"cdD:eI:n:o:rs:p:PUvw:xz",
267271
[
268272
"check",
273+
"check-fail-msg=",
269274
"diff",
270275
"markers=",
271276
"verbosity=",
@@ -313,6 +318,8 @@ def parse_args(self, argv):
313318
self.eof_can_be_end = True
314319
elif o == "--check":
315320
self.check = True
321+
elif o == "--check-fail-msg":
322+
self.check_fail_msg = a
316323
elif o == "--diff":
317324
self.diff = True
318325
elif o == "--markers":
@@ -794,7 +801,10 @@ def callable_main(self, argv):
794801
raise CogUsageError("No files to process")
795802

796803
if self.check_failed:
797-
raise CogCheckFailed("Check failed")
804+
msg = "Check failed"
805+
if self.options.check_fail_msg:
806+
msg = f"{msg}: {self.options.check_fail_msg}"
807+
raise CogCheckFailed(msg)
798808

799809
def main(self, argv):
800810
"""Handle the command-line execution for cog."""

cogapp/test_cogapp.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,26 @@ def test_check_bad_with_diff(self):
22102210
self.assertEqual(self.output.getvalue(), reindent_block(output))
22112211
self.assert_made_files_unchanged(d)
22122212

2213+
def test_check_bad_with_message(self):
2214+
d = {
2215+
"changed.cog": """\
2216+
//[[[cog
2217+
cog.outl("goodbye world")
2218+
//]]]
2219+
hello world
2220+
//[[[end]]]
2221+
""",
2222+
}
2223+
make_files(d)
2224+
self.run_check(
2225+
["--check-fail-msg=Run `make cogged` to fix", "changed.cog"], status=5
2226+
)
2227+
self.assertEqual(
2228+
self.output.getvalue(),
2229+
"Checking changed.cog (changed)\nCheck failed: Run `make cogged` to fix\n",
2230+
)
2231+
self.assert_made_files_unchanged(d)
2232+
22132233
def test_check_mixed(self):
22142234
d = {
22152235
"unchanged.cog": """\

docs/running.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Cog is a command-line utility which takes arguments in standard form.
5656
-z The end-output marker can be omitted, and is assumed at eof.
5757
-v Print the version of cog and exit.
5858
--check Check that the files would not change if run again.
59+
--check-fail-msg='MSG'
60+
If --check fails, include MSG in the output to help devs
61+
understand how to run cog in your project.
5962
--diff With --check, show a diff of what failed the check.
6063
--markers='START END END-OUTPUT'
6164
The patterns surrounding cog inline instructions. Should
@@ -66,7 +69,7 @@ Cog is a command-line utility which takes arguments in standard form.
6669
1 lists only changed files, 0 lists no files.
6770
-h, --help Print this help.
6871
69-
.. {{{end}}} (sum: VLpx2/79qp)
72+
.. {{{end}}} (sum: HvoLNTo8/2)
7073
7174
In addition to running cog as a command on the command line, you can also
7275
invoke it as a module with the Python interpreter:
@@ -217,6 +220,10 @@ check that your files have been updated properly.
217220
The ``--diff`` option will show a unified diff of the change that caused
218221
``--check`` to fail.
219222

223+
The ``--check-fail-msg`` option can be used to provide a message as part of the
224+
output if ``--check`` fails. This can be used to give instructions about how
225+
to run cog in your project to fix the problem.
226+
220227

221228
Output line suffixes
222229
--------------------

0 commit comments

Comments
 (0)