Skip to content

Commit 641d3d6

Browse files
committed
Fixing has_git to also check if in git repo
1 parent c6bc84a commit 641d3d6

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<p align="center">
2+
<img src="images/logo.png" width="40%" align="middle">
3+
</p>
4+
15
# Typed Argument Parser (Tap)
26

37
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/typed-argument-parser)](https://badge.fury.io/py/typed-argument-parser)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
setup(
77
name='typed-argument-parser',
8-
version='1.4.2',
8+
version='1.4.3',
99
author='Jesse Michel and Kyle Swanson',
1010
1111
description='Typed Argument Parser',
1212
long_description=long_description,
1313
long_description_content_type='text/markdown',
1414
url='https://github.com/swansonk14/typed-argument-parser',
15-
download_url='https://github.com/swansonk14/typed-argument-parser/v_1.4.2.tar.gz',
15+
download_url='https://github.com/swansonk14/typed-argument-parser/v_1.4.3.tar.gz',
1616
license='MIT',
1717
packages=find_packages(),
1818
package_data={'tap': ['py.typed']},

tap/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121
PRIMITIVES = (str, int, float, bool)
2222

2323

24-
def check_output(command: List[str]) -> str:
24+
def check_output(command: List[str], suppress_stderr: bool = True) -> str:
2525
"""Runs subprocess.check_output and returns the result as a string.
2626
2727
:param command: A list of strings representing the command to run on the command line.
28+
:param suppress_stderr: Whether to suppress anything written to standard error.
2829
:return: The output of the command, converted from bytes to string and stripped.
2930
"""
30-
return subprocess.check_output(command).decode('utf-8').strip()
31+
with open(os.devnull, 'w') as devnull:
32+
devnull = devnull if suppress_stderr else None
33+
output = subprocess.check_output(command, stderr=devnull).decode('utf-8').strip()
34+
return output
3135

3236

3337
def has_git() -> bool:
@@ -36,9 +40,9 @@ def has_git() -> bool:
3640
:return: True if git is installed, False otherwise.
3741
"""
3842
try:
39-
subprocess.check_output(['git', '--version'])
40-
return True
41-
except FileNotFoundError:
43+
output = check_output(['git', 'rev-parse', '--is-inside-work-tree'])
44+
return output == 'true'
45+
except (FileNotFoundError, subprocess.CalledProcessError):
4246
return False
4347

4448

tests/test_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing_extensions import Literal
1010

1111
from tap.utils import (
12+
has_git,
1213
get_class_column,
1314
get_class_variables,
1415
get_git_root,
@@ -37,6 +38,15 @@ def setUp(self) -> None:
3738
def tearDown(self) -> None:
3839
self.temp_dir.cleanup()
3940

41+
def test_has_git_true(self) -> None:
42+
self.assertTrue(has_git())
43+
44+
def test_has_git_false(self) -> None:
45+
with TemporaryDirectory() as temp_dir_no_git:
46+
os.chdir(temp_dir_no_git)
47+
self.assertFalse(has_git())
48+
os.chdir(self.temp_dir.name)
49+
4050
def test_get_git_root(self) -> None:
4151
self.assertTrue(get_git_root() in f'/private{self.temp_dir.name}')
4252

0 commit comments

Comments
 (0)