Skip to content

Commit a03e406

Browse files
author
Brian Kohan
committed
fix #44
1 parent 6f53751 commit a03e406

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

django_typer/tests/tests.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,40 @@ def test_group_call(self):
11621162
with self.assertRaises(NotImplementedError):
11631163
get_command("groups")()
11641164

1165+
def test_get_help_from_incongruent_path(self):
1166+
"""
1167+
https://github.com/bckohan/django-typer/issues/44
1168+
"""
1169+
# change dir to the first dir that is not a parent
1170+
cwd = Path(os.getcwd())
1171+
try:
1172+
for directory in os.listdir("/"):
1173+
top_dir = Path(f"/{directory}")
1174+
try:
1175+
cwd.relative_to(top_dir)
1176+
except ValueError:
1177+
# change cwd to the first directory that is not a parent and try
1178+
# to invoke help from there
1179+
os.chdir(top_dir)
1180+
result = subprocess.run(
1181+
[sys.executable, manage_py.absolute(), "groups", "--help"],
1182+
capture_output=True,
1183+
text=True,
1184+
env=os.environ,
1185+
)
1186+
self.assertGreater(
1187+
sim := similarity(
1188+
result.stdout,
1189+
(
1190+
TESTS_DIR / "test_app" / "helps" / f"groups.txt"
1191+
).read_text(),
1192+
),
1193+
0.96, # width inconsistences drive this number < 1
1194+
)
1195+
return
1196+
finally:
1197+
os.chdir(cwd)
1198+
11651199
@override_settings(
11661200
INSTALLED_APPS=[
11671201
"django_typer.tests.test_app",

django_typer/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def get_usage_script(script: t.Optional[str] = None) -> t.Union[Path, str]:
2525
cmd_pth = Path(script or sys.argv[0])
2626
if shutil.which(cmd_pth.name):
2727
return cmd_pth.name
28-
return cmd_pth.absolute().relative_to(Path(os.getcwd()))
28+
try:
29+
return cmd_pth.absolute().relative_to(Path(os.getcwd()))
30+
except ValueError:
31+
return cmd_pth.absolute()
2932

3033

3134
def traceback_config() -> t.Union[bool, t.Dict[str, t.Any]]:

0 commit comments

Comments
 (0)