Skip to content

Commit e022585

Browse files
authored
Merge pull request #1633 from brandonpayton/fix-config-type-error-reporting
Fix configobj to relay tuple type errors
2 parents 4ce9178 + 48da2de commit e022585

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

alot/utils/configobj.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,15 @@ def width_tuple(value):
7777
raise VdtTypeError(value)
7878
elif value[0] not in ['fit', 'weight']:
7979
raise VdtTypeError(value)
80-
if value[0] == 'fit':
81-
if not isinstance(value[1], int) or not isinstance(value[2], int):
82-
VdtTypeError(value)
83-
res = 'fit', int(value[1]), int(value[2])
84-
else:
85-
if not isinstance(value[1], int):
86-
VdtTypeError(value)
87-
res = 'weight', int(value[1])
80+
try:
81+
if value[0] == 'fit':
82+
res = 'fit', int(value[1]), int(value[2])
83+
else:
84+
res = 'weight', int(value[1])
85+
except IndexError:
86+
raise VdtTypeError(value)
87+
except ValueError:
88+
raise VdtValueError(value)
8889
return res
8990

9091

tests/utils/test_configobj.py

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import unittest
33

44
from alot.utils import configobj as checks
5+
from validate import VdtTypeError, VdtValueError
56

67
# Good descriptive test names often don't fit PEP8, which is meant to cover
78
# functions meant to be called by humans.
@@ -17,3 +18,34 @@ def test_strings_are_converted_to_single_item_lists(self):
1718
def test_empty_strings_are_converted_to_empty_lists(self):
1819
forced = checks.force_list('')
1920
self.assertEqual(forced, [])
21+
22+
23+
class TestWidthTuple(unittest.TestCase):
24+
25+
def test_validates_width_tuple(self):
26+
with self.assertRaises(VdtTypeError):
27+
checks.width_tuple('invalid-value')
28+
29+
def test_validates_width_tuple_for_fit_requires_two_args(self):
30+
with self.assertRaises(VdtTypeError):
31+
checks.width_tuple(['fit', 123])
32+
33+
def test_args_for_fit_must_be_numbers(self):
34+
with self.assertRaises(VdtValueError):
35+
checks.width_tuple(['fit', 123, 'not-a-number'])
36+
37+
def test_fit_with_two_numbers(self):
38+
fit_result = checks.width_tuple(['fit', 123, 456])
39+
self.assertEqual(('fit', 123, 456), fit_result)
40+
41+
def test_validates_width_tuple_for_weight_needs_an_argument(self):
42+
with self.assertRaises(VdtTypeError):
43+
checks.width_tuple(['weight'])
44+
45+
def test_arg_for_width_must_be_a_number(self):
46+
with self.assertRaises(VdtValueError):
47+
checks.width_tuple(['weight', 'not-a-number'])
48+
49+
def test_width_with_a_number(self):
50+
weight_result = checks.width_tuple(['weight', 123])
51+
self.assertEqual(('weight', 123), weight_result)

0 commit comments

Comments
 (0)