Skip to content

Commit 07643df

Browse files
authored
Fix failing tests (#19)
1 parent ec055e7 commit 07643df

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

src/tfdocs/__main__.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11
#!/usr/bin/env python
22
# Copyright (c) 2024 Vajeen Karunathilaka <vajeen@gmail.com>
33
#
4-
# MIT License (see header above)
4+
# MIT License
55

66
from __future__ import annotations
77

88
import errno
99
import sys
1010
from pathlib import Path
11-
from typing import Dict, List, Optional
1211

1312
from rich.console import Console
1413

1514
from tfdocs import cli
1615
from tfdocs import readme
1716

1817

19-
def main(argv: Optional[List[str]] = None) -> int:
18+
def main(argv: list[str] | None = None) -> None:
19+
"""
20+
Entry point used by tests. It MUST call sys.exit(..) so tests that
21+
expect SystemExit behave correctly.
22+
"""
2023
if argv is None:
2124
argv = sys.argv
2225

2326
options = cli.get_parser(argv[1:])
2427

2528
if options.version:
2629
print(f"tfdocs {cli.get_version()}")
27-
return 0
30+
sys.exit(0)
2831

29-
module_name: str = options.module_name or Path.cwd().name
32+
module_name = options.module_name or Path.cwd().name
3033

3134
rd = readme.Readme(
32-
readme_file=options.readme_file,
33-
variables_file=options.variables_file,
34-
module_name=module_name,
35-
module_source=options.source,
36-
module_source_git=options.git_source,
35+
options.readme_file,
36+
options.variables_file,
37+
module_name,
38+
options.source,
39+
options.git_source,
3740
)
3841

3942
if not options.dry_run and options.format:
@@ -44,7 +47,7 @@ def main(argv: Optional[List[str]] = None) -> int:
4447
rd.print_variables_file()
4548
if not rd.get_status()["variables"]:
4649
rd.print_readme()
47-
return report_and_exit(
50+
report_and_exit(
4851
rd.get_status(),
4952
options.readme_file,
5053
options.variables_file,
@@ -53,7 +56,7 @@ def main(argv: Optional[List[str]] = None) -> int:
5356
)
5457

5558
rd.print_readme()
56-
return report_and_exit(
59+
report_and_exit(
5760
rd.get_status(),
5861
options.readme_file,
5962
options.variables_file,
@@ -62,7 +65,7 @@ def main(argv: Optional[List[str]] = None) -> int:
6265
)
6366

6467
rd.write_readme()
65-
return report_and_exit(
68+
report_and_exit(
6669
rd.get_status(),
6770
options.readme_file,
6871
options.variables_file,
@@ -72,14 +75,18 @@ def main(argv: Optional[List[str]] = None) -> int:
7275

7376

7477
def report_and_exit(
75-
status: Dict[str, bool],
78+
status: dict[str, bool],
7679
readme_file: str,
7780
variables_file: str,
7881
format_variables: bool,
7982
dry_run: bool,
80-
) -> int:
83+
) -> None:
84+
"""
85+
Print a summary and exit with the expected codes.
86+
Exits -1 when there are updates/pending updates; 0 otherwise.
87+
"""
8188
console = Console()
82-
changed_files: List[str] = []
89+
changed_files: list[str] = []
8390

8491
if status.get("readme"):
8592
changed_files.append(readme_file)
@@ -94,19 +101,22 @@ def report_and_exit(
94101
if not dry_run
95102
else f"[yellow]Pending changes:[/] {changed_list}"
96103
)
97-
return -1
104+
sys.exit(-1)
98105
else:
99106
console.print("[cyan]Nothing to update!!![/]")
100-
return 0
107+
sys.exit(0)
101108

102109

103110
def _cli_entrypoint() -> None:
111+
"""
112+
Console script entrypoint. Mirrors your original signal/errno handling.
113+
"""
104114
try:
105115
sys.exit(main(sys.argv))
106116
except OSError as exc:
107-
if exc.errno != errno.EPIPE:
108-
raise
109-
sys.exit(0)
117+
if exc.errno == errno.EPIPE:
118+
return
119+
raise
110120
except KeyboardInterrupt:
111121
sys.exit(130)
112122
except RuntimeError as exc:

0 commit comments

Comments
 (0)