Skip to content

Commit

Permalink
Simplify test + compare config content
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 2, 2022
1 parent 143a1be commit 5d9a609
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 43 deletions.
7 changes: 7 additions & 0 deletions jupytext/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ def default_formats(self, path):

return None

def __eq__(self, other):
for key in self.class_trait_names():
if getattr(self, key) != getattr(other, key):
return False

return True


def preferred_format(incomplete_format, preferred_formats):
"""Return the preferred format for the given extension"""
Expand Down
4 changes: 2 additions & 2 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,14 @@ def get_config(self, path, use_cache=False):
self.cached_config.config = self.load_config_file(
config_file,
prev_config_file=self.cached_config.config_file,
prev_config=self.cached_config,
prev_config=self.cached_config.config,
)
else:
config_file = find_global_jupytext_configuration_file()
self.cached_config.config = self.load_config_file(
config_file,
prev_config_file=self.cached_config.config_file,
prev_config=self.cached_config,
prev_config=self.cached_config.config,
is_os_path=True,
)
self.cached_config.config_file = config_file
Expand Down
87 changes: 46 additions & 41 deletions tests/test_cm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,54 +223,59 @@ def test_test_no_text_representation_metadata_in_ipynb_900(
assert "text_representation" not in tmpdir.join("test.ipynb").read()


@pytest.mark.parametrize(
"cm_config_log_level", ["warning", "info", "info_if_changed", "debug", "none"]
)
def test_cm_config_log_level(cwd_tmp_path, tmp_path, cm_config_log_level, caplog):
def test_cm_config_no_log(cwd_tmp_path, tmp_path, caplog):
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmp_path)

config = f'cm_config_log_level="{cm_config_log_level}"'
config = 'cm_config_log_level="none"'
(tmp_path / "jupytext.toml").write_text(config)
(tmp_path / "nb1.py").write_text("# %%")
(tmp_path / "subfolder").mkdir()
(tmp_path / "subfolder" / "jupytext.toml").write_text(config)
(tmp_path / "subfolder" / "nb2.py").write_text("# %%")

if cm_config_log_level == "none":
with caplog.at_level(logging.DEBUG):
cm.get("nb1.py", type="notebook", content=False)
cm.get("nb1.py", type="notebook", content=True)
cm.get("subfolder/nb2.py", type="notebook", content=False)
cm.get("subfolder/nb2.py", type="notebook", content=True)
assert "Jupytext configuration file" not in caplog.text
return

log_level_changed = (
"info" if cm_config_log_level == "info_if_changed" else cm_config_log_level
)
log_level_unchanged = (
"none" if cm_config_log_level == "info_if_changed" else cm_config_log_level
)
caplog.set_level(logging.DEBUG)

cm.get("nb1.py", type="notebook", content=False)
cm.get("nb1.py", type="notebook", content=True)
cm.get("subfolder/nb2.py", type="notebook", content=False)
cm.get("subfolder/nb2.py", type="notebook", content=True)

assert "Jupytext configuration file" not in caplog.text


def test_cm_config_log_only_if_changed(cwd_tmp_path, tmp_path, caplog):
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(tmp_path)

config = ""
(tmp_path / "jupytext.toml").write_text(config)
(tmp_path / "nb1.py").write_text("# %%")
(tmp_path / "subfolder").mkdir()
(tmp_path / "subfolder" / "jupytext.toml").write_text(config)
(tmp_path / "subfolder" / "nb2.py").write_text("# %%")

caplog.set_level(logging.INFO)

cm.get("nb1.py", type="notebook", content=False)
assert "Jupytext configuration file" in caplog.text
caplog.clear()

# Same notebook, same config => no log
cm.get("nb1.py", type="notebook", content=True)
assert "Jupytext configuration file" not in caplog.text

# Same notebook, config changed => log
(tmp_path / "jupytext.toml").write_text('formats="ipynb,py:percent"')
cm.get("nb1.py", type="notebook", content=True)
assert "Jupytext configuration file" in caplog.text
caplog.clear()

# Different folder, different config
cm.get("subfolder/nb2.py", type="notebook", content=False)
assert "Jupytext configuration file" in caplog.text
caplog.clear()

log = mock.MagicMock(return_value=None)
with mock.patch.object(cm.log, log_level_changed, log):
cm.get("nb1.py", type="notebook", content=False)
log.assert_called()

log = mock.MagicMock(return_value=None)
if log_level_unchanged != "none":
with mock.patch.object(cm.log, log_level_unchanged, log):
cm.get("nb1.py", type="notebook", content=True)
log.assert_called()

log = mock.MagicMock(return_value=None)
with mock.patch.object(cm.log, log_level_changed, log):
cm.get("subfolder/nb2.py", type="notebook", content=False)
log.assert_called()

log = mock.MagicMock(return_value=None)
if log_level_unchanged != "none":
with mock.patch.object(cm.log, log_level_unchanged, log):
cm.get("subfolder/nb2.py", type="notebook", content=True)
log.assert_called()
# Same config as previously => no log
cm.get("subfolder/nb2.py", type="notebook", content=False)
assert "Jupytext configuration file" not in caplog.text

0 comments on commit 5d9a609

Please sign in to comment.