|
1 | 1 | import json
|
2 |
| -import os |
3 |
| -import re |
4 | 2 | from pathlib import Path
|
5 | 3 |
|
6 |
| -# Function to print GitHub action notice |
7 |
| -def print_github_action_notice(file, url): |
8 |
| - print(f"::warning file={file},line=1::Annotation: {url}") |
| 4 | +def github_action_notice(file, url, line): |
| 5 | + """ |
| 6 | + Print GitHub action notice with file path, URL, and line number. |
9 | 7 |
|
10 |
| -# Read specification URLs from JSON file |
11 |
| -with open("bin/specification_urls.json", "r") as f: |
| 8 | + Parameters: |
| 9 | + file (str): File path. |
| 10 | + url (str): URL. |
| 11 | + line (int): Line number. |
| 12 | + """ |
| 13 | + return f"::warning file={file},line={line}::Annotation: {url}" |
| 14 | + |
| 15 | +def find_test_line_number(test_content, test_name): |
| 16 | + """ |
| 17 | + Find the line number of a test in the JSON content. |
| 18 | +
|
| 19 | + Parameters: |
| 20 | + test_content (str): JSON content. |
| 21 | + test_name (str): Test name. |
| 22 | +
|
| 23 | + Returns: |
| 24 | + int: Line number of the test. |
| 25 | + """ |
| 26 | + lines = test_content.split("\n") # Split content into lines |
| 27 | + for i, line in enumerate(lines, start=1): # Iterate over lines |
| 28 | + if test_name in line: # Check if test name is found in the line |
| 29 | + return i # Return the line number if found |
| 30 | + return 1 # Return None if test name is not found |
| 31 | + |
| 32 | +# Specify the path to the JSON file using pathlib.Path |
| 33 | +json_file_path = Path("bin/specification_urls.json") |
| 34 | + |
| 35 | +# Read specification URLs from JSON file using pathlib.Path |
| 36 | +with json_file_path.open("r", encoding="utf-8") as f: |
12 | 37 | urls = json.load(f)
|
13 | 38 |
|
14 | 39 | # Iterate through JSON files in tests folder and subdirectories
|
15 |
| -for root, dirs, files in os.walk("tests"): |
16 |
| - for file_name in files: |
17 |
| - if file_name.endswith(".json"): |
18 |
| - file_path = os.path.join(root, file_name) |
19 |
| - # Read the file content |
20 |
| - with open(file_path, 'r', encoding='utf-8') as f: |
21 |
| - changed_file_content = f.read() |
22 |
| - |
23 |
| - # Parse JSON content |
24 |
| - try: |
25 |
| - json_content = json.loads(changed_file_content) |
26 |
| - for test in json_content: |
27 |
| - if "specification" in test: |
28 |
| - for specification_object in test["specification"]: |
29 |
| - for spec, section in specification_object.items(): |
30 |
| - draft = Path(file_path).parent.name |
31 |
| - if spec in ["core", "validation", "hyper-schema"]: |
32 |
| - print_github_action_notice(file_path, urls[draft][spec] + section) |
33 |
| - elif spec in ["quote"]: |
34 |
| - continue |
35 |
| - elif spec in ["ecma262", "perl5"]: |
36 |
| - print_github_action_notice(file_path, urls[spec] + section) |
37 |
| - elif re.match("^rfc\\d+$", spec): |
38 |
| - print_github_action_notice(file_path, urls["rfc"] + spec + ".txt#" + section) |
39 |
| - else: |
40 |
| - print_github_action_notice(file_path, urls["iso"]) |
41 |
| - except json.JSONDecodeError: |
42 |
| - print(f"Failed to parse JSON content for file: {file_path}") |
| 40 | +for file_path in Path("tests").rglob("*.json"): |
| 41 | + # Read the file content using pathlib.Path |
| 42 | + with file_path.open('r', encoding='utf-8') as f: |
| 43 | + changed_file_content = f.read() |
| 44 | + |
| 45 | + # Parse JSON content |
| 46 | + try: |
| 47 | + json_content = json.loads(changed_file_content) |
| 48 | + for test in json_content: |
| 49 | + if "specification" in test: |
| 50 | + line_number = find_test_line_number(changed_file_content, test.get("description") ) |
| 51 | + |
| 52 | + for specification_object in test["specification"]: |
| 53 | + for spec, section in specification_object.items(): |
| 54 | + draft = file_path.parent.name |
| 55 | + if spec in ["quote"]: |
| 56 | + continue |
| 57 | + elif spec in ["core", "validation", "hyper-schema"]: |
| 58 | + url = urls[draft][spec].format(spec=spec, section=section) |
| 59 | + else: |
| 60 | + url = urls[spec].format(spec=spec, section=section) |
| 61 | + annotation = github_action_notice(file_path, url, line_number) |
| 62 | + print(annotation) |
| 63 | + |
| 64 | + except json.JSONDecodeError: |
| 65 | + print(f"::error file={file_path}::Failed to parse JSON content") |
0 commit comments