Skip to content

Commit

Permalink
Remove sys.exit calls from convert.py (#5451)
Browse files Browse the repository at this point in the history
* Replace sys.exit with CondaBuildUserError exception in convert.py

* Update unit tests

* Update unit tests

* Replace sys.exit call in convert.py with a return statement, update unit tests
  • Loading branch information
beeankha authored Aug 12, 2024
1 parent 45fe475 commit 7816bc8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
10 changes: 6 additions & 4 deletions conda_build/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import os
import re
import shutil
import sys
import tarfile
import tempfile
from pathlib import Path
from typing import TYPE_CHECKING

from .exceptions import CondaBuildUserError
from .utils import ensure_list, filter_info_files, walk

if TYPE_CHECKING:
Expand Down Expand Up @@ -818,13 +818,15 @@ def conda_convert(
else:
for c_extension in imports:
print(c_extension)
sys.exit()
return

if not show_imports and len(platforms) == 0:
sys.exit("Error: --platform option required for conda package conversion.")
raise CondaBuildUserError(
"Error: --platform option required for conda package conversion."
)

if len(retrieve_c_extensions(file_path)) > 0 and not force:
sys.exit(
raise CondaBuildUserError(
f"WARNING: Package {os.path.basename(file_path)} contains C extensions; skipping conversion. "
"Use -f to force conversion."
)
Expand Down
27 changes: 12 additions & 15 deletions tests/test_api_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from conda.gateways.connection.download import download

from conda_build import api
from conda_build.exceptions import CondaBuildUserError
from conda_build.utils import on_win, package_has_file

from .utils import assert_package_consistency, metadata_dir
Expand Down Expand Up @@ -60,8 +61,7 @@ def test_show_imports(base_platform, package, capfd):
download(f, fn)

for platform in platforms:
with pytest.raises(SystemExit):
api.convert(fn, platforms=platform, show_imports=True)
api.convert(fn, platforms=platform, show_imports=True)

output, error = capfd.readouterr()

Expand All @@ -80,8 +80,7 @@ def test_no_imports_found(base_platform, package, capfd):
fn = f"{package_name}-py36_0.tar.bz2"
download(f, fn)

with pytest.raises(SystemExit):
api.convert(fn, platforms=None, show_imports=True)
api.convert(fn, platforms=None, show_imports=True)

output, error = capfd.readouterr()
assert "No imports found." in output
Expand All @@ -96,13 +95,12 @@ def test_no_platform(base_platform, package):
fn = f"{package_name}-py36_0.tar.bz2"
download(f, fn)

with pytest.raises(SystemExit) as e:
with pytest.raises(
CondaBuildUserError,
match="Error: --platform option required for conda package conversion.",
):
api.convert(fn, platforms=None)

assert "Error: --platform option required for conda package conversion." in str(
e.value
)


@pytest.mark.parametrize("base_platform", ["linux", "win", "osx"])
@pytest.mark.parametrize("package", [("cryptography-1.8.1", "__about__.py")])
Expand All @@ -121,14 +119,13 @@ def test_c_extension_error(base_platform, package):
download(f, fn)

for platform in platforms:
with pytest.raises(SystemExit) as e:
with pytest.raises(
CondaBuildUserError,
match=f"WARNING: Package {fn} contains C extensions; skipping conversion. "
"Use -f to force conversion.",
):
api.convert(fn, platforms=platform)

assert (
f"WARNING: Package {fn} contains C extensions; skipping conversion. "
"Use -f to force conversion."
) in str(e.value)


@pytest.mark.parametrize("base_platform", ["linux", "win", "osx"])
@pytest.mark.parametrize("package", [("cryptography-1.8.1", "__about__.py")])
Expand Down

0 comments on commit 7816bc8

Please sign in to comment.