Skip to content

Commit 6d1ebec

Browse files
committed
Replace unicode emojis with standard exit codes for CLI validation.
1 parent 8d5d657 commit 6d1ebec

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

src/codicefiscale/cli.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ def default_encoder(obj: Any) -> Any:
4343
sys.stdout.write(f"{cf_output}\n")
4444

4545

46-
def _validate_from_args(args: argparse.Namespace) -> None:
46+
def _validate_from_args(args: argparse.Namespace) -> int:
4747
if codicefiscale.is_valid(args.code):
48-
sys.stdout.write("✅\n")
48+
return 0
4949
else:
50-
sys.stdout.write("❌\n")
50+
return 1
5151

5252

5353
def run() -> None:
@@ -144,6 +144,6 @@ def run_with_args(args: argparse.Namespace) -> None:
144144
elif args.subcommand == "encode":
145145
_encode_from_args(args)
146146
elif args.subcommand == "validate":
147-
_validate_from_args(args)
147+
sys.exit(_validate_from_args(args))
148148
else:
149149
sys.stdout.write("For more info run: 'python -m codicefiscale --help'\n")

tests/test_cli.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from unittest import mock
66

77
from codicefiscale import __version__
8-
from codicefiscale.cli import run, run_with_args
8+
from codicefiscale.cli import _validate_from_args, run, run_with_args
99

1010

1111
def assert_command_output(command, expected_output):
@@ -588,32 +588,30 @@ def test_decode_with_wrong_code():
588588

589589

590590
def test_validate():
591-
with mock.patch("sys.stdout", new=StringIO()) as fake_output:
592-
args = argparse.Namespace(
593-
code="RSSMRA90A01H501W",
594-
subcommand="validate",
595-
)
596-
run_with_args(args)
597-
output = fake_output.getvalue().strip()
598-
assert output == "✅"
591+
args = argparse.Namespace(
592+
code="RSSMRA90A01H501W",
593+
subcommand="validate",
594+
)
595+
exit_code = _validate_from_args(args)
596+
assert exit_code == 0
599597

600598

601599
def test_validate_from_command_line():
602600
cmd = "python -m codicefiscale validate RSSMRA90A01H501W"
603-
assert_command_output(cmd, "✅")
601+
result = subprocess.run(cmd, shell=True)
602+
assert result.returncode == 0
604603

605604

606605
def test_validate_with_wrong_code():
607-
with mock.patch("sys.stdout", new=StringIO()) as fake_output:
608-
args = argparse.Namespace(
609-
code="RSSMRA90A01H501X",
610-
subcommand="validate",
611-
)
612-
run_with_args(args)
613-
output = fake_output.getvalue().strip()
614-
assert output == "❌"
606+
args = argparse.Namespace(
607+
code="RSSMRA90A01H501X",
608+
subcommand="validate",
609+
)
610+
exit_code = _validate_from_args(args)
611+
assert exit_code == 1
615612

616613

617614
def test_validate_with_wrong_code_from_command_line():
618615
cmd = "python -m codicefiscale validate RSSMRA90A01H501X"
619-
assert_command_output(cmd, "❌")
616+
result = subprocess.run(cmd, shell=True)
617+
assert result.returncode == 1

0 commit comments

Comments
 (0)