diff --git a/jupytext/config.py b/jupytext/config.py index 9d68466fa..dd489900e 100644 --- a/jupytext/config.py +++ b/jupytext/config.py @@ -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""" diff --git a/jupytext/contentsmanager.py b/jupytext/contentsmanager.py index aaeff7ba6..311e9725c 100644 --- a/jupytext/contentsmanager.py +++ b/jupytext/contentsmanager.py @@ -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 diff --git a/tests/test_cm_config.py b/tests/test_cm_config.py index 94bce6567..2e0d6c744 100644 --- a/tests/test_cm_config.py +++ b/tests/test_cm_config.py @@ -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