Skip to content

Commit

Permalink
Merge pull request #856 from sphinx-contrib/flexible-boolean-cfg-valu…
Browse files Browse the repository at this point in the history
…es-with-ints

config: flexible boolean processing from int value
  • Loading branch information
jdknight authored Nov 18, 2023
2 parents 498a908 + e9871b8 commit 01a3b74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sphinxcontrib/confluencebuilder/config/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def bool(self):
value = self._value()

if value is not None:
if isinstance(value, str):
if isinstance(value, str) or isinstance(value, int):
try:
str2bool(value)
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion sphinxcontrib/confluencebuilder/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def str2bool(value):
``ValueError`` is raised if the string value is not an accepted string
"""

value = value.lower()
value = str(value).lower()
if value in ['y', 'yes', 't', 'true', 'on', '1']:
return True
elif value in ['n', 'no', 'f', 'false', 'off', '0']:
Expand Down
37 changes: 37 additions & 0 deletions tests/unit-tests/test_util_str2bool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SPDX-License-Identifier: BSD-2-Clause
# Copyright Sphinx Confluence Builder Contributors (AUTHORS)

from sphinxcontrib.confluencebuilder.util import str2bool
import unittest


class TestConfluenceUtilStr2Bool(unittest.TestCase):
def test_util_str2bool_invalid(self):
with self.assertRaises(ValueError):
str2bool(None)

with self.assertRaises(ValueError):
str2bool(2)

with self.assertRaises(ValueError):
str2bool({})

def test_util_str2bool_true(self):
self.assertTrue(str2bool('on'))
self.assertTrue(str2bool('ON'))
self.assertTrue(str2bool('true'))
self.assertTrue(str2bool('TRUE'))
self.assertTrue(str2bool('yes'))
self.assertTrue(str2bool('YES'))
self.assertTrue(str2bool('1'))
self.assertTrue(str2bool(1))

def test_util_str2bool_false(self):
self.assertFalse(str2bool('false'))
self.assertFalse(str2bool('FALSE'))
self.assertFalse(str2bool('no'))
self.assertFalse(str2bool('NO'))
self.assertFalse(str2bool('off'))
self.assertFalse(str2bool('OFF'))
self.assertFalse(str2bool('0'))
self.assertFalse(str2bool(0))

0 comments on commit 01a3b74

Please sign in to comment.