Skip to content

Commit b7c258f

Browse files
committed
add support for os.path.exists when new --git flag is used
We need to be able to accept args such as /dev/null and /dev/stdin and these do not meet truth test with os.path.isfile. See source-foundry#48 (comment) for discussion
1 parent 57344e7 commit b7c258f

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

lib/fdiff/__main__.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from fdiff.color import color_unified_diff_line
99
from fdiff.diff import external_diff, u_diff
1010
from fdiff.textiter import head, tail
11-
from fdiff.utils import file_exists, get_tables_argument_list
11+
from fdiff.utils import path_exists, get_tables_argument_list
1212

1313

1414
def main(): # pragma: no cover
@@ -86,9 +86,16 @@ def run(argv):
8686
args, positionals = parser.parse_known_args(argv)
8787

8888
inputs = argparse.Namespace()
89+
include_dir_paths = False
8990
if args.git:
9091
inputs.PREFILE = args.git[1]
9192
inputs.POSTFILE = args.git[4]
93+
# If the --git flag is used, we need to accept arguments
94+
# that do not meet the definition of a file path using
95+
# os.path.exists instead of os.path.isfile
96+
# See https://github.com/source-foundry/fdiff/pull/48#discussion_r410424497
97+
# for additional details
98+
include_dir_paths = True
9299
else:
93100
inputparser = argparse.ArgumentParser()
94101
inputparser.add_argument("PREFILE", help="Font file path/URL 1")
@@ -105,12 +112,16 @@ def run(argv):
105112
# File path argument validations
106113
# -------------------------------
107114

108-
if not inputs.PREFILE.startswith("http") and not file_exists(inputs.PREFILE):
115+
if not inputs.PREFILE.startswith("http") and not path_exists(
116+
inputs.PREFILE, include_dir_paths=include_dir_paths
117+
):
109118
sys.stderr.write(
110119
f"[*] ERROR: The file path '{inputs.PREFILE}' can not be found.{os.linesep}"
111120
)
112121
sys.exit(1)
113-
if not inputs.POSTFILE.startswith("http") and not file_exists(inputs.POSTFILE):
122+
if not inputs.POSTFILE.startswith("http") and not path_exists(
123+
inputs.POSTFILE, include_dir_paths=include_dir_paths
124+
):
114125
sys.stderr.write(
115126
f"[*] ERROR: The file path '{inputs.POSTFILE}' can not be found.{os.linesep}"
116127
)

lib/fdiff/utils.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
from datetime import datetime, timezone
66

77

8-
def file_exists(path):
9-
"""Validates file path as existing local file"""
10-
return os.path.exists(path)
8+
def path_exists(path, include_dir_paths=False):
9+
"""Validates existing paths. The include_dir_paths parameter
10+
toggles acceptance of dir paths in addition to file paths."""
11+
if include_dir_paths:
12+
return os.path.exists(path)
13+
else:
14+
return os.path.isfile(path)
1115

1216

1317
def get_file_modtime(path):

tests/test_utils.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
import os
22
import re
33

4-
from fdiff.utils import get_file_modtime, get_tables_argument_list, file_exists
4+
from fdiff.utils import get_file_modtime, get_tables_argument_list, path_exists
55

66
import pytest
77

88

9-
def test_file_exists_true():
10-
assert file_exists(os.path.join("tests", "testfiles", "test.txt")) is True
9+
def test_path_exists_default_true():
10+
assert (
11+
path_exists(
12+
os.path.join("tests", "testfiles", "test.txt"), include_dir_paths=False
13+
)
14+
is True
15+
)
1116

1217

13-
def test_file_exists_false():
14-
assert file_exists(os.path.join("tests", "testfiles", "bogus.jpg")) is False
18+
def test_path_exists_default_false():
19+
assert (
20+
path_exists(
21+
os.path.join("tests", "testfiles", "bogus.jpg"), include_dir_paths=False
22+
)
23+
is False
24+
)
25+
26+
27+
def test_path_exists_default_dirpath_fails():
28+
assert (
29+
path_exists(os.path.join("tests", "testfiles"), include_dir_paths=False)
30+
is False
31+
)
32+
33+
34+
def test_path_exists_default_dirpath_toggle_succeeds():
35+
assert (
36+
path_exists(os.path.join("tests", "testfiles"), include_dir_paths=True) is True
37+
)
1538

1639

1740
def test_get_file_modtime():

tests/test_utils_unix_only.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import os
2+
import sys
3+
4+
import pytest
5+
6+
from fdiff.utils import path_exists
7+
8+
if sys.platform.startswith("win"):
9+
pytest.skip("skipping Unix only tests", allow_module_level=True)
10+
11+
12+
def test_path_exists_default_dirpath_toggle_succeeds():
13+
assert path_exists("/dev/null", include_dir_paths=True) is True

0 commit comments

Comments
 (0)