-
-
Notifications
You must be signed in to change notification settings - Fork 264
Coverage improvement plan #1147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
benoit-cty
merged 5 commits into
mlco2:master
from
DiogoRibeiro7:coverage-improvement-plan
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
53cc99e
test: increase CLI and API coverage
DiogoRibeiro7 534c2c9
test: increase resource tracker and powermetrics coverage
DiogoRibeiro7 2ab15e1
test: increase cpu coverage
DiogoRibeiro7 dab2651
test: stabilize failing coverage tests
DiogoRibeiro7 2cb03a4
test: fix auth callback coverage test and temp credentials path
DiogoRibeiro7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import configparser | ||
|
|
||
| import pytest | ||
|
|
||
| from codecarbon.cli import cli_utils | ||
|
|
||
|
|
||
| def test_get_config_reads_codecarbon_section(tmp_path): | ||
| config_path = tmp_path / ".codecarbon.config" | ||
| config_path.write_text("[codecarbon]\napi_endpoint=https://example.test\n") | ||
|
|
||
| config = cli_utils.get_config(config_path) | ||
|
|
||
| assert config["api_endpoint"] == "https://example.test" | ||
|
|
||
|
|
||
| def test_get_config_raises_when_missing(tmp_path): | ||
| with pytest.raises(FileNotFoundError): | ||
| cli_utils.get_config(tmp_path / ".codecarbon.config") | ||
|
|
||
|
|
||
| def test_get_api_endpoint_appends_default_when_missing_key(tmp_path): | ||
| config_path = tmp_path / ".codecarbon.config" | ||
| config_path.write_text("[codecarbon]\n") | ||
|
|
||
| endpoint = cli_utils.get_api_endpoint(config_path) | ||
|
|
||
| assert endpoint == "https://api.codecarbon.io" | ||
| parser = configparser.ConfigParser() | ||
| parser.read(config_path) | ||
| assert parser["codecarbon"]["api_endpoint"] == "https://api.codecarbon.io" | ||
|
|
||
|
|
||
| def test_get_api_endpoint_returns_default_when_file_missing(tmp_path): | ||
| endpoint = cli_utils.get_api_endpoint(tmp_path / ".codecarbon.config") | ||
|
|
||
| assert endpoint == "https://api.codecarbon.io" | ||
|
|
||
|
|
||
| def test_get_existing_exp_id_returns_none_on_key_error(monkeypatch): | ||
| def raise_key_error(): | ||
| raise KeyError("missing") | ||
|
|
||
| monkeypatch.setattr(cli_utils, "get_hierarchical_config", raise_key_error) | ||
|
|
||
| assert cli_utils.get_existing_exp_id() is None | ||
|
|
||
|
|
||
| def test_get_existing_exp_id_reads_experiment_id(monkeypatch): | ||
| monkeypatch.setattr( | ||
| cli_utils, "get_hierarchical_config", lambda: {"experiment_id": "exp-123"} | ||
| ) | ||
|
|
||
| assert cli_utils.get_existing_exp_id() == "exp-123" | ||
|
|
||
|
|
||
| def test_write_local_exp_id_creates_section(tmp_path): | ||
| config_path = tmp_path / ".codecarbon.config" | ||
|
|
||
| cli_utils.write_local_exp_id("exp-456", config_path) | ||
|
|
||
| parser = configparser.ConfigParser() | ||
| parser.read(config_path) | ||
| assert parser["codecarbon"]["experiment_id"] == "exp-456" | ||
|
|
||
|
|
||
| def test_overwrite_local_config_updates_existing_file(tmp_path): | ||
| config_path = tmp_path / ".codecarbon.config" | ||
| config_path.write_text("[codecarbon]\nexperiment_id=old\n") | ||
|
|
||
| cli_utils.overwrite_local_config("experiment_id", "new", config_path) | ||
|
|
||
| parser = configparser.ConfigParser() | ||
| parser.read(config_path) | ||
| assert parser["codecarbon"]["experiment_id"] == "new" | ||
|
|
||
|
|
||
| def test_create_new_config_file_creates_parent_and_file(monkeypatch, tmp_path): | ||
| target = tmp_path / "nested" / ".codecarbon.config" | ||
| prompts = iter([str(target)]) | ||
|
|
||
| monkeypatch.setattr( | ||
| cli_utils.typer, "prompt", lambda *args, **kwargs: next(prompts) | ||
| ) | ||
| monkeypatch.setattr(cli_utils.Confirm, "ask", lambda *args, **kwargs: True) | ||
|
|
||
| created_path = cli_utils.create_new_config_file() | ||
|
|
||
| assert created_path == target | ||
| assert target.exists() | ||
| assert target.read_text() == "[codecarbon]\n" | ||
|
|
||
|
|
||
| def test_create_new_config_file_expands_home(monkeypatch, tmp_path): | ||
| home = tmp_path / "home" | ||
| home.mkdir() | ||
| target = home / ".codecarbon.config" | ||
|
|
||
| monkeypatch.setattr(cli_utils.Path, "home", lambda: home) | ||
| monkeypatch.setattr( | ||
| cli_utils.typer, "prompt", lambda *args, **kwargs: "~/.codecarbon.config" | ||
| ) | ||
|
|
||
| created_path = cli_utils.create_new_config_file() | ||
|
|
||
| assert created_path == target | ||
| assert target.exists() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.