-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes a path bug for the conda-debug command
- Loading branch information
1 parent
3268fdd
commit d2ca8de
Showing
10 changed files
with
228 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
from argparse import ArgumentParser, Namespace | ||
from functools import wraps | ||
from typing import Sequence, Callable, Tuple | ||
|
||
from conda_build.utils import CONDA_PACKAGE_EXTENSIONS | ||
from conda_build import utils | ||
|
||
ParserFunction = Callable[..., Tuple[ArgumentParser, Namespace]] | ||
ValidatorFunction = Callable[[str, Namespace], str] | ||
|
||
|
||
def validate_args( | ||
validators: Sequence[Tuple[str, ValidatorFunction]], | ||
parser: ArgumentParser, | ||
): | ||
""" | ||
Runs a set of validation rules for a command. We assume that the first positional | ||
argument is and | ||
""" | ||
def outer_wrap(func): | ||
@wraps(func) | ||
def wrapper(*args_, **kwargs): | ||
args = sys.argv[1:] | ||
cmd_args = parser.parse_args(args) | ||
|
||
for arg, validate in validators: | ||
arg_val = getattr(cmd_args, arg) | ||
setattr(cmd_args, arg, validate(arg_val, cmd_args)) | ||
|
||
return func(cmd_args, *args_, **kwargs) | ||
return wrapper | ||
return outer_wrap | ||
|
||
|
||
def get_is_conda_pkg_or_recipe_error_message() -> str: | ||
"""Return the error displayed on the `validate_is_conda_pkg_or_recipe_dir` validator""" | ||
valid_ext_str = ' or '.join(CONDA_PACKAGE_EXTENSIONS) | ||
return ( | ||
'Error: Unable to parse provided recipe directory or package file.\n\n' | ||
f'Please make sure this argument is either a valid package \n' | ||
f'file ({valid_ext_str}) or points to a directory containing recipe.' | ||
) | ||
|
||
|
||
def validate_is_conda_pkg_or_recipe_dir(arg_val: str, _: Namespace) -> str: | ||
""" | ||
Makes sure the argument is either a conda pkg file or a recipe directory. | ||
""" | ||
if os.path.isdir(arg_val): | ||
return arg_val | ||
elif utils.is_conda_pkg(arg_val): | ||
return arg_val | ||
else: | ||
sys.stderr.write(get_is_conda_pkg_or_recipe_error_message()) | ||
sys.exit(1) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import io | ||
import sys | ||
from unittest import mock | ||
|
||
import pytest | ||
from pytest import CaptureFixture | ||
from pyfakefs.fake_filesystem import FakeFilesystem | ||
|
||
from conda_build.cli import main_debug as debug | ||
from conda_build import validators as valid | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def main_debug_help() -> str: | ||
"""Read what the current help message should be and return it as a fixture""" | ||
parser = debug.get_parser() | ||
|
||
with io.StringIO() as fp: | ||
parser.print_usage(file=fp) | ||
fp.seek(0) | ||
return fp.read() | ||
|
||
|
||
def test_main_debug_help_message(capsys: CaptureFixture, main_debug_help: str): | ||
sys.argv = ['conda-debug'] | ||
|
||
with pytest.raises(SystemExit): | ||
debug.main() | ||
|
||
captured = capsys.readouterr() | ||
assert main_debug_help in captured.err | ||
|
||
|
||
def test_main_debug_file_does_not_exist(capsys: CaptureFixture): | ||
sys.argv = ['conda-debug', 'file-does-not-exist'] | ||
|
||
with pytest.raises(SystemExit): | ||
debug.main() | ||
|
||
captured = capsys.readouterr() | ||
assert captured.err == valid.get_is_conda_pkg_or_recipe_error_message() | ||
|
||
|
||
def test_main_debug_happy_path(fs: FakeFilesystem, capsys: CaptureFixture): | ||
""" | ||
Happy path through the main_debug.main function. | ||
""" | ||
with mock.patch('conda_build.api.debug') as mock_debug: | ||
fake_pkg_file = 'fake-conda-pkg.conda' | ||
fs.create_file(fake_pkg_file) | ||
sys.argv = ['conda-debug', fake_pkg_file] | ||
|
||
debug.main() | ||
|
||
caputured = capsys.readouterr() | ||
|
||
assert caputured.err == '' | ||
|
||
assert len(mock_debug.mock_calls) == 2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ glob2 | |
jinja2 | ||
pkginfo | ||
psutil | ||
pyfakefs | ||
pytest | ||
pytest-cov | ||
pytest-mock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from argparse import Namespace | ||
|
||
import pytest | ||
from pyfakefs.fake_filesystem import FakeFilesystem | ||
|
||
from conda_build import validators as valid | ||
|
||
IS_CONDA_PACKAGE_OR_DIR_DATA = ( | ||
('aws-c-common-0.4.57-hb1e8313_1.tar.bz2', True, False, True), | ||
('somedir', True, True, True), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize('value,expected,is_dir,create', IS_CONDA_PACKAGE_OR_DIR_DATA) | ||
def test_validate_is_conda_pkg_or_recipe_dir( | ||
fs: FakeFilesystem, value: str, expected: bool, is_dir: bool, create: bool | ||
): | ||
if create: | ||
if is_dir: | ||
fs.create_dir(value) | ||
else: | ||
fs.create_file(value) | ||
name_space = Namespace() # intentionally left empty because our validator doesn't need it | ||
|
||
assert valid.validate_is_conda_pkg_or_recipe_dir(value, name_space) |