From 2cd627071944784daae5de765d1b65883f638766 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Thu, 8 Jun 2023 11:41:48 -0700 Subject: [PATCH] Don't print tracebacks on cli invocation yaml2ics can be run in three ways: - `python yaml2ics.py` - `python -m yaml2ics` - `yaml2ics` This ensures that none of those print tracebacks, while testing (which relies on errors being raised) still functions as expected. --- pyproject.toml | 2 +- yaml2ics.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5f77cdd..5ec5c19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,7 +31,7 @@ test = ["pytest >= 7.3", "pytest-cov >= 4.0"] lint = ["pre-commit >= 3.0"] [project.scripts] -yaml2ics = "yaml2ics:main" +yaml2ics = "yaml2ics:cli" [project.urls] Home = "https://github.com/scientific-python/yaml2ics" diff --git a/yaml2ics.py b/yaml2ics.py index df0cdef..1a99268 100644 --- a/yaml2ics.py +++ b/yaml2ics.py @@ -186,6 +186,9 @@ def files_to_calendar(files: list) -> ics.Calendar: return calendar +# `main` is separate from `cli` to facilitate testing. +# The only difference being that `main` raises errors while +# `cli` prints them and exits with errorcode 1 def main(): if len(sys.argv) < 2: raise RuntimeError("Usage: yaml2ics.py FILE1.yaml FILE2.yaml ...") @@ -200,9 +203,13 @@ def main(): print(calendar.serialize()) -if __name__ == "__main__": # pragma: no cover +def cli(): try: main() except Exception as e: print(e, file=sys.stderr) sys.exit(-1) + + +if __name__ == "__main__": # pragma: no cover + cli()