Skip to content

Commit 89507c8

Browse files
authored
Add CLI tests and run them in CI (#29)
* ci: run pytest with default verbosity * tests(cli): locate console script by interpreter path * tests(cli): harden and extend coverage * ci: remove pip caching from workflow
1 parent acad2b6 commit 89507c8

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
python -m pip install --upgrade pip
2929
pip install flake8 pytest
3030
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
31+
pip install .
3132
- name: Lint with flake8
3233
run: |
3334
# stop the build if there are Python syntax errors or undefined names

tests/test_cli.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
import shutil
3+
import subprocess
4+
import sys
5+
from pathlib import Path
6+
7+
import pytest
8+
from korean_romanizer.romanizer import Romanizer
9+
10+
11+
UTF8_ENV = {**os.environ, "PYTHONIOENCODING": "UTF-8"}
12+
13+
14+
def _run_cli(args, *, input=None):
15+
cmd = [sys.executable, "-m", "korean_romanizer.cli", *args]
16+
return subprocess.run(
17+
cmd,
18+
input=input,
19+
capture_output=True,
20+
text=True,
21+
encoding="utf-8",
22+
check=False,
23+
env=UTF8_ENV,
24+
timeout=10,
25+
)
26+
27+
28+
@pytest.mark.parametrize(
29+
"text",
30+
[
31+
"안녕하세요",
32+
"아이유 방탄소년단",
33+
"구미, 영동",
34+
"구미,\t영동 서울!!",
35+
],
36+
)
37+
def test_cli_matches_library(text):
38+
args = text.split()
39+
expected = Romanizer(" ".join(args)).romanize().strip()
40+
proc = _run_cli(args)
41+
assert proc.returncode == 0
42+
assert proc.stderr == ""
43+
assert proc.stdout.strip() == expected
44+
45+
46+
def test_cli_help():
47+
proc = _run_cli(["-h"])
48+
assert proc.returncode == 0
49+
assert proc.stderr == ""
50+
assert "usage" in proc.stdout.lower()
51+
52+
53+
@pytest.mark.parametrize("stdin", [None, "안녕하세요\n"])
54+
def test_cli_no_args_shows_usage(stdin):
55+
proc = _run_cli([], input=stdin)
56+
assert proc.returncode != 0
57+
assert "usage" in proc.stderr.lower()
58+
59+
60+
def test_cli_long_input():
61+
text = ("안녕하세요 " * 1000).strip()
62+
args = text.split()
63+
expected = Romanizer(" ".join(args)).romanize().strip()
64+
proc = _run_cli(args)
65+
assert proc.returncode == 0
66+
assert proc.stderr == ""
67+
assert proc.stdout.strip() == expected
68+
69+
70+
def test_console_script_smoke():
71+
exe = shutil.which("kroman")
72+
if not exe:
73+
candidate = Path(sys.executable).with_name("kroman")
74+
if candidate.exists():
75+
exe = str(candidate)
76+
if not exe:
77+
candidate = Path(sys.executable).with_name("kroman.exe")
78+
if candidate.exists():
79+
exe = str(candidate)
80+
if not exe:
81+
pytest.skip("kroman console script not installed")
82+
proc = subprocess.run(
83+
[exe, "안녕하세요"],
84+
capture_output=True,
85+
text=True,
86+
encoding="utf-8",
87+
check=False,
88+
env=UTF8_ENV,
89+
timeout=10,
90+
)
91+
expected = Romanizer("안녕하세요").romanize().strip()
92+
assert proc.returncode == 0
93+
assert proc.stderr == ""
94+
assert proc.stdout.strip() == expected

0 commit comments

Comments
 (0)