2020-09-28 06:18:19 +00:00
|
|
|
#!/usr/bin/env python3
|
2021-05-12 07:48:23 +00:00
|
|
|
import hashlib
|
2020-09-28 06:18:19 +00:00
|
|
|
import importlib.util
|
|
|
|
import json
|
2020-11-29 17:41:09 +00:00
|
|
|
import os
|
2020-09-28 06:18:19 +00:00
|
|
|
import pathlib
|
|
|
|
from types import ModuleType
|
|
|
|
|
|
|
|
import pytest
|
2020-11-29 17:41:09 +00:00
|
|
|
import requests
|
2020-09-28 06:18:19 +00:00
|
|
|
|
|
|
|
PROJECT_EULER_DIR_PATH = pathlib.Path.cwd().joinpath("project_euler")
|
2020-10-24 13:37:33 +00:00
|
|
|
PROJECT_EULER_ANSWERS_PATH = pathlib.Path.cwd().joinpath(
|
|
|
|
"scripts", "project_euler_answers.json"
|
2020-09-28 06:18:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
with open(PROJECT_EULER_ANSWERS_PATH) as file_handle:
|
2021-05-12 07:48:23 +00:00
|
|
|
PROBLEM_ANSWERS: dict[str, str] = json.load(file_handle)
|
2020-09-28 06:18:19 +00:00
|
|
|
|
|
|
|
|
2020-10-13 10:11:12 +00:00
|
|
|
def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
|
|
|
|
"""Converts a file path to a Python module"""
|
|
|
|
spec = importlib.util.spec_from_file_location(file_path.name, str(file_path))
|
2021-06-08 20:49:33 +00:00
|
|
|
module = importlib.util.module_from_spec(spec) # type: ignore
|
2021-04-07 02:42:56 +00:00
|
|
|
spec.loader.exec_module(module) # type: ignore
|
2020-10-13 10:11:12 +00:00
|
|
|
return module
|
|
|
|
|
|
|
|
|
2021-05-12 07:48:23 +00:00
|
|
|
def all_solution_file_paths() -> list[pathlib.Path]:
|
2020-10-13 10:11:12 +00:00
|
|
|
"""Collects all the solution file path in the Project Euler directory"""
|
|
|
|
solution_file_paths = []
|
|
|
|
for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
|
|
|
|
if problem_dir_path.is_file() or problem_dir_path.name.startswith("_"):
|
2020-09-28 06:18:19 +00:00
|
|
|
continue
|
2020-10-13 10:11:12 +00:00
|
|
|
for file_path in problem_dir_path.iterdir():
|
|
|
|
if file_path.suffix != ".py" or file_path.name.startswith(("_", "test")):
|
|
|
|
continue
|
|
|
|
solution_file_paths.append(file_path)
|
|
|
|
return solution_file_paths
|
|
|
|
|
|
|
|
|
2020-11-29 17:41:09 +00:00
|
|
|
def get_files_url() -> str:
|
|
|
|
"""Return the pull request number which triggered this action."""
|
|
|
|
with open(os.environ["GITHUB_EVENT_PATH"]) as file:
|
|
|
|
event = json.load(file)
|
|
|
|
return event["pull_request"]["url"] + "/files"
|
|
|
|
|
|
|
|
|
2021-05-12 07:48:23 +00:00
|
|
|
def added_solution_file_path() -> list[pathlib.Path]:
|
2020-11-29 17:41:09 +00:00
|
|
|
"""Collects only the solution file path which got added in the current
|
|
|
|
pull request.
|
|
|
|
|
|
|
|
This will only be triggered if the script is ran from GitHub Actions.
|
|
|
|
"""
|
|
|
|
solution_file_paths = []
|
|
|
|
headers = {
|
|
|
|
"Accept": "application/vnd.github.v3+json",
|
2023-10-15 19:40:13 +00:00
|
|
|
"Authorization": "token " + os.environ["GITHUB_TOKEN"],
|
2020-11-29 17:41:09 +00:00
|
|
|
}
|
|
|
|
files = requests.get(get_files_url(), headers=headers).json()
|
|
|
|
for file in files:
|
|
|
|
filepath = pathlib.Path.cwd().joinpath(file["filename"])
|
|
|
|
if (
|
|
|
|
filepath.suffix != ".py"
|
|
|
|
or filepath.name.startswith(("_", "test"))
|
|
|
|
or not filepath.name.startswith("sol")
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
solution_file_paths.append(filepath)
|
|
|
|
return solution_file_paths
|
|
|
|
|
|
|
|
|
2021-05-12 07:48:23 +00:00
|
|
|
def collect_solution_file_paths() -> list[pathlib.Path]:
|
2020-11-29 17:41:09 +00:00
|
|
|
if os.environ.get("CI") and os.environ.get("GITHUB_EVENT_NAME") == "pull_request":
|
|
|
|
# Return only if there are any, otherwise default to all solutions
|
|
|
|
if filepaths := added_solution_file_path():
|
|
|
|
return filepaths
|
|
|
|
return all_solution_file_paths()
|
|
|
|
|
|
|
|
|
2020-10-13 10:11:12 +00:00
|
|
|
@pytest.mark.parametrize(
|
2020-10-15 07:13:28 +00:00
|
|
|
"solution_path",
|
|
|
|
collect_solution_file_paths(),
|
|
|
|
ids=lambda path: f"{path.parent.name}/{path.name}",
|
2020-10-13 10:11:12 +00:00
|
|
|
)
|
2020-11-29 17:41:09 +00:00
|
|
|
def test_project_euler(solution_path: pathlib.Path) -> None:
|
2020-10-13 10:11:12 +00:00
|
|
|
"""Testing for all Project Euler solutions"""
|
2020-10-15 07:13:28 +00:00
|
|
|
# problem_[extract this part] and pad it with zeroes for width 3
|
|
|
|
problem_number: str = solution_path.parent.name[8:].zfill(3)
|
2020-10-13 10:11:12 +00:00
|
|
|
expected: str = PROBLEM_ANSWERS[problem_number]
|
|
|
|
solution_module = convert_path_to_module(solution_path)
|
2021-04-07 02:42:56 +00:00
|
|
|
answer = str(solution_module.solution()) # type: ignore
|
2021-05-12 07:48:23 +00:00
|
|
|
answer = hashlib.sha256(answer.encode()).hexdigest()
|
|
|
|
assert (
|
|
|
|
answer == expected
|
|
|
|
), f"Expected solution to {problem_number} to have hash {expected}, got {answer}"
|