From 205a36cae48e538e6741ee5fd1d9bec0dac0355d Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Fri, 19 Mar 2021 14:03:30 +0000 Subject: [PATCH] Assure project_dir is always valid (#1482) Avoid case where project_dir could endup being None. --- src/ansiblelint/cli.py | 11 +++++++---- src/ansiblelint/config.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/ansiblelint/cli.py b/src/ansiblelint/cli.py index af76a91f3e..508cb4e913 100644 --- a/src/ansiblelint/cli.py +++ b/src/ansiblelint/cli.py @@ -187,7 +187,7 @@ def get_cli_parser() -> argparse.ArgumentParser: parser.add_argument( '--project-dir', dest='project_dir', - default=None, + default=".", help="Location of project/repository, autodetected based on location " " of configuration file.", ) @@ -324,7 +324,7 @@ def merge_config(file_config: Dict[Any, Any], cli_config: Namespace) -> Namespac 'enable_list': [], } - scalar_map = {"loop_var_prefix": None, "project_dir": None} + scalar_map = {"loop_var_prefix": None, "project_dir": "."} if not file_config: # use defaults if we don't have a config file and the commandline @@ -377,15 +377,18 @@ def get_config(arguments: List[str]) -> Namespace: options.rulesdirs = get_rules_dirs(options.rulesdir, options.use_default_rules) - if not options.project_dir: + if options.project_dir == ".": project_dir = os.path.dirname( os.path.abspath( options.config_file or f"{guess_project_dir()}/.ansible-lint" ) ) options.project_dir = normpath(project_dir) + if not options.project_dir or not os.path.exists(options.project_dir): + raise RuntimeError( + f"Failed to determine a valid project_dir: {options.project_dir}" + ) - # print(666, options.quiet, options.verbosity) # Compute final verbosity level by subtracting -q counter. options.verbosity -= options.quiet return config diff --git a/src/ansiblelint/config.py b/src/ansiblelint/config.py index 1ffb2b30f1..2ff68905e8 100644 --- a/src/ansiblelint/config.py +++ b/src/ansiblelint/config.py @@ -72,7 +72,7 @@ mock_roles=[], loop_var_prefix=None, offline=False, - project_dir=None, + project_dir=".", # default should be valid folder (do not use None here) extra_vars=None, enable_list=[], skip_action_validation=True,