Skip to content

Commit a68f501

Browse files
Let users pass config to CLI (#132)
We have an option in the cli to allow users to pass config to it. Config controls things like plugins and runners. Until this point we didn't have to use it, and it was actually broken. The issue was that `yaml.safe_load` expects a strem (e.g. file contents), not the file name. So this fixes that issue, and also adds test to make sure the logging is correct. I decided to move the loading of the config to a helper function to make it easier to test. Turns out you can't just explicitly call `cli` function because it's wrapped in so much click stuff. Having it in a separate place makes it easier to test the actual config too.
1 parent 2a77e0e commit a68f501

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

codecov_cli/helpers/config.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import logging
2+
import pathlib
3+
4+
import yaml
5+
6+
logger = logging.getLogger("codecovcli")
7+
8+
9+
def load_cli_config(codecov_yml_path: pathlib.Path):
10+
if codecov_yml_path.exists() and codecov_yml_path.is_file():
11+
logger.debug(f"Loading config from {codecov_yml_path}")
12+
with open(codecov_yml_path, "r") as file_stream:
13+
return yaml.safe_load(file_stream.read())
14+
logger.warning(
15+
f"File {codecov_yml_path} not found, or is not a file. Ignoring config."
16+
)
17+
return None

codecov_cli/main.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import typing
44

55
import click
6-
import yaml
76

87
from codecov_cli.commands.base_picking import pr_base_picking
98
from codecov_cli.commands.commit import create_commit
@@ -14,6 +13,7 @@
1413
from codecov_cli.commands.staticanalysis import static_analysis
1514
from codecov_cli.commands.upload import do_upload
1615
from codecov_cli.helpers.ci_adapters import get_ci_adapter, get_ci_providers_list
16+
from codecov_cli.helpers.config import load_cli_config
1717
from codecov_cli.helpers.logging_utils import configure_logger
1818
from codecov_cli.helpers.versioning_systems import get_versioning_system
1919

@@ -46,13 +46,9 @@ def cli(
4646
ctx.obj["ci_adapter"] = get_ci_adapter(auto_load_params_from)
4747
ctx.obj["versioning_system"] = get_versioning_system()
4848
ctx.obj["codecov_yaml"] = (
49-
yaml.safe_load(codecov_yml_path.read())
50-
if codecov_yml_path is not None
51-
else None
49+
load_cli_config(codecov_yml_path) if codecov_yml_path else None
5250
)
53-
if ctx.obj["codecov_yaml"]:
54-
logger.debug(f"Using codecov_yaml from {codecov_yml_path}")
55-
else:
51+
if ctx.obj["codecov_yaml"] is None:
5652
logger.debug("No codecov_yaml found")
5753

5854

samples/example_cli_config.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
runners:
2+
python:
3+
collect_tests_options:
4+
- --ignore
5+
- batata

tests/helpers/test_config.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pathlib
2+
3+
from codecov_cli.helpers.config import load_cli_config
4+
5+
6+
def test_load_config(mocker):
7+
path = pathlib.Path("samples/example_cli_config.yml")
8+
result = load_cli_config(path)
9+
assert result == {
10+
"runners": {"python": {"collect_tests_options": ["--ignore", "batata"]}}
11+
}
12+
13+
14+
def test_load_config_doesnt_exist(mocker):
15+
path = pathlib.Path("doesnt/exist")
16+
result = load_cli_config(path)
17+
assert result == None
18+
19+
20+
def test_load_config_not_file(mocker):
21+
path = pathlib.Path("samples/")
22+
result = load_cli_config(path)
23+
assert result == None

0 commit comments

Comments
 (0)