Skip to content

Commit

Permalink
Rename get_root() to find_root()
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleve committed May 23, 2022
1 parent d3e9e4a commit 26710ac
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# pyrootutils

[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml/badge.svg)](https://github.com/ashleve/pyrootutils/actions/workflows/tests.yaml)
[![Python](https://img.shields.io/badge/python-3.7+-blue.svg)](https://www.python.org/downloads/release/python-370/)
[![Tests](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml/badge.svg?branch=main&event=push)](https://github.com/ashleve/pyrootutils/actions/workflows/test.yml)
[![Issues](https://img.shields.io/github/issues/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/issues)
[![License](https://img.shields.io/github/license/ashleve/pyrootutils)](https://github.com/ashleve/pyrootutils/blob/main/LICENSE)
[![Release](https://img.shields.io/pypi/v/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)
[![PyPi](https://img.shields.io/pypi/dm/pyrootutils)](pypi.org/project/pyrootutils/1.0.0/)

A simple python package to solve all your problems with pythonpath, working directory, file paths, module imports and environment variables.

Expand Down Expand Up @@ -33,10 +38,10 @@ import pyrootutils
# find absolute root path (searches for directory containing .project-root file)
# search starts from current file and recursively goes over parent directories
# returns pathlib object
path = pyrootutils.get_root(search_from=__file__, indicator=".project-root")
path = pyrootutils.find_root(search_from=__file__, indicator=".project-root")

# find absolute root path (searches for directory containing any of the files on the list)
path = pyrootutils.get_root(search_from=__file__, indicator=[".git", "setup.cfg"])
path = pyrootutils.find_root(search_from=__file__, indicator=[".git", "setup.cfg"])

# take advantage of the pathlib syntax
data_dir = path / "data"
Expand All @@ -56,7 +61,7 @@ pyrootutils.set_root(
import pyrootutils


# combines get_root() and set_root() into one method
# combines find_root() and set_root() into one method
root = pyrootutils.setup_root(
search_from=__file__,
indicator=".git" # indicator of the root directory
Expand Down
4 changes: 2 additions & 2 deletions pyrootutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .pyrootutils import get_root, set_root, setup_root
from .pyrootutils import find_root, set_root, setup_root

__all__ = ["get_root", "set_root", "setup_root"]
__all__ = ["find_root", "set_root", "setup_root"]
13 changes: 3 additions & 10 deletions pyrootutils/pyrootutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def _pyrootutils_recursive_search(path: Path, indicators: Iterable[str]) -> Path
return _pyrootutils_recursive_search(path.parent, indicators)


def get_root(
search_from: Union[str, Path],
def find_root(
search_from: Union[str, Path] = ".",
indicator: Union[str, Iterable[str]] = ".project-root",
) -> Path:
"""Recursively searches for project root indicator(s), starting from given path.
Expand Down Expand Up @@ -87,7 +87,6 @@ def set_root(
Raises:
FileNotFoundError: if root path does not exist.
Exception: if all options are False.
Returns:
None
Expand All @@ -97,12 +96,6 @@ def set_root(
if not os.path.exists(path):
raise FileNotFoundError("Project root path does not exist.")

if not pythonpath and not cwd and not project_root_env_var and not dotenv:
raise Exception(
"No options selected. \
<pythonpath=False>, <cwd=False>, <project_root_env_var=False>, <dotenv=False>."
)

if pythonpath:
sys.path.insert(0, path)

Expand Down Expand Up @@ -137,6 +130,6 @@ def setup_root(
Returns:
Path: path to project root.
"""
path = get_root(search_from, indicator)
path = find_root(search_from, indicator)
set_root(path, pythonpath, cwd, project_root_env_var, dotenv)
return path
35 changes: 16 additions & 19 deletions tests/test_pyrootutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,61 @@

import pytest

from pyrootutils import get_root, set_root, setup_root
from pyrootutils import find_root, set_root, setup_root


def test_pyrootutils():
assert get_root
assert find_root
assert set_root
assert setup_root


def test_get_root():
path = get_root(__file__)
def test_find_root():
path = find_root(__file__)
assert path.exists()

path = get_root(str(__file__))
path = find_root(str(__file__))
assert path.exists()

path = get_root(Path(__file__))
path = find_root(Path(__file__))
assert path.exists()

path = get_root(__file__, ".git")
path = find_root(__file__, ".git")
assert path.exists()

path = get_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"])
path = find_root(__file__, indicator=[".setup.cfg", "setup.py", "LICENSE"])
assert path.exists()

path = get_root("")
path = find_root("")
assert path.exists()

path = get_root(".")
path = find_root(".")
assert path.exists()

with pytest.raises(FileNotFoundError):
path = get_root(__file__, indicator="abc")
path = find_root(__file__, indicator="abc")

with pytest.raises(FileNotFoundError):
path = get_root(__file__, indicator=["abc", "def", "fgh"])
path = find_root(__file__, indicator=["abc", "def", "fgh"])

with pytest.raises(FileNotFoundError):
path = get_root(Path(__file__).parent.parent.parent)
path = find_root(Path(__file__).parent.parent.parent)

with pytest.raises(TypeError):
path = get_root([])
path = find_root([])

with pytest.raises(TypeError):
path = get_root("", ["abs", "def", 42])
path = find_root("", ["abs", "def", 42])


def test_set_root():

path = get_root(__file__)
path = find_root(__file__)
assert path.exists()

os.chdir(path.parent)
assert os.getcwd() != str(path)

with pytest.raises(Exception):
set_root(path, pythonpath=False, cwd=False, project_root_env_var=False, dotenv=False)

assert "PROJECT_ROOT" not in os.environ

set_root(path, pythonpath=False, cwd=False, project_root_env_var=True, dotenv=False)
Expand Down

0 comments on commit 26710ac

Please sign in to comment.