Skip to content

Commit

Permalink
Remove sys.exit from develop.py (#5453)
Browse files Browse the repository at this point in the history
* Replace sys.exit in develop.py with CondaBuildUserError

* Add unit tests

* Update new test_get_setup_py

* Apply suggestions from code review

Co-authored-by: Ken Odegard <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Ken Odegard <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 12, 2024
1 parent 0b772d4 commit 45fe475
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
22 changes: 13 additions & 9 deletions conda_build/develop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
import shutil
import sys
from os.path import abspath, exists, expanduser, isdir, join
from pathlib import Path
from typing import TYPE_CHECKING

from .exceptions import CondaBuildUserError
from .os_utils.external import find_executable
from .post import mk_relative_osx
from .utils import check_call_env, get_site_packages, on_mac, rec_glob

if TYPE_CHECKING:
from pathlib import Path


def relink_sharedobjects(pkg_path, build_prefix):
"""
Expand Down Expand Up @@ -56,13 +62,13 @@ def write_to_conda_pth(sp_dir, pkg_path):
print("added " + pkg_path)


def get_setup_py(path_):
"""Return full path to setup.py or exit if not found"""
def get_setup_py(path_: Path) -> Path:
"""Return full path to setup.py or raise error if not found"""
# build path points to source dir, builds are placed in the
setup_py = join(path_, "setup.py")

if not exists(setup_py):
sys.exit(f"No setup.py found in {path_}. Exiting.")
raise CondaBuildUserError(f"No setup.py found in {path_}.")

return setup_py

Expand Down Expand Up @@ -136,12 +142,10 @@ def execute(
uninstall: bool = False,
) -> None:
if not isdir(prefix):
sys.exit(
f"""\
Error: environment does not exist: {prefix}
#
# Use 'conda create' to create the environment first.
#"""
raise CondaBuildUserError(
f"Error: environment does not exist: {prefix}\n"
f"\n"
f"Use 'conda create' to create the environment first."
)

assert find_executable("python", prefix=prefix)
Expand Down
20 changes: 19 additions & 1 deletion tests/test_develop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

import pytest

from conda_build.develop import _uninstall, write_to_conda_pth
from conda_build.develop import _uninstall, execute, get_setup_py, write_to_conda_pth
from conda_build.exceptions import CondaBuildUserError
from conda_build.utils import rm_rf

from .utils import thisdir
Expand Down Expand Up @@ -99,3 +100,20 @@ def test_uninstall(site_packages: Path, conda_pth: Path):
_uninstall(site_packages, path)

assert list(filter(None, conda_pth.read_text().split("\n"))) == develop_paths


def test_get_setup_py(tmp_path: Path):
setup_py_path = tmp_path / "setup.py"
setup_py_path.touch()
result = get_setup_py(str(tmp_path))
assert "setup.py" in result

with pytest.raises(CondaBuildUserError, match="No setup.py found in "):
get_setup_py("/path/to/non-existent")


def test_execute_error_nonexistent_prefix():
with pytest.raises(
CondaBuildUserError, match="Error: environment does not exist: "
):
execute("/path/to/non-existent/prefix", "python", "setup.py", "install")

0 comments on commit 45fe475

Please sign in to comment.