2019-12-21 19:24:34 +01:00
|
|
|
"""
|
2019-12-21 21:22:32 +01:00
|
|
|
Testing suite for https://github.com/bregman-arie/devops-interview-questions
|
|
|
|
written by surister
|
2019-12-21 19:24:34 +01:00
|
|
|
|
2019-12-21 21:22:32 +01:00
|
|
|
Even though both check_details_tag and check_summary_tags are practically the
|
|
|
|
same, due to readability and functionality it was decided to be split like
|
|
|
|
that.
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
Usage:
|
2020-01-09 12:12:06 +01:00
|
|
|
$ python tests/syntax_lint.py
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
import sys
|
2019-12-21 19:24:34 +01:00
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
p = sys.argv[1]
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
|
|
|
|
def count_details(file_list):
|
|
|
|
"""
|
|
|
|
Counts the total amount of <details> and </details>
|
|
|
|
|
|
|
|
Used for debugging purpose, not meant to be used in actual tests
|
|
|
|
"""
|
|
|
|
details_final_count = 0
|
|
|
|
details_count = 0
|
|
|
|
|
|
|
|
for line_number, line in enumerate(file_list):
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<details>" in line:
|
2019-12-21 19:24:34 +01:00
|
|
|
details_count += 1
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</details>" in line:
|
2019-12-21 19:24:34 +01:00
|
|
|
details_final_count += 1
|
|
|
|
|
2019-12-24 19:34:31 +01:00
|
|
|
return details_count == details_final_count
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
|
2020-01-09 12:12:06 +01:00
|
|
|
def count_summary(file_list):
|
|
|
|
"""
|
|
|
|
Counts the total amount of <details> and </details>
|
|
|
|
|
|
|
|
Used for debugging purpose, not meant to be used in actual tests
|
|
|
|
"""
|
|
|
|
details_final_count = 0
|
|
|
|
details_count = 0
|
|
|
|
|
|
|
|
for line_number, line in enumerate(file_list):
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<summary>" in line:
|
2020-01-09 12:12:06 +01:00
|
|
|
details_count += 1
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</summary>" in line:
|
2020-01-09 12:12:06 +01:00
|
|
|
details_final_count += 1
|
|
|
|
|
|
|
|
return details_count == details_final_count
|
|
|
|
|
|
|
|
|
2019-12-21 19:24:34 +01:00
|
|
|
def check_details_tag(file_list):
|
|
|
|
"""
|
|
|
|
Check whether the structure:
|
|
|
|
<details>
|
|
|
|
...
|
|
|
|
</details>
|
|
|
|
|
|
|
|
Is correctly followed, if not generates an error.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
after_detail = False
|
|
|
|
error = False
|
2023-02-05 19:50:51 +01:00
|
|
|
err_message = ""
|
2019-12-21 19:24:34 +01:00
|
|
|
for line_number, line in enumerate(file_list):
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<details>" in line and b"</details>" in line:
|
2019-12-21 19:24:34 +01:00
|
|
|
pass
|
|
|
|
else:
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<details>" in line and after_detail:
|
|
|
|
err_message = f"Missing closing detail tag round line {line_number - 1}"
|
2019-12-21 19:24:34 +01:00
|
|
|
error = True
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</details>" in line and not after_detail:
|
|
|
|
err_message = f"Missing opening detail tag round line {line_number - 1}"
|
2019-12-21 19:24:34 +01:00
|
|
|
error = True
|
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<details>" in line:
|
2019-12-21 19:24:34 +01:00
|
|
|
after_detail = True
|
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</details>" in line and after_detail:
|
2019-12-21 19:24:34 +01:00
|
|
|
after_detail = False
|
|
|
|
|
|
|
|
if error:
|
2019-12-21 23:42:12 +01:00
|
|
|
errors.append(err_message)
|
|
|
|
|
|
|
|
error = False
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
def check_summary_tag(file_list):
|
|
|
|
"""
|
|
|
|
Check whether the structure:
|
|
|
|
<summary>
|
|
|
|
...
|
|
|
|
</summary>
|
|
|
|
|
|
|
|
Is correctly followed, if not generates an error.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-12-21 23:18:34 +01:00
|
|
|
after_summary = False
|
2019-12-21 19:24:34 +01:00
|
|
|
error = False
|
2023-02-05 19:50:51 +01:00
|
|
|
err_message = ""
|
|
|
|
for idx, line in enumerate(file_list):
|
|
|
|
line_number = idx + 1
|
|
|
|
if b"<summary>" in line and b"</summary>" in line:
|
|
|
|
if after_summary:
|
|
|
|
err_message = f"Missing closing summary tag around line {line_number}"
|
|
|
|
error = True
|
|
|
|
|
2019-12-21 19:24:34 +01:00
|
|
|
else:
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<summary>" in line and after_summary:
|
|
|
|
err_message = f"Missing closing summary tag around line {line_number}"
|
2019-12-21 19:24:34 +01:00
|
|
|
error = True
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</summary>" in line and not after_summary:
|
|
|
|
err_message = f"Missing opening summary tag around line {line_number}"
|
2019-12-21 19:24:34 +01:00
|
|
|
error = True
|
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"<summary>" in line:
|
2019-12-21 23:18:34 +01:00
|
|
|
after_summary = True
|
2019-12-21 19:24:34 +01:00
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
if b"</summary>" in line and after_summary:
|
2019-12-21 23:18:34 +01:00
|
|
|
after_summary = False
|
2019-12-21 19:24:34 +01:00
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
if error:
|
|
|
|
errors.append(err_message)
|
2019-12-21 23:42:12 +01:00
|
|
|
|
|
|
|
error = False
|
2019-12-21 19:24:34 +01:00
|
|
|
|
|
|
|
|
2023-02-05 19:50:51 +01:00
|
|
|
def check_md_file(file_name):
|
|
|
|
with open(p, "rb") as f:
|
|
|
|
file_list = [line.rstrip() for line in f.readlines()]
|
2019-12-21 19:24:34 +01:00
|
|
|
check_details_tag(file_list)
|
|
|
|
check_summary_tag(file_list)
|
2023-02-05 19:50:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(f"..........Checking {p}..........")
|
|
|
|
check_md_file(p)
|
2019-12-21 23:42:12 +01:00
|
|
|
if errors:
|
2023-02-05 19:50:51 +01:00
|
|
|
print(f"{p} failed", file=sys.stderr)
|
2019-12-21 23:42:12 +01:00
|
|
|
for error in errors:
|
2023-02-05 19:50:51 +01:00
|
|
|
print(error, file=sys.stderr)
|
2019-12-21 23:42:12 +01:00
|
|
|
exit(1)
|
|
|
|
|
|
|
|
print("Tests passed successfully.")
|