From 7324552d9f89e18046bf4d511317a940105287e2 Mon Sep 17 00:00:00 2001 From: tau3 Date: Thu, 15 Feb 2018 20:11:00 +0400 Subject: Fixed start path resolving in case of absent working directory --- .gitignore | 2 ++ ranger/core/main.py | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 88c75b90..3d946b72 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ /ranger_fm.egg-info /stuff/* + +.idea \ No newline at end of file diff --git a/ranger/core/main.py b/ranger/core/main.py index 4adea918..5f5af332 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -93,10 +93,7 @@ def main( args.selectfile = os.path.abspath(args.selectfile) args.paths.insert(0, os.path.dirname(args.selectfile)) - if args.paths: - paths = [p[7:] if p.startswith('file:///') else p for p in args.paths] - else: - paths = [os.environ.get('PWD', os.getcwd())] + paths = __get_paths(args) paths_inaccessible = [] for path in paths: try: @@ -235,6 +232,24 @@ https://github.com/ranger/ranger/issues return exit_code # pylint: disable=lost-exception +def __get_paths(args): + if args.paths: + prefix = 'file:///' + prefix_length = len(prefix) + paths = [path[prefix_length:] if path.startswith(prefix) else path for path in args.paths] + else: + start_directory = os.environ.get('PWD') + is_valid_start_directory = start_directory and os.path.exists(start_directory) + if not is_valid_start_directory: + start_directory = __get_home_directory() + paths = [start_directory] + return paths + + +def __get_home_directory(): + return os.path.expanduser('~') + + def xdg_path(env_var): path = os.environ.get(env_var) if path and os.path.isabs(path): -- cgit 1.4.1-2-gfad0 From 2c4322cac434f767ede180b9724f77e2a7845367 Mon Sep 17 00:00:00 2001 From: tau3 Date: Thu, 15 Feb 2018 21:11:47 +0400 Subject: Implemented unit test for recent fix --- .gitignore | 3 ++- ranger/core/main.py | 4 ++-- tests/ranger/core/__init__.py | 0 tests/ranger/core/test_main.py | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 tests/ranger/core/__init__.py create mode 100644 tests/ranger/core/test_main.py diff --git a/.gitignore b/.gitignore index 3d946b72..73ca85e6 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ /stuff/* -.idea \ No newline at end of file +.idea +.pytest_cache diff --git a/ranger/core/main.py b/ranger/core/main.py index 5f5af332..595ebfae 100644 --- a/ranger/core/main.py +++ b/ranger/core/main.py @@ -93,7 +93,7 @@ def main( args.selectfile = os.path.abspath(args.selectfile) args.paths.insert(0, os.path.dirname(args.selectfile)) - paths = __get_paths(args) + paths = get_paths(args) paths_inaccessible = [] for path in paths: try: @@ -232,7 +232,7 @@ https://github.com/ranger/ranger/issues return exit_code # pylint: disable=lost-exception -def __get_paths(args): +def get_paths(args): if args.paths: prefix = 'file:///' prefix_length = len(prefix) diff --git a/tests/ranger/core/__init__.py b/tests/ranger/core/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/ranger/core/test_main.py b/tests/ranger/core/test_main.py new file mode 100644 index 00000000..d992b8a7 --- /dev/null +++ b/tests/ranger/core/test_main.py @@ -0,0 +1,18 @@ +import collections +import os + +from ranger.core import main + + +def test_get_paths(): + args_tuple = collections.namedtuple('args', 'paths') + args = args_tuple(paths=None) + + paths = main.get_paths(args) + + for path in paths: + assert os.path.exists(path) + + +if __name__ == '__main__': + test_get_paths() -- cgit 1.4.1-2-gfad0