Skip to content

Commit 0a42d85

Browse files
committed
Add test cases for non-core logic
* Adds test cases for handling argument parsing. Had to adjust how arguments were getting passed around slightly. Also makes the user pass long args explicitly * Adds test cases for interactive and non-interactive modes * Adds test cases for git operations. Had to add edge cases to git operations to handle a few edge cases missed in the code.
1 parent 17af0ef commit 0a42d85

6 files changed

+993
-91
lines changed

git_py_stats/arg_parser.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from argparse import ArgumentParser, Namespace
1010
from typing import List, Optional
1111

12-
1312
def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
1413
"""
1514
Parse command-line arguments and return them.
@@ -24,9 +23,10 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
2423
args = parse_arguments(['--detailed-git-stats'])
2524
print(args.detailed_git_stats) # True
2625
"""
27-
26+
2827
parser = ArgumentParser(
29-
description="Git Py Stats - A Python Implementation of Git Quick Stats."
28+
description="Git Py Stats - A Python Implementation of Git Quick Stats.",
29+
allow_abbrev=False, # Force users to be explicit. Makes testing sane.
3030
)
3131

3232
# Generate Options
@@ -40,6 +40,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
4040
"-R",
4141
"--git-stats-by-branch",
4242
metavar="BRANCH",
43+
type=str,
4344
help="See detailed list of git stats by branch",
4445
)
4546
parser.add_argument(
@@ -52,6 +53,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
5253
"-L",
5354
"--changelogs-by-author",
5455
metavar='"AUTHOR NAME"',
56+
type=str,
5557
help="See changelogs by author",
5658
)
5759
parser.add_argument(
@@ -96,6 +98,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
9698
"-n",
9799
"--new-contributors",
98100
metavar="DATE",
101+
type=str,
99102
help="List everyone who made their first contribution since a specified date",
100103
)
101104
parser.add_argument(
@@ -132,6 +135,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
132135
"-W",
133136
"--commits-by-author-by-weekday",
134137
metavar='"AUTHOR NAME"',
138+
type=str,
135139
help="Displays a list of commits per weekday by author",
136140
)
137141
parser.add_argument(
@@ -144,6 +148,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
144148
"-A",
145149
"--commits-by-author-by-hour",
146150
metavar='"AUTHOR NAME"',
151+
type=str,
147152
help="Displays a list of commits per hour by author",
148153
)
149154
parser.add_argument(
@@ -156,6 +161,7 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
156161
"-Z",
157162
"--commits-by-author-by-timezone",
158163
metavar='"AUTHOR NAME"',
164+
type=str,
159165
help="Displays a list of commits per timezone by author",
160166
)
161167

@@ -169,4 +175,4 @@ def parse_arguments(argv: Optional[List[str]] = None) -> Namespace:
169175

170176
# Help option inherited from argparse by default, no need to impl them.
171177

172-
return parser.parse_args()
178+
return parser.parse_args(argv)

git_py_stats/git_operations.py

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ def run_git_command(cmd: List[str]) -> Optional[str]:
1616
Returns:
1717
The standard output from the git command if successful, None otherwise.
1818
"""
19+
if not cmd:
20+
print("Error: Command list is empty!")
21+
return None
1922
try:
2023
result = subprocess.run(
2124
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True
@@ -24,6 +27,10 @@ def run_git_command(cmd: List[str]) -> Optional[str]:
2427
except subprocess.CalledProcessError as e:
2528
print(f"Error running command: {e}")
2629
return None
30+
# Grab any other possible exception
31+
except Exception as e:
32+
print(f"Unexpected error running command: {e}")
33+
return None
2734

2835

2936
def check_git_repository() -> bool:

0 commit comments

Comments
 (0)