Skip to content

Fix configobj to relay tuple type errors #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions alot/utils/configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@ def width_tuple(value):
raise VdtTypeError(value)
elif value[0] not in ['fit', 'weight']:
raise VdtTypeError(value)
if value[0] == 'fit':
if not isinstance(value[1], int) or not isinstance(value[2], int):
VdtTypeError(value)
res = 'fit', int(value[1]), int(value[2])
else:
if not isinstance(value[1], int):
VdtTypeError(value)
res = 'weight', int(value[1])
try:
if value[0] == 'fit':
res = 'fit', int(value[1]), int(value[2])
else:
res = 'weight', int(value[1])
except IndexError:
raise VdtTypeError(value)
except ValueError:
raise VdtValueError(value)
return res


Expand Down
32 changes: 32 additions & 0 deletions tests/utils/test_configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest

from alot.utils import configobj as checks
from validate import VdtTypeError, VdtValueError

# Good descriptive test names often don't fit PEP8, which is meant to cover
# functions meant to be called by humans.
Expand All @@ -17,3 +18,34 @@ def test_strings_are_converted_to_single_item_lists(self):
def test_empty_strings_are_converted_to_empty_lists(self):
forced = checks.force_list('')
self.assertEqual(forced, [])


class TestWidthTuple(unittest.TestCase):

def test_validates_width_tuple(self):
with self.assertRaises(VdtTypeError):
checks.width_tuple('invalid-value')

def test_validates_width_tuple_for_fit_requires_two_args(self):
with self.assertRaises(VdtTypeError):
checks.width_tuple(['fit', 123])

def test_args_for_fit_must_be_numbers(self):
with self.assertRaises(VdtValueError):
checks.width_tuple(['fit', 123, 'not-a-number'])

def test_fit_with_two_numbers(self):
fit_result = checks.width_tuple(['fit', 123, 456])
self.assertEqual(('fit', 123, 456), fit_result)

def test_validates_width_tuple_for_weight_needs_an_argument(self):
with self.assertRaises(VdtTypeError):
checks.width_tuple(['weight'])

def test_arg_for_width_must_be_a_number(self):
with self.assertRaises(VdtValueError):
checks.width_tuple(['weight', 'not-a-number'])

def test_width_with_a_number(self):
weight_result = checks.width_tuple(['weight', 123])
self.assertEqual(('weight', 123), weight_result)