Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit a27c573

Browse files
authored
fix: search acceptable paths for codecov yaml if not passed in (#368)
* fix: search acceptable paths for codecov yaml if not passed in * add test case ensuring load_cli_config() calls _find_codecov_yamls
1 parent 019a55a commit a27c573

File tree

15 files changed

+105
-12
lines changed

15 files changed

+105
-12
lines changed

codecov_cli/helpers/config.py

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,65 @@
11
import logging
22
import pathlib
3+
import typing
34

45
import yaml
56

7+
from codecov_cli.helpers.versioning_systems import get_versioning_system
8+
69
logger = logging.getLogger("codecovcli")
710

811
CODECOV_API_URL = "https://api.codecov.io"
912
LEGACY_CODECOV_API_URL = "https://codecov.io"
1013

14+
# Relative to the project root
15+
CODECOV_YAML_RECOGNIZED_DIRECTORIES = [
16+
"",
17+
".github/",
18+
"dev/",
19+
]
20+
21+
CODECOV_YAML_RECOGNIZED_FILENAMES = [
22+
"codecov.yml",
23+
"codecov.yaml",
24+
".codecov.yml",
25+
".codecov.yaml",
26+
]
27+
28+
29+
def _find_codecov_yamls():
30+
vcs = get_versioning_system()
31+
vcs_root = vcs.get_network_root() if vcs else None
32+
project_root = vcs_root if vcs_root else pathlib.Path.cwd()
33+
34+
yamls = []
35+
for directory in CODECOV_YAML_RECOGNIZED_DIRECTORIES:
36+
dir_candidate = project_root / directory
37+
if not dir_candidate.exists() or not dir_candidate.is_dir():
38+
continue
39+
40+
for filename in CODECOV_YAML_RECOGNIZED_FILENAMES:
41+
file_candidate = dir_candidate / filename
42+
if file_candidate.exists() and file_candidate.is_file():
43+
yamls.append(file_candidate)
44+
45+
return yamls
46+
47+
48+
def load_cli_config(codecov_yml_path: typing.Optional[pathlib.Path]):
49+
if not codecov_yml_path:
50+
yamls = _find_codecov_yamls()
51+
codecov_yml_path = yamls[0] if yamls else None
52+
53+
if not codecov_yml_path:
54+
logger.warning("No config file could be found. Ignoring config.")
55+
return None
56+
57+
if not codecov_yml_path.exists() or not codecov_yml_path.is_file():
58+
logger.warning(
59+
f"Config file {codecov_yml_path} not found, or is not a file. Ignoring config."
60+
)
61+
return None
1162

12-
def load_cli_config(codecov_yml_path: pathlib.Path):
13-
if codecov_yml_path.exists() and codecov_yml_path.is_file():
14-
logger.debug(f"Loading config from {codecov_yml_path}")
15-
with open(codecov_yml_path, "r") as file_stream:
16-
return yaml.safe_load(file_stream.read())
17-
logger.warning(
18-
f"Config file {codecov_yml_path} not found, or is not a file. Ignoring config."
19-
)
20-
return None
63+
logger.debug(f"Loading config from {codecov_yml_path}")
64+
with open(codecov_yml_path, "r") as file_stream:
65+
return yaml.safe_load(file_stream.read())

codecov_cli/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
)
3535
@click.option(
3636
"--codecov-yml-path",
37-
type=click.Path(path_type=pathlib.Path),
38-
default=pathlib.Path("codecov.yml"),
37+
type=click.Path(path_type=typing.Optional[pathlib.Path]),
38+
default=None,
3939
)
4040
@click.option(
4141
"--enterprise-url", "--url", "-u", help="Change the upload host (Enterprise use)"

samples/fake_project/.codecov.yaml

Whitespace-only changes.

samples/fake_project/.codecov.yml

Whitespace-only changes.

samples/fake_project/.github/.codecov.yaml

Whitespace-only changes.

samples/fake_project/.github/.codecov.yml

Whitespace-only changes.

samples/fake_project/.github/codecov.yaml

Whitespace-only changes.

samples/fake_project/.github/codecov.yml

Whitespace-only changes.

samples/fake_project/codecov.yaml

Whitespace-only changes.

samples/fake_project/codecov.yml

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

0 commit comments

Comments
 (0)