Skip to content

Commit

Permalink
Add a test, move parsing of avoid_duplicate_runs
Browse files Browse the repository at this point in the history
  • Loading branch information
PGijsbers committed May 14, 2024
1 parent dbffcac commit 4a9b568
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion openml/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _setup(config: _Config | None = None) -> None:
if config is None:
config = _parse_config(config_file)

avoid_duplicate_runs = config.get("avoid_duplicate_runs", "").lower() == "true"
avoid_duplicate_runs = config["avoid_duplicate_runs"]
apikey = config["apikey"]
server = config["server"]
short_cache_dir = Path(config["cachedir"])
Expand Down Expand Up @@ -328,6 +328,10 @@ def _parse_config(config_file: str | Path) -> _Config:
logger.info("Error opening file %s: %s", config_file, e.args[0])
config_file_.seek(0)
config.read_file(config_file_)
if isinstance(config["FAKE_SECTION"]["avoid_duplicate_runs"], str):
config["FAKE_SECTION"]["avoid_duplicate_runs"] = config["FAKE_SECTION"].getboolean(
"avoid_duplicate_runs"
) # type: ignore
return dict(config.items("FAKE_SECTION")) # type: ignore


Expand Down
17 changes: 17 additions & 0 deletions tests/test_openml/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,20 @@ def test_example_configuration_start_twice(self):

assert openml.config.apikey == "610344db6388d9ba34f6db45a3cf71de"
assert openml.config.server == self.production_server


def test_configuration_file_not_overwritten_on_load():
""" Regression test for #1337 """
config_file_content = "apikey = abcd"
with tempfile.TemporaryDirectory() as tmpdir:
config_file_path = Path(tmpdir) / "config"
with config_file_path.open("w") as config_file:
config_file.write(config_file_content)

read_config = openml.config._parse_config(config_file_path)

with config_file_path.open("r") as config_file:
new_file_content = config_file.read()

assert config_file_content == new_file_content
assert "abcd" == read_config["apikey"]

0 comments on commit 4a9b568

Please sign in to comment.