Skip to content

Commit 78f1795

Browse files
authored
Merge pull request #974 from bact/test-coverage
Add test coverage
2 parents 6d0637a + 3f0e433 commit 78f1795

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pythainlp/cli/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import io
77
import sys
8-
from argparse import ArgumentParser
8+
from argparse import ArgumentError, ArgumentParser
99

1010
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
1111
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8")
@@ -23,6 +23,14 @@ def make_usage(command: str) -> dict:
2323

2424

2525
def exit_if_empty(command: str, parser: ArgumentParser) -> None:
26+
"""Print help and exit if command is empty.
27+
28+
:param command: command from command line
29+
:type command: str
30+
:param parser: parser object of the app
31+
:type parser: ArgumentParser
32+
"""
2633
if not command:
27-
parser.print_help()
28-
sys.exit(0)
34+
if parser:
35+
parser.print_help()
36+
raise ArgumentError(None, "No command provided.")

tests/test_cli.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@
1313

1414

1515
class CliTestCase(unittest.TestCase):
16+
def test_cli(self):
17+
with self.assertRaises((ArgumentError, SystemExit)):
18+
cli.exit_if_empty("", None)
19+
1620
def test_cli_main(self):
1721
# call with no argument, should exit with 2
1822
with self.assertRaises(SystemExit) as ex:
1923
__main__.main()
2024
self.assertEqual(ex.exception.code, 2)
2125

2226
with self.assertRaises((ArgumentError, SystemExit)):
23-
self.assertIsNone(__main__.main(["thainlp"]))
27+
__main__.main(["thainlp"])
2428

2529
with self.assertRaises((ArgumentError, SystemExit)):
26-
self.assertIsNone(
27-
__main__.main(["thainlp", "NOT_EXIST", "command"])
28-
)
30+
__main__.main(["thainlp", "NOT_EXIST", "command"])
2931

3032
self.assertIsNone(__main__.main(["thainlp", "data", "path"]))
3133

0 commit comments

Comments
 (0)