forked from bregman-arie/devops-exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_lint.py
142 lines (104 loc) · 3.52 KB
/
syntax_lint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Testing suite for https://github.com/bregman-arie/devops-interview-questions
written by surister
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.
Usage:
$ python tests/syntax_lint.py
"""
import pathlib
p = pathlib.Path(__file__).parent.parent.joinpath('README.md')
with open(p, 'rb') as f:
file_list = [line.rstrip() for line in f.readlines()]
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):
if b'<details>' in line:
details_count += 1
if b'</details>' in line:
details_final_count += 1
return details_count == details_final_count
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):
if b'<summary>' in line:
details_count += 1
if b'</summary>' in line:
details_final_count += 1
return details_count == details_final_count
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
err_message = ''
for line_number, line in enumerate(file_list):
if b'<details>' in line and b'</details>' in line:
pass
else:
if b'<details>' in line and after_detail:
err_message = f'Missing closing detail tag round line {line_number - 1}'
error = True
if b'</details>' in line and not after_detail:
err_message = f'Missing opening detail tag round line {line_number - 1}'
error = True
if b'<details>' in line:
after_detail = True
if b'</details>' in line and after_detail:
after_detail = False
if error:
errors.append(err_message)
error = False
def check_summary_tag(file_list):
"""
Check whether the structure:
<summary>
...
</summary>
Is correctly followed, if not generates an error.
"""
after_summary = False
error = False
err_message = ''
for line_number, line in enumerate(file_list):
if b'<summary>' in line and b'</summary>' in line:
pass
else:
if b'<summary>' in line and after_summary:
err_message = f'Missing closing summary tag around line {line_number}'
error = True
if b'</summary>' in line and not after_summary:
err_message = f'Missing opening summary tag around line {line_number}'
error = True
if b'<summary>' in line:
after_summary = True
if b'</summary>' in line and after_summary:
after_summary = False
if error:
errors.append(err_message)
error = False
if __name__ == '__main__':
check_details_tag(file_list)
check_summary_tag(file_list)
if errors:
for error in errors:
print(error)
exit(1)
print("Tests passed successfully.")