Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions codecarbon/cli/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def get_config(path: Optional[Path] = None):
config = configparser.ConfigParser()
config.read(str(p))
if "codecarbon" in config.sections():
d = dict(config["codecarbon"])
return d
return dict(config["codecarbon"])
return {}

else:
raise FileNotFoundError(
Expand All @@ -34,8 +34,9 @@ def get_api_endpoint(path: Optional[Path] = None):
if "api_endpoint" in d:
return d["api_endpoint"]
else:
with p.open("a") as f:
f.write("api_endpoint=https://api.codecarbon.io\n")
config["codecarbon"]["api_endpoint"] = "https://api.codecarbon.io"
with p.open("w") as f:
config.write(f)
return "https://api.codecarbon.io"


Expand Down
19 changes: 19 additions & 0 deletions tests/cli/test_cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,22 @@ def test_create_new_config_file_expands_home(monkeypatch, tmp_path):

assert created_path == target
assert target.exists()


def test_get_config_returns_empty_dict_without_codecarbon_section(tmp_path):
config_path = tmp_path / ".codecarbon.config"
config_path.write_text("[other]\nkey=value\n")

assert cli_utils.get_config(config_path) == {}


def test_get_api_endpoint_writes_default_under_codecarbon_section(tmp_path):
config_path = tmp_path / ".codecarbon.config"
config_path.write_text("[codecarbon]\nproject_id=abc\n\n[other]\nkey=value\n")

cli_utils.get_api_endpoint(config_path)

parser = configparser.ConfigParser()
parser.read(config_path)
assert parser["codecarbon"]["api_endpoint"] == "https://api.codecarbon.io"
assert "api_endpoint" not in parser["other"]
Loading