From a033dcb2676ec2d45e06b90285b2ec27f95f5c6d Mon Sep 17 00:00:00 2001 From: Simon Jones Date: Sat, 11 Nov 2023 17:30:35 -0500 Subject: [PATCH] fix: fixing errors in config file integration testing --- analysis.md | 4 +-- chasten/configuration.py | 28 +++++++++++++++--- tests/test_main.py | 62 ++++++++++++++++++++++++++-------------- 3 files changed, 66 insertions(+), 28 deletions(-) diff --git a/analysis.md b/analysis.md index b2d1fb15..a2351505 100644 --- a/analysis.md +++ b/analysis.md @@ -57,7 +57,7 @@ - /_scratch/chasten/chasten/output.py - 7 matches - /_scratch/chasten/chasten/checks.py - 13 matches - /_scratch/chasten/chasten/validate.py - 3 matches - - /_scratch/chasten/chasten/configuration.py - 15 matches + - /_scratch/chasten/chasten/configuration.py - 18 matches - /_scratch/chasten/chasten/filesystem.py - 11 matches # PASSED: **ID:** 'CL002', **Name:** 'dummy-test-double-nested-if', **Pattern:** './/FunctionDef/body//If\[ancestor::If and not(parent::orelse)]', min=None, max=None @@ -67,5 +67,5 @@ - /_scratch/chasten/chasten/output.py - 1 matches - /_scratch/chasten/chasten/checks.py - 3 matches - /_scratch/chasten/chasten/validate.py - 1 matches - - /_scratch/chasten/chasten/configuration.py - 3 matches + - /_scratch/chasten/chasten/configuration.py - 5 matches - /_scratch/chasten/chasten/filesystem.py - 3 matches diff --git a/chasten/configuration.py b/chasten/configuration.py index 1485ab22..98187159 100644 --- a/chasten/configuration.py +++ b/chasten/configuration.py @@ -118,6 +118,7 @@ def validate_configuration_files( """Validate the configuration.""" chasten_user_config_url_str = "" chasten_user_config_dir_str = "" + chasten_user_config_file_str = "" # there is a specified configuration directory path or url; # this overrides the use of the configuration files that # may exist inside of the platform-specific directory @@ -131,8 +132,22 @@ def validate_configuration_files( chasten_user_config_url_str = str(parse_url(config)) # input configuration is valid file path elif Path(config).exists(): - # re-parse input config so it is of type Path - chasten_user_config_dir_str = str(Path(config)) + # input configuration is a directory + if Path(config).is_dir(): + # re-parse input config so it is of type Path + chasten_user_config_dir_str = str(Path(config)) + # input configuration is a file + elif Path(config).is_file(): + # re-parse input config so it is of type Path + config_as_path = Path(config) + # get directory containing config file + chasten_user_config_dir_str = str(Path(*config_as_path.parts[: len(config_as_path.parts) - 1])) + # isolate config file + chasten_user_config_file_str = str(config_as_path.parts[-1]) + else: + output.logger.error( + "\nGiven configuration was a Path, but was the wrong file type.\n" + ) # the configuration file does not exist and thus, # since config was explicit, it is not possible # to validate the configuration file @@ -175,6 +190,11 @@ def validate_configuration_files( + chasten_user_config_dir_str + constants.markers.Newline ) + # optional argument if chasten_user_config_file_str is not empty + # argument will be supplied as unpacked dict + chasten_user_config_file_str_argument = {} + if chasten_user_config_file_str != "": + chasten_user_config_file_str_argument["configuration_file"] = chasten_user_config_file_str # extract the configuration details ( configuration_valid, @@ -182,7 +202,7 @@ def validate_configuration_files( configuration_file_yaml_str, yaml_data_dict, ) = extract_configuration_details_from_config_dir( - Path(chasten_user_config_dir_str) + Path(chasten_user_config_dir_str), **chasten_user_config_file_str_argument ) # it was not possible to extract the configuration details and # thus this function should return immediately with False @@ -260,7 +280,7 @@ def validate_configuration_files( output.logger.error( f"\nChecks file directive was not a valid Path or URL (given: '{checks_file_name}')\n" ) - # FIXME: print error or something + return (False, {}) # the checks file could not be extracted in a valid # fashion and thus there is no need to continue the # validation of this file or any of the other check file diff --git a/tests/test_main.py b/tests/test_main.py index cb99e95f..469f2861 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -247,11 +247,17 @@ def test_cli_analyze_incorrect_arguments_correct_config(tmpdir): def test_cli_analyze_url_config(cwd): """Confirm that using the command-line interface correctly handles a valid URL configuration.""" - # get current git branch to use - git_directory = cwd / Path(".git") / Path("HEAD") - branch = re.findall("(?:ref: refs/heads/).+", git_directory.read_text())[0] + git_HEAD = cwd / Path(".git") / Path("HEAD") + git_config = cwd / Path(".git") / Path("config") + # get current git branch to use by reading local git data + branch = re.match("(?:ref: refs/heads/)(\S+)", git_HEAD.read_text()).group(1) + # get the git repo ("owner/repo") by reading local git data + owner_slash_repo = re.search( + '(?:\[remote "origin"\]\n\turl = (git@github.com:|https://github.com/))(\S+?)(?:\.git)', + git_config.read_text(), + ).group(2) # use current git branch to fetch config files from raw text repo files - config_url = f"https://raw.githubusercontent.com/AstuteSource/chasten/{branch}/.chasten/config_url_checks_file.yml" + config_url = f"https://raw.githubusercontent.com/{owner_slash_repo}/{branch}/.chasten/config_url_checks_file.yml" project_name = "test" # call the analyze command result = runner.invoke( @@ -260,7 +266,7 @@ def test_cli_analyze_url_config(cwd): "analyze", project_name, "--search-path", - test_one, + cwd, "--config", config_url, "--verbose", @@ -269,13 +275,19 @@ def test_cli_analyze_url_config(cwd): assert result.exit_code == 0 -def test_cli_analyze_url_config_with_local_checks_file(): +def test_cli_analyze_url_config_with_local_checks_file(cwd): """Confirm that using the command-line interface aborts execution when given a URL config that uses a local file path to specify checks files.""" - # get current git branch to use - git_directory = cwd / Path(".git") / Path("HEAD") - branch = re.findall("(?:ref: refs/heads/).+", git_directory.read_text())[0] + git_HEAD = cwd / Path(".git") / Path("HEAD") + git_config = cwd / Path(".git") / Path("config") + # get current git branch to use by reading local git data + branch = re.match("(?:ref: refs/heads/)(\S+)", git_HEAD.read_text()).group(1) + # get the git repo ("owner/repo") by reading local git data + owner_slash_repo = re.search( + '(?:\[remote "origin"\]\n\turl = (git@github.com:|https://github.com/))(\S+?)(?:\.git)', + git_config.read_text(), + ).group(2) # use current git branch to fetch config files from raw text repo files - config_url = f"https://raw.githubusercontent.com/AstuteSource/chasten/{branch}/.chasten/config.yml" + config_url = f"https://raw.githubusercontent.com/{owner_slash_repo}/{branch}/.chasten/config.yml" project_name = "test" # call the analyze command result = runner.invoke( @@ -284,7 +296,7 @@ def test_cli_analyze_url_config_with_local_checks_file(): "analyze", project_name, "--search-path", - test_one, + cwd, "--config", config_url, "--verbose", @@ -305,16 +317,16 @@ def test_cli_analyze_local_config_with_url_checks_file(cwd): "analyze", project_name, "--search-path", - test_one, + cwd, "--config", - config_url, + configuration_file, "--verbose", ], ) assert result.exit_code == 0 -def test_cli_analyze_local_config_with_url_and_filesystem_checks_files(cwd): +def test_cli_analyze_local_config_with_url_and_local_checks_files(cwd): """Confirm that using the command-line interface correctly handles a local config that references a combination of URL endpoints and local files for each checks file.""" configuration_file = cwd / Path(".chasten") / Path("config_url_and_local_checks_files.yml") project_name = "test" @@ -325,22 +337,28 @@ def test_cli_analyze_local_config_with_url_and_filesystem_checks_files(cwd): "analyze", project_name, "--search-path", - test_one, + cwd, "--config", - config_url, + configuration_file, "--verbose", ], ) assert result.exit_code == 0 -def test_cli_analyze_url_config_with_url_and_filesystem_checks_files(): +def test_cli_analyze_url_config_with_url_and_local_checks_files(cwd): """Confirm that using the command-line interface aborts execution when given a URL config that references a combination of URL endpoints and local files for each checks file.""" - # get current git branch to use - git_directory = cwd / Path(".git") / Path("HEAD") - branch = re.findall("(?:ref: refs/heads/).+", git_directory.read_text())[0] + git_HEAD = cwd / Path(".git") / Path("HEAD") + git_config = cwd / Path(".git") / Path("config") + # get current git branch to use by reading local git data + branch = re.match("(?:ref: refs/heads/)(\S+)", git_HEAD.read_text()).group(1) + # get the git repo ("owner/repo") by reading local git data + owner_slash_repo = re.search( + '(?:\[remote "origin"\]\n\turl = (git@github.com:|https://github.com/))(\S+?)(?:\.git)', + git_config.read_text(), + ).group(2) # use current git branch to fetch config files from raw text repo files - config_url = f"https://raw.githubusercontent.com/AstuteSource/chasten/{branch}/.chasten/config_url_and_local_checks_files.yml" + config_url = f"https://raw.githubusercontent.com/{owner_slash_repo}/{branch}/.chasten/config_url_and_local_checks_files.yml" project_name = "test" # call the analyze command result = runner.invoke( @@ -349,7 +367,7 @@ def test_cli_analyze_url_config_with_url_and_filesystem_checks_files(): "analyze", project_name, "--search-path", - test_one, + cwd, "--config", config_url, "--verbose",