|
| 1 | +import logging |
1 | 2 | import os
|
2 | 3 | import typing
|
3 | 4 | from pathlib import Path
|
4 | 5 |
|
5 | 6 | from codecov_cli.helpers.folder_searcher import globs_to_regex, search_files
|
6 | 7 | from codecov_cli.types import UploadCollectionResultFile
|
7 | 8 |
|
| 9 | +logger = logging.getLogger("codecovcli") |
| 10 | + |
8 | 11 | coverage_files_patterns = [
|
9 | 12 | "*.clover",
|
10 | 13 | "*.codecov.*",
|
|
163 | 166 |
|
164 | 167 | class CoverageFileFinder(object):
|
165 | 168 | def __init__(
|
166 |
| - self, project_root: Path = None, folders_to_ignore: typing.List[str] = None |
| 169 | + self, |
| 170 | + project_root: Path = None, |
| 171 | + folders_to_ignore: typing.List[str] = None, |
| 172 | + explicitly_listed_files: typing.List[Path] = None, |
| 173 | + disable_search: bool = False, |
167 | 174 | ):
|
168 | 175 | self.project_root = project_root or Path(os.getcwd())
|
169 | 176 | self.folders_to_ignore = folders_to_ignore or []
|
| 177 | + self.explicitly_listed_files = explicitly_listed_files or None |
| 178 | + self.disable_search = disable_search |
170 | 179 |
|
171 | 180 | def find_coverage_files(self) -> typing.List[UploadCollectionResultFile]:
|
172 |
| - regex_patterns_to_include = globs_to_regex(coverage_files_patterns) |
173 | 181 | regex_patterns_to_exclude = globs_to_regex(coverage_files_excluded_patterns)
|
| 182 | + coverage_files_paths = [] |
| 183 | + user_coverage_files_paths = [] |
| 184 | + if self.explicitly_listed_files: |
| 185 | + user_coverage_files_paths = self.get_user_specified_coverage_files( |
| 186 | + regex_patterns_to_exclude |
| 187 | + ) |
| 188 | + if not self.disable_search: |
| 189 | + regex_patterns_to_include = globs_to_regex(coverage_files_patterns) |
| 190 | + coverage_files_paths = search_files( |
| 191 | + self.project_root, |
| 192 | + default_folders_to_ignore + self.folders_to_ignore, |
| 193 | + filename_include_regex=regex_patterns_to_include, |
| 194 | + filename_exclude_regex=regex_patterns_to_exclude, |
| 195 | + ) |
| 196 | + result_files = [ |
| 197 | + UploadCollectionResultFile(path) |
| 198 | + for path in coverage_files_paths |
| 199 | + if coverage_files_paths |
| 200 | + ] |
| 201 | + user_result_files = [ |
| 202 | + UploadCollectionResultFile(path) |
| 203 | + for path in user_coverage_files_paths |
| 204 | + if user_coverage_files_paths |
| 205 | + ] |
| 206 | + |
| 207 | + return list(set(result_files + user_result_files)) |
174 | 208 |
|
175 |
| - coverage_files_paths = search_files( |
176 |
| - self.project_root, |
177 |
| - default_folders_to_ignore + self.folders_to_ignore, |
178 |
| - filename_include_regex=regex_patterns_to_include, |
179 |
| - filename_exclude_regex=regex_patterns_to_exclude, |
| 209 | + def get_user_specified_coverage_files(self, regex_patterns_to_exclude): |
| 210 | + user_filenames_to_include = [] |
| 211 | + files_excluded_but_user_includes = [] |
| 212 | + for file in self.explicitly_listed_files: |
| 213 | + user_filenames_to_include.append(file.name) |
| 214 | + if regex_patterns_to_exclude.match(file.name): |
| 215 | + files_excluded_but_user_includes.append(str(file)) |
| 216 | + if files_excluded_but_user_includes: |
| 217 | + logger.warning( |
| 218 | + "Some files being explicitly added are found in the list of excluded files for upload.", |
| 219 | + extra=dict( |
| 220 | + extra_log_attributes=dict(files=files_excluded_but_user_includes) |
| 221 | + ), |
| 222 | + ) |
| 223 | + regex_patterns_to_include = globs_to_regex(user_filenames_to_include) |
| 224 | + multipart_include_regex = globs_to_regex( |
| 225 | + [str(path.resolve()) for path in self.explicitly_listed_files] |
180 | 226 | )
|
| 227 | + user_coverage_files_paths = list( |
| 228 | + search_files( |
| 229 | + self.project_root, |
| 230 | + default_folders_to_ignore + self.folders_to_ignore, |
| 231 | + filename_include_regex=regex_patterns_to_include, |
| 232 | + filename_exclude_regex=regex_patterns_to_exclude, |
| 233 | + multipart_include_regex=multipart_include_regex, |
| 234 | + ) |
| 235 | + ) |
| 236 | + not_found_files = [] |
| 237 | + for filepath in self.explicitly_listed_files: |
| 238 | + if filepath.resolve() not in user_coverage_files_paths: |
| 239 | + not_found_files.append(filepath) |
| 240 | + |
| 241 | + if not_found_files: |
| 242 | + logger.warning( |
| 243 | + "Some files were not found", |
| 244 | + extra=dict(extra_log_attributes=dict(not_found_files=not_found_files)), |
| 245 | + ) |
181 | 246 |
|
182 |
| - return [UploadCollectionResultFile(path) for path in coverage_files_paths] |
| 247 | + return user_coverage_files_paths |
183 | 248 |
|
184 | 249 |
|
185 | 250 | def select_coverage_file_finder(
|
186 |
| - root_folder_to_search, folders_to_ignore, explicitly_listed_files |
| 251 | + root_folder_to_search, folders_to_ignore, explicitly_listed_files, disable_search |
187 | 252 | ):
|
188 |
| - return CoverageFileFinder(root_folder_to_search, folders_to_ignore) |
| 253 | + return CoverageFileFinder( |
| 254 | + root_folder_to_search, |
| 255 | + folders_to_ignore, |
| 256 | + explicitly_listed_files, |
| 257 | + disable_search, |
| 258 | + ) |
0 commit comments