Skip to content

Commit

Permalink
Only accept documented choices after -i and -q (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriPapadopoulos authored Jun 20, 2024
1 parent 058eaf1 commit b6de749
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,11 +501,13 @@ def parse_options(
action="store",
type=int,
default=0,
choices=range(0, 4),
help="set interactive mode when writing changes:\n"
"- 0: no interactivity.\n"
"- 1: ask for confirmation.\n"
"- 2: ask user to choose one fix when more than one is available.\n"
"- 3: both 1 and 2",
metavar="MODE",
)

parser.add_argument(
Expand All @@ -514,6 +516,7 @@ def parse_options(
action="store",
type=int,
default=34,
choices=range(0, 64),
help="bitmask that allows suppressing messages:\n"
"- 0: print all messages.\n"
"- 1: disable warnings about wrong encoding.\n"
Expand All @@ -526,6 +529,7 @@ def parse_options(
"combined; e.g. use 3 for levels 1+2, 7 for "
"1+2+4, 23 for 1+2+4+16, etc. "
"The default mask is %(default)s.",
metavar="LEVEL",
)

parser.add_argument(
Expand Down
7 changes: 6 additions & 1 deletion codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path
from shutil import copyfile
from typing import Any, Generator, Optional, Tuple, Union
from unittest import mock

import pytest

Expand Down Expand Up @@ -237,7 +238,11 @@ def test_interactivity(
try:
assert cs.main(fname) == 0, "empty file"
fname.write_text("abandonned\n")
assert cs.main("-i", "-1", fname) == 1, "bad"
with mock.patch.object(sys, "argv", ("-i", "-1", fname)):
with pytest.raises(SystemExit) as e:
cs.main("-i", "-1", fname)
assert e.type == SystemExit
assert e.value.code != 0
with FakeStdin("y\n"):
assert cs.main("-i", "3", fname) == 1
with FakeStdin("n\n"):
Expand Down

0 comments on commit b6de749

Please sign in to comment.