Skip to content
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

[Python3.11] Improve the regex expression #3804

Merged
merged 2 commits into from
Aug 1, 2024
Merged
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
68 changes: 34 additions & 34 deletions desktop/core/src/desktop/lib/conf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from future import standard_library
standard_library.install_aliases()
from builtins import object
import configobj
import logging
import pytest
import re
import sys
import logging
from builtins import object

import pytest
import configobj

from desktop.lib.conf import *

Expand All @@ -31,17 +30,19 @@
else:
from cStringIO import StringIO as string_io


def my_dynamic_default():
"""
Calculates a sum
"""
return 3 + 4


class TestConfig(object):
"""Unit tests for the configuration module."""

# Some test configurations to load
CONF_ONE="""
CONF_ONE = """
foo = 123
list=a,b,c
"""
Expand All @@ -62,33 +63,33 @@ def setup_class(cls):
logging.basicConfig(level=logging.DEBUG)
cls.conf = ConfigSection(
members=dict(
FOO = Config("foo",
FOO=Config("foo",
help="A vanilla configuration param",
type=int),
BAR = Config("bar", default=456,
BAR=Config("bar", default=456,
help="Config with default",
type=int),
REQ = Config("req", required=True,
REQ=Config("req", required=True,
help="A required config",
type=int),
OPT_NOT_THERE = Config("blahblah"),
REQ_NOT_THERE = Config("blah", required=True, help="Another required"),
PRIVATE_CONFIG= Config("dontseeme",private=True),
DYNAMIC_DEF = Config("dynamic_default", dynamic_default=my_dynamic_default,
OPT_NOT_THERE=Config("blahblah"),
REQ_NOT_THERE=Config("blah", required=True, help="Another required"),
PRIVATE_CONFIG=Config("dontseeme", private=True),
DYNAMIC_DEF=Config("dynamic_default", dynamic_default=my_dynamic_default,
type=int),
SOME_SECTION = ConfigSection(
SOME_SECTION=ConfigSection(
"some_section",
private=True,
members=dict(BAZ = Config("baz", default="baz_default"))),
LIST = Config("list", type=list),
CLUSTERS = UnspecifiedConfigSection(
members=dict(BAZ=Config("baz", default="baz_default"))),
LIST=Config("list", type=list),
CLUSTERS=UnspecifiedConfigSection(
"clusters",
help="Details about your Hadoop cluster(s)",
each=ConfigSection(
help="Details about a cluster - one section for each.",
members=dict(HOST = Config("host", help="Hostname for the NN",
members=dict(HOST=Config("host", help="Hostname for the NN",
required=True),
PORT = Config("port", help="Thrift port for the NN",
PORT=Config("port", help="Thrift port for the NN",
type=int, default=10090))))))
cls.conf = cls.conf.bind(
load_confs([configobj.ConfigObj(infile=string_io(cls.CONF_ONE)),
Expand All @@ -113,12 +114,12 @@ def test_load(self):
assert 456 == self.conf.BAR.get()
assert 345 == self.conf.REQ.get()

assert None == self.conf.OPT_NOT_THERE.get()
assert self.conf.OPT_NOT_THERE.get() is None
with pytest.raises(KeyError):
self.conf.REQ_NOT_THERE.get()

def test_list_values(self):
assert ["a","b","c"] == self.conf.LIST.get()
assert ["a", "b", "c"] == self.conf.LIST.get()

def test_sections(self):
assert 2 == len(self.conf.CLUSTERS)
Expand Down Expand Up @@ -154,7 +155,7 @@ def test_set_for_testing(self):
# Check default values
close_foo3 = self.conf.FOO.set_for_testing(present=False)
try:
assert None == self.conf.FOO.get()
assert self.conf.FOO.get() is None
finally:
close_foo3()
finally:
Expand All @@ -179,25 +180,24 @@ def test_set_for_testing(self):
close()
assert "baz_default" == self.conf.SOME_SECTION.BAZ.get()


def test_coerce_bool(self):
assert False == coerce_bool(False)
assert False == coerce_bool("FaLsE")
assert False == coerce_bool("no")
assert False == coerce_bool("0")
assert True == coerce_bool("TrUe")
assert True == coerce_bool("YES")
assert True == coerce_bool("1")
assert True == coerce_bool(True)
assert not coerce_bool(False)
assert not coerce_bool("FaLsE")
assert not coerce_bool("no")
assert not coerce_bool("0")
assert coerce_bool("TrUe")
assert coerce_bool("YES")
assert coerce_bool("1")
assert coerce_bool(True)
with pytest.raises(Exception):
coerce_bool(tuple("foo"))

def test_print_help(self):
out = string_io()
self.conf.print_help(out=out, skip_header=True)
out = out.getvalue().strip()
assert not "dontseeme" in out
assert re.sub("^ (?m)", "", """
assert "dontseeme" not in out
assert re.sub("(?m)^ ", "", """
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an official guideline doc from python.org? Please share the source.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Key: bar (optional)
Default: 456
Config with default
Expand Down
Loading