Skip to content

Commit

Permalink
fix: fixing errors in config file integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
simojo committed Nov 11, 2023
1 parent d3b90c0 commit a033dcb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 28 deletions.
4 changes: 2 additions & 2 deletions analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
28 changes: 24 additions & 4 deletions chasten/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -175,14 +190,19 @@ 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,
configuration_file_path_str,
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
Expand Down Expand Up @@ -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
Expand Down
62 changes: 40 additions & 22 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ([email protected]:|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(
Expand All @@ -260,7 +266,7 @@ def test_cli_analyze_url_config(cwd):
"analyze",
project_name,
"--search-path",
test_one,
cwd,
"--config",
config_url,
"--verbose",
Expand All @@ -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 = ([email protected]:|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(
Expand All @@ -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",
Expand All @@ -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"
Expand All @@ -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 = ([email protected]:|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(
Expand All @@ -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",
Expand Down

0 comments on commit a033dcb

Please sign in to comment.