1
1
#!/usr/bin/env python3
2
2
import importlib .util
3
3
import json
4
+ import os
4
5
import pathlib
5
6
from types import ModuleType
6
7
from typing import Dict , List
7
8
8
9
import pytest
10
+ import requests
9
11
10
12
PROJECT_EULER_DIR_PATH = pathlib .Path .cwd ().joinpath ("project_euler" )
11
13
PROJECT_EULER_ANSWERS_PATH = pathlib .Path .cwd ().joinpath (
@@ -24,7 +26,7 @@ def convert_path_to_module(file_path: pathlib.Path) -> ModuleType:
24
26
return module
25
27
26
28
27
- def collect_solution_file_paths () -> List [pathlib .Path ]:
29
+ def all_solution_file_paths () -> List [pathlib .Path ]:
28
30
"""Collects all the solution file path in the Project Euler directory"""
29
31
solution_file_paths = []
30
32
for problem_dir_path in PROJECT_EULER_DIR_PATH .iterdir ():
@@ -37,12 +39,51 @@ def collect_solution_file_paths() -> List[pathlib.Path]:
37
39
return solution_file_paths
38
40
39
41
42
+ def get_files_url () -> str :
43
+ """Return the pull request number which triggered this action."""
44
+ with open (os .environ ["GITHUB_EVENT_PATH" ]) as file :
45
+ event = json .load (file )
46
+ return event ["pull_request" ]["url" ] + "/files"
47
+
48
+
49
+ def added_solution_file_path () -> List [pathlib .Path ]:
50
+ """Collects only the solution file path which got added in the current
51
+ pull request.
52
+
53
+ This will only be triggered if the script is ran from GitHub Actions.
54
+ """
55
+ solution_file_paths = []
56
+ headers = {
57
+ "Accept" : "application/vnd.github.v3+json" ,
58
+ "Authorization" : "token " + os .environ ["GITHUB_TOKEN" ],
59
+ }
60
+ files = requests .get (get_files_url (), headers = headers ).json ()
61
+ for file in files :
62
+ filepath = pathlib .Path .cwd ().joinpath (file ["filename" ])
63
+ if (
64
+ filepath .suffix != ".py"
65
+ or filepath .name .startswith (("_" , "test" ))
66
+ or not filepath .name .startswith ("sol" )
67
+ ):
68
+ continue
69
+ solution_file_paths .append (filepath )
70
+ return solution_file_paths
71
+
72
+
73
+ def collect_solution_file_paths () -> List [pathlib .Path ]:
74
+ if os .environ .get ("CI" ) and os .environ .get ("GITHUB_EVENT_NAME" ) == "pull_request" :
75
+ # Return only if there are any, otherwise default to all solutions
76
+ if filepaths := added_solution_file_path ():
77
+ return filepaths
78
+ return all_solution_file_paths ()
79
+
80
+
40
81
@pytest .mark .parametrize (
41
82
"solution_path" ,
42
83
collect_solution_file_paths (),
43
84
ids = lambda path : f"{ path .parent .name } /{ path .name } " ,
44
85
)
45
- def test_project_euler (solution_path : pathlib .Path ):
86
+ def test_project_euler (solution_path : pathlib .Path ) -> None :
46
87
"""Testing for all Project Euler solutions"""
47
88
# problem_[extract this part] and pad it with zeroes for width 3
48
89
problem_number : str = solution_path .parent .name [8 :].zfill (3 )
0 commit comments