Skip to content

Commit

Permalink
Fix a bug with ini configs lacking '.cfg' suffix
Browse files Browse the repository at this point in the history
The check for which config loading path to use incorrectly treated
'.cfg' as the only valid suffix for ini configs.
This is not the case for a config loaded with `-c`.

To fix, introduce a regression test, then make some strategic use of
pathlib to simplify path/file inspection and fix the issue.

fixes #614
  • Loading branch information
sirosen committed Jun 1, 2024
1 parent 439d82c commit 195763f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ testsuites.
Unreleased
----------

Fixed
~~~~~

* Fix a bug with config loading which caused custom ini configs not to load if
they were not named with a ``.cfg`` extension. Thanks :user:`grhwalls` for
the bug report!

0.15.0 (2024-05-30)
-------------------

Expand Down
18 changes: 11 additions & 7 deletions nose2/session.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import argparse
import logging
import os

# py2/py3 compatible load of SafeConfigParser/ConfigParser
import pathlib
from configparser import ConfigParser

from nose2 import config, events, util
Expand Down Expand Up @@ -120,11 +119,12 @@ def loadConfigFiles(self, *filenames):
"""
for filename in filenames:
if filename.endswith(".cfg"):
self.config.read(filename)
elif filename.endswith("pyproject.toml"):
if not os.path.exists(filename):
continue
path = pathlib.Path(filename)
if not path.exists():
continue

# handle pyproject.toml case
if path.name == "pyproject.toml":
toml_config = load_toml(filename)
if not isinstance(toml_config.get("tool"), dict):
continue
Expand All @@ -133,6 +133,10 @@ def loadConfigFiles(self, *filenames):
continue
self.config.read_dict(tool_table["nose2"])

# else, use the config parser to read config data
else:
self.config.read(path)

def loadPlugins(self, modules=None, exclude=None):
"""Load plugins.
Expand Down
21 changes: 21 additions & 0 deletions nose2/tests/functional/test_session.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import pathlib
import sys
import tempfile
import unittest

from nose2 import session
from nose2._toml import TOML_ENABLED
from nose2.tests._common import FunctionalTestCase, support_file


def test_loading_config_from_ini_file_without_cfg_suffix():
"""
Regression test for https://github.com/nose-devs/nose2/issues/614
It is possible to have nose2 config embedded in any ini config file, and it might
not have a recognizable name.
It is not guaranteed that filenames used for config will end in `.cfg`.
"""
sess = session.Session()
with tempfile.TemporaryDirectory() as tmpdirname:
tmpdir = pathlib.Path(tmpdirname)

foo_ini = tmpdir / "foo.ini"
foo_ini.write_text("""[alpha]\na = 1""")

sess.loadConfigFiles(str(foo_ini))
assert sess.config.has_section("alpha")


class SessionFunctionalTests(FunctionalTestCase):
def setUp(self):
self.s = session.Session()
Expand Down

0 comments on commit 195763f

Please sign in to comment.