Skip to content

Commit

Permalink
Merge branch 'alexandrebarbaruiva-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
gotcha committed Mar 2, 2021
2 parents 1e48e13 + 23b0299 commit 6e40a67
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 19 deletions.
3 changes: 3 additions & 0 deletions HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Changelog
0.13.5 (unreleased)
-------------------

- Add support for pyproject.toml as configuration file
[alexandrebarbaruiva]

- For users using python 3.4, install 6.0.0 <= IPython < 7.0.0,
for users using python 3.5, install 7.0.0 <= IPython < 7.10.0,
for users using python 3.6, install 7.10.0 <= IPython < 7.17.0,
Expand Down
26 changes: 21 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ interface whenever `cond` equals to `True`.
Using configuration file
++++++++++++++++++++++++

It's possible to set up context using a `.ipdb` file on your home folder or `setup.cfg`
on your project folder. You can also set your file location via env var `$IPDB_CONFIG`.
Your environment variable has priority over the home configuration file,
which in turn has priority over the setup config file. Currently, only context setting
is available.
It's possible to set up context using a `.ipdb` file on your home folder, `setup.cfg`
or `pyproject.toml` on your project folder. You can also set your file location via
env var `$IPDB_CONFIG`. Your environment variable has priority over the home
configuration file, which in turn has priority over the setup config file.
Currently, only context setting is available.

A valid setup.cfg is as follows

Expand All @@ -60,6 +60,14 @@ A valid .ipdb is as follows
context=5


A valid pyproject.toml is as follows

::

[tool.ipdb]
context=5


The post-mortem function, ``ipdb.pm()``, is equivalent to the magic function
``%debug``.

Expand Down Expand Up @@ -132,13 +140,21 @@ Development

Pull requests should take care of updating the changelog ``HISTORY.txt``.

Under the unreleased section, add your changes and your username.

Manual testing
++++++++++++++

To test your changes, make use of ``manual_test.py``. Create a virtual environment,
install IPython and run ``python manual_test.py`` and check if your changes are in effect.
If possible, create automated tests for better behaviour control.

Automated testing
+++++++++++++++++

To run automated tests locally, create a virtual environment, install `coverage`
and run `coverage run setup.py test`.

Third-party support
-------------------

Expand Down
10 changes: 6 additions & 4 deletions ipdb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ def set_trace(frame=None, context=None, cond=True):
def get_context_from_config():
try:
parser = get_config()
return parser.getint("ipdb", "context")
return parser.getint("tool.ipdb", "context", fallback=parser.getint("ipdb", "context"))
except (configparser.NoSectionError, configparser.NoOptionError):
return 3
except ValueError:
value = parser.get("ipdb", "context")
value = parser.get("tool.ipdb", "context", fallback=parser.get("ipdb", "context"))
raise ValueError(
"In %s, context value [%s] cannot be converted into an integer."
% (parser.filepath, value)
Expand Down Expand Up @@ -140,7 +140,7 @@ def get_config():
filepaths = []

# Low priority goes first in the list
for cfg_file in ("setup.cfg", ".ipdb"):
for cfg_file in ("setup.cfg", ".ipdb", "pyproject.toml"):
cwd_filepath = os.path.join(os.getcwd(), cfg_file)
if os.path.isfile(cwd_filepath):
filepaths.append(cwd_filepath)
Expand Down Expand Up @@ -168,10 +168,12 @@ def get_config():
parser.filepath = filepath
# Users are expected to put an [ipdb] section
# only if they use setup.cfg
if filepath.endswith('setup.cfg'):
if filepath.endswith('setup.cfg') or filepath.endswith('pyproject.toml'):
with open(filepath) as f:
parser.remove_section("ipdb")
read_func(f)
else:
parser.remove_section("tool.ipdb")
read_func(ConfigFile(filepath))
return parser

Expand Down
106 changes: 96 additions & 10 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,91 @@ def setUp(self):
"context = {}".format(str(self.setup_context)),
],
)
self.pyproject_filename = os.path.join(cwd_dir, "pyproject.toml")
self.pyproject_context = 30
self._write_file(
self.pyproject_filename,
[
"[tool.ipdb]",
"context = {}".format(str(self.pyproject_context)),
],
)

def test_noenv_nodef_nosetup_pyproject(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb does not exist,
setup.cfg does not exist, pyproject.toml exists
Result: load pyproject.toml
"""
os.unlink(self.env_filename)
os.unlink(self.default_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["tool.ipdb"], cfg.sections())
self.assertEqual(self.pyproject_context, cfg.getint("tool.ipdb", "context"))
self.assertRaises(configparser.NoOptionError, cfg.get, "tool.ipdb", "version")

def test_env_nodef_setup_pyproject(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb does not exist,
setup.cfg exists, pyproject.toml exists
Result: load $IPDB_CONFIG
"""
os.unlink(self.default_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
self.assertEqual(self.env_context, cfg.getint("ipdb", "context"))
self.assertRaises(configparser.NoOptionError, cfg.get, "ipdb", "version")

def test_env_def_setup_pyproject(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb exists,
setup.cfg exists, pyproject.toml exists
Result: load $IPDB_CONFIG
"""
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
self.assertEqual(self.env_context, cfg.getint("ipdb", "context"))
self.assertRaises(configparser.NoOptionError, cfg.get, "ipdb", "version")

def test_noenv_nodef_setup_pyproject(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb does not exist,
setup.cfg exists, pyproject.toml exists
Result: load pyproject.toml
"""
os.unlink(self.env_filename)
os.unlink(self.default_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["tool.ipdb"], cfg.sections())
self.assertEqual(self.pyproject_context, cfg.getint("tool.ipdb", "context"))
self.assertRaises(configparser.NoOptionError, cfg.get, "tool.ipdb", "version")

def test_noenv_def_setup_pyproject(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb exists,
setup.cfg exists, pyproject.toml exists
Result: load .ipdb
"""
os.unlink(self.env_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
self.assertEqual(self.default_context, cfg.getint("ipdb", "context"))
self.assertRaises(configparser.NoOptionError, cfg.get, "ipdb", "version")

def test_env_nodef_nosetup(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb does not exist,
setup.cfg does not exist
setup.cfg does not exist, pyproject.toml does not exist
Result: load $IPDB_CONFIG
"""
os.unlink(self.default_filename)
os.unlink(self.pyproject_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename,
HOME=self.tmpd):
Expand All @@ -112,10 +189,11 @@ def test_env_nodef_nosetup(self):
def test_noenv_def_nosetup(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb exists,
setup.cfg does not exist
setup.cfg does not exist, pyproject.toml does not exist
Result: load $HOME/.ipdb
"""
os.unlink(self.env_filename)
os.unlink(self.pyproject_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
Expand All @@ -126,11 +204,12 @@ def test_noenv_def_nosetup(self):
def test_noenv_nodef_nosetup(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb does not
exist, setup.cfg does not exist
exist, setup.cfg does not exist, pyproject.toml does not exist
Result: load nothing
"""
os.unlink(self.env_filename)
os.unlink(self.default_filename)
os.unlink(self.pyproject_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
Expand All @@ -139,10 +218,11 @@ def test_noenv_nodef_nosetup(self):
def test_env_cwd(self):
"""
Setup: $IPDB_CONFIG is set, .ipdb in local dir,
setup.cfg does not exist
setup.cfg does not exist, pyproject.toml does not exist
Result: load .ipdb
"""
os.chdir(self.tmpd) # setUp is already set to restore us to our pre-testing cwd
os.unlink(self.pyproject_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename,
HOME=self.tmpd):
Expand All @@ -154,9 +234,10 @@ def test_env_cwd(self):
def test_env_def_nosetup(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb exists,
setup.cfg does not exist
setup.cfg does not exist, pyproject.toml does not exist
Result: load $IPDB_CONFIG
"""
os.unlink(self.pyproject_filename)
os.remove(self.setup_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename,
HOME=self.tmpd):
Expand All @@ -168,10 +249,11 @@ def test_env_def_nosetup(self):
def test_noenv_def_setup(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb exists,
setup.cfg exists
setup.cfg exists, pyproject.toml does not exist
Result: load $HOME/.ipdb
"""
os.unlink(self.env_filename)
os.unlink(self.pyproject_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
Expand All @@ -181,11 +263,12 @@ def test_noenv_def_setup(self):
def test_noenv_nodef_setup(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb does not exist,
setup.cfg exists
setup.cfg exists, pyproject.toml does not exist
Result: load setup
"""
os.unlink(self.env_filename)
os.unlink(self.default_filename)
os.unlink(self.pyproject_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
Expand All @@ -195,9 +278,10 @@ def test_noenv_nodef_setup(self):
def test_env_def_setup(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb exists,
setup.cfg exists
setup.cfg exists, pyproject.toml does not exist
Result: load $IPDB_CONFIG
"""
os.unlink(self.pyproject_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename,
HOME=self.tmpd):
cfg = get_config()
Expand All @@ -208,10 +292,11 @@ def test_env_def_setup(self):
def test_env_nodef_setup(self):
"""
Setup: $IPDB_CONFIG is set, $HOME/.ipdb does not
exist, setup.cfg exists
exist, setup.cfg exists, pyproject.toml does not exist
Result: load $IPDB_CONFIG
"""
os.unlink(self.default_filename)
os.unlink(self.pyproject_filename)
with ModifiedEnvironment(IPDB_CONFIG=self.env_filename,
HOME=self.tmpd):
cfg = get_config()
Expand All @@ -222,10 +307,11 @@ def test_env_nodef_setup(self):
def test_noenv_def_setup(self):
"""
Setup: $IPDB_CONFIG unset, $HOME/.ipdb exists,
setup.cfg exists
setup.cfg exists, pyproject.toml does not exist
Result: load $HOME/.ipdb
"""
os.unlink(self.env_filename)
os.unlink(self.pyproject_filename)
with ModifiedEnvironment(IPDB_CONFIG=None, HOME=self.tmpd):
cfg = get_config()
self.assertEqual(["ipdb"], cfg.sections())
Expand Down

0 comments on commit 6e40a67

Please sign in to comment.