Skip to content

Commit

Permalink
tests passing locally
Browse files Browse the repository at this point in the history
  • Loading branch information
kalefranz committed Jul 28, 2016
1 parent 2affca1 commit 84189af
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ junit.xml
.version
tmpfile.rc
.DS_Store
.coverage*
4 changes: 3 additions & 1 deletion conda/_vendor/auxlib/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
def isiterable(obj):
# and not a string
if PY2:
return hasattr(obj, '__iter__') and not isinstance(obj, string_types)
return (hasattr(obj, '__iter__')
and not isinstance(obj, string_types)
and type(obj) is not type)
else:
return not isinstance(obj, string_types) and isinstance(obj, collections.Iterable)
6 changes: 5 additions & 1 deletion conda/_vendor/auxlib/type_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ def typify(value, type_hint=None):
raise NotImplementedError()
elif type_hint is not None:
# coerce using the type hint, or use boolify for bool
return boolify(value) if type_hint == bool else type_hint(value)
try:
return boolify(value) if type_hint == bool else type_hint(value)
except ValueError as e:
# ValueError: invalid literal for int() with base 10: 'nope'
raise TypeCoercionError(value, text_type(e))
else:
# no type hint, but we know value is a string, so try to match with the regex patterns
candidate = _REGEX.convert(value)
Expand Down
5 changes: 0 additions & 5 deletions conda/cli/main_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,10 @@ def execute_config(args, parser):
json_get = {}

if args.show_sources:
from conda.base.context import context
print(context.dump_locations())
return

if args.show:
from conda.base.context import context
d = dict((key, getattr(context, key))
for key in ('add_anaconda_token',
'add_pip_as_python_dependency',
Expand Down Expand Up @@ -269,12 +267,9 @@ def execute_config(args, parser):
return

if args.validate:
from conda.base.context import context
context.validate_all()
return



if args.system:
rc_path = sys_rc_path
elif args.file:
Expand Down
48 changes: 24 additions & 24 deletions conda/common/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

def pretty_list(iterable): # TODO: move to conda.common
if not isiterable(iterable):
iterable = list(iterable)
return "\n - ".join(chain.from_iterable((('',), iterable)))
iterable = [iterable]
return ''.join(" - %s\n" % item for item in iterable)


class ConfigurationError(CondaError):
Expand All @@ -63,11 +63,12 @@ def __init__(self, parameter_name, parameter_value, source, msg=None, **kwargs):
self.parameter_name = parameter_name
self.parameter_value = parameter_value
self.source = source
if msg is None:
msg = ("Parameter %s = %r declared in %s is invalid."
% (parameter_name, parameter_value, source))
super(ConfigurationError, self).__init__(msg, **kwargs)

def __str__(self):
return ("Parameter %s = %r declared in %s is invalid."
% (self.parameter_name, self.parameter_value, self.source))


class MultipleKeysError(ValidationError):

Expand All @@ -93,9 +94,11 @@ def __init__(self, parameter_name, parameter_value, source, wrong_type, valid_ty
class InvalidElementTypeError(InvalidTypeError):
def __init__(self, parameter_name, parameter_value, source, wrong_type,
valid_types, index_or_key):
msg = ("Parameter %s declared in %s has invalid element %r at index %d.\n"
"Valid element types: %s." % (parameter_name, source, parameter_value,
index_or_key, pretty_list(valid_types)))
qualifier = "at index" if isinstance(index_or_key, int) else "for key"
msg = ("Parameter %s declared in %s has invalid element %r %s %s.\n"
"Valid element types:\n"
"%s." % (parameter_name, source, parameter_value, qualifier,
index_or_key, pretty_list(valid_types)))
super(InvalidElementTypeError, self).__init__(parameter_name, parameter_value, source,
wrong_type, valid_types, msg=msg)

Expand Down Expand Up @@ -413,12 +416,13 @@ def __get__(self, instance, instance_type):

matches, errors = self._get_all_matches(instance)
try:
result = typify_data_structure(self._merge(matches) if matches else self.default)
result = typify_data_structure(self._merge(matches) if matches else self.default,
self._element_type)
except TypeCoercionError as e:
if 'result' not in locals():
result = None
errors.append(CustomValidationError(self.name, e.value, "<<merged>>", text_type(e)))
self.validate_and_raise(instance, result, errors)
else:
errors.extend(self.collect_errors(instance, result))
self.raise_errors(errors)
instance._cache[self.name] = result
return result

Expand All @@ -438,8 +442,8 @@ def collect_errors(self, instance, value, source="<<merged>>"):
"""
errors = []
if not isinstance(value, self._type):
errors.append([InvalidTypeError(self.name, value, source, type(value),
self._type)])
errors.append(InvalidTypeError(self.name, value, source, type(value),
self._type))
elif self._validation is not None:
result = self._validation(value)
if result is False:
Expand All @@ -448,9 +452,8 @@ def collect_errors(self, instance, value, source="<<merged>>"):
errors.append(CustomValidationError(self.name, value, source, result))
return errors

def validate_and_raise(self, instance, value, other_errors=()):
errors = other_errors or []
errors.extend(self.collect_errors(instance, value))
@staticmethod
def raise_errors(errors):
if not errors:
return True
elif len(errors) == 1:
Expand Down Expand Up @@ -610,12 +613,9 @@ def __init__(self, element_type, default=None, aliases=(), validation=None):

def collect_errors(self, instance, value, source="<<merged>>"):
errors = super(MapParameter, self).collect_errors(instance, value)

element_type = self._element_type
for key, val in iteritems(value):
if not isinstance(val, element_type):
errors.append(InvalidElementTypeError(self.name, val, source,
type(val), element_type, key))
errors.extend(InvalidElementTypeError(self.name, val, source, type(val), element_type, key)
for key, val in iteritems(value) if not isinstance(val, element_type))
return errors

def _merge(self, matches):
Expand Down Expand Up @@ -729,8 +729,8 @@ def check_source(self, source):
typed_value = typify_data_structure(match.value(parameter.__class__),
parameter._element_type)
except TypeCoercionError as e:
validation_errors.append(CustomValidationError(match.key, e.value, match.source, text_type(e)))

validation_errors.append(CustomValidationError(match.key, e.value,
match.source, text_type(e)))
else:
validation_result = parameter.collect_errors(self, typed_value, match.source)
if validation_result is not True:
Expand Down
4 changes: 2 additions & 2 deletions conda/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def __init__(self, message, *args, **kwargs):
super(CondaSignatureError, self).__init__(msg, *args, **kwargs)


def print_exception(exception):
def print_conda_exception(exception):
from conda.base.context import context
from conda.cli.common import stdout_json
from sys import stderr
Expand Down Expand Up @@ -406,7 +406,7 @@ def conda_exception_handler(func, *args, **kwargs):
if context.debug:
print_unexpected_error_message(e)
else:
print_exception(e)
print_conda_exception(e)
return 1
except Exception as e:
print_unexpected_error_message(e)
Expand Down
17 changes: 9 additions & 8 deletions tests/common/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from conda._vendor.auxlib.ish import dals
from conda.common.compat import odict, string_types
from conda.common.configuration import Configuration, MapParameter, ParameterFlag, \
PrimitiveParameter, SequenceParameter, YamlRawParameter, load_file_configs
from conda.common.configuration import (Configuration, MapParameter, ParameterFlag,
PrimitiveParameter, SequenceParameter, YamlRawParameter,
load_file_configs, MultiValidationError)
from conda.common.yaml import yaml_load
from conda.exceptions import ValidationError as CondaValidationError
from conda.common.configuration import ValidationError
from os import environ, mkdir
from os.path import join
from pytest import raises
Expand Down Expand Up @@ -309,16 +310,16 @@ def test_list_merges(self):

def test_validation(self):
config = TestConfiguration()._add_raw_data(load_from_string_data('bad_boolean'))
raises(CondaValidationError, lambda: config.always_yes)
raises(ValidationError, lambda: config.always_yes)

config = TestConfiguration()._add_raw_data(load_from_string_data('too_many_aliases'))
raises(CondaValidationError, lambda: config.always_yes)
raises(ValidationError, lambda: config.always_yes)

config = TestConfiguration()._add_raw_data(load_from_string_data('not_an_int'))
raises(CondaValidationError, lambda: config.always_an_int)
raises(ValidationError, lambda: config.always_an_int)

config = TestConfiguration()._add_raw_data(load_from_string_data('bad_boolean_map'))
raises(CondaValidationError, lambda: config.boolean_map)
raises(ValidationError, lambda: config.boolean_map)

config = TestConfiguration()._add_raw_data(load_from_string_data('good_boolean_map'))
assert config.boolean_map['a_true'] is True
Expand All @@ -336,4 +337,4 @@ def test_validate_all(self):
config.validate_all()

config = TestConfiguration()._add_raw_data(load_from_string_data('bad_boolean_map'))
raises(CondaValidationError, config.validate_all)
raises(MultiValidationError, config.validate_all)

0 comments on commit 84189af

Please sign in to comment.