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

[Feature] Add support for toml #596

Closed
wants to merge 3 commits into from
Closed
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
11 changes: 10 additions & 1 deletion nose2/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# py2/py3 compatible load of SafeConfigParser/ConfigParser
from configparser import ConfigParser

import toml

from nose2 import config, events, util

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -119,7 +121,14 @@ def loadConfigFiles(self, *filenames):
Loads all names files that exist into ``self.config``.

"""
self.config.read(filenames)
for filename in filenames:
if filename.endswith(".cfg"):
self.config.read(filename)
elif filename.endswith(".toml"):
with open(filename) as f:
toml_config = toml.load(f)
if "tool" in toml_config and "nose2" in toml_config["tool"]:
self.config.read_dict(toml_config["tool"]["nose2"])

def loadPlugins(self, modules=None, exclude=None):
"""Load plugins.
Expand Down
5 changes: 5 additions & 0 deletions nose2/tests/functional/support/toml/a.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.nose2.a]
a = 1

[tool.nose2.unittest]
plugins = "plugin_a"
5 changes: 5 additions & 0 deletions nose2/tests/functional/support/toml/b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[tool.nose2.b]
b = """
4
5
"""
51 changes: 51 additions & 0 deletions nose2/tests/functional/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,54 @@ def test_session_config_cacheing(self):
# rather than parsing config file again
secondaccess = cache_sess.get("a")
assert secondaccess.as_int("a") == 0


class SessionTomlFunctionalTests(FunctionalTestCase):
def setUp(self):
self.s = session.Session()
self.s.loadConfigFiles(
support_file("toml", "a.toml"), support_file("toml", "b.toml")
)
sys.path.insert(0, support_file("lib"))

def test_session_can_load_config_files(self):
assert self.s.config.has_section("a")
assert self.s.config.has_section("b")

def test_session_holds_plugin_config(self):
plug_config = self.s.get("a")
assert plug_config

def test_session_can_load_toml_plugins_from_modules(self):
self.s.loadPlugins()
assert self.s.plugins
plug = self.s.plugins[0]
self.assertEqual(plug.a, 1)

def test_session_config_cacheing(self):
"""Test caching of config sections works"""

# Create new session (generic one likely already cached
# depending on test order)
cache_sess = session.Session()
cache_sess.loadConfigFiles(support_file("toml", "a.toml"))

# First access to given section, should read from config file
firstaccess = cache_sess.get("a")
assert firstaccess.as_int("a") == 1

# Hack cached Config object internals to make the stored value
# something different
cache_sess.configCache["a"]._mvd["a"] = "0"
newitems = []
for item in cache_sess.configCache["a"]._items:
if item != ("a", "1"):
newitems.append(item)
else:
newitems.append(("a", "0"))
cache_sess.configCache["a"]._items = newitems

# Second access to given section, confirm returns cached value
# rather than parsing config file again
secondaccess = cache_sess.get("a")
assert secondaccess.as_int("a") == 0
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires = [
name = "nose2"
description = "unittest with plugins"
readme = "README.rst"
keywords= [
keywords = [
"testing",
"tests",
"unittest",
Expand Down Expand Up @@ -40,6 +40,9 @@ classifiers=[
dynamic = [
"version",
]
dependencies = [
"toml>=0.10.2", # Add other dependencies here
]
[project.optional-dependencies]
coverage_plugin = [
"coverage",
Expand Down