Skip to content

Commit

Permalink
Test that the contents manager can load the global configuration file
Browse files Browse the repository at this point in the history
when no local configuration file is found
  • Loading branch information
mwouts committed Sep 27, 2020
1 parent 044e02d commit 27b71bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
13 changes: 7 additions & 6 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,20 +437,21 @@ def get_config_file(self, directory):
return path

if not directory:
return find_global_jupytext_configuration_file()
return None

parent_dir = self.get_parent_dir(directory)
return self.get_config_file(parent_dir)

def load_config_file(self, config_file):
def load_config_file(self, config_file, is_os_path=False):
"""Load the configuration file"""
if config_file is None:
return None
self.log.info("Loading Jupytext configuration file at %s", config_file)
if config_file.endswith(".py"):
config_dict = load_jupytext_configuration_file(
self._get_os_path(config_file)
)
if config_file.endswith(".py") and not is_os_path:
config_file = self._get_os_path(config_file)
is_os_path = True
if is_os_path:
config_dict = load_jupytext_configuration_file(config_file)
else:
model = self.super.get(config_file, content=True, type="file")
config_dict = load_jupytext_configuration_file(
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,26 @@ def test_incorrect_config_message(tmpdir, cfg_file, cfg_text):

with pytest.raises(HTTPError, match=expected_message):
cm.save(dict(type="notebook", content=SAMPLE_NOTEBOOK), "notebook.ipynb")


def test_global_config_file(tmpdir):
cm_dir = tmpdir.join("cm_dir").mkdir()
cm = jupytext.TextFileContentsManager()
cm.root_dir = str(cm_dir)

tmpdir.join("jupytext.toml").write('default_jupytext_formats = "ipynb,Rmd"')

def fake_global_config_directory():
return [str(tmpdir)]

with mock.patch(
"jupytext.config.global_jupytext_configuration_directories",
fake_global_config_directory,
):
nb = new_notebook(cells=[new_code_cell("1+1")])
model = dict(content=nb, type="notebook")
cm.save(model, "notebook.ipynb")
assert set(model["path"] for model in cm.get("/", content=True)["content"]) == {
"notebook.ipynb",
"notebook.Rmd",
}

0 comments on commit 27b71bf

Please sign in to comment.