Skip to content

Commit 1d41c60

Browse files
author
Konstantinacc
committed
Refactoring of function report_creator.
1 parent 90982c7 commit 1d41c60

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

codebender_testing/utils.py

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,17 @@ def read_last_log(compile_type):
123123
}
124124

125125

126-
# Creates a report json after each compile test
127126
def report_creator(compile_type, log_entry, log_file):
127+
"""Creates a report json after each compile.
128+
`logs`: a list in which all log files located in logs directory are added.
129+
`logs_to_examine`: the list of all log files located in logs directory sorted.
130+
`tail`: the two most recent logs in logs directory.
131+
`diff`: a dictionary where all differences between the two logs are stored.
132+
`changes`: a counter indicating the number of differences found between the two logs.
133+
"""
134+
128135
logs = os.listdir(get_path('logs'))
136+
129137
logs_re = re.compile(r'.+cb_compile_tester.+')
130138
if compile_type == 'library':
131139
logs_re = re.compile(r'.+libraries_test.+')
@@ -134,8 +142,10 @@ def report_creator(compile_type, log_entry, log_file):
134142
elif compile_type == 'target_library':
135143
logs_re = re.compile(r'.+target_libraries.+')
136144

137-
logs = sorted([x for x in logs if x != '.gitignore' and logs_re.match(x)])
145+
logs = sorted([x for x in logs if logs_re.match(x)])
138146
tail = logs[-2:]
147+
148+
# Opens the last, or the last two log files and gathers all their contents.
139149
logs_to_examine = []
140150
for log in tail:
141151
try:
@@ -146,68 +156,75 @@ def report_creator(compile_type, log_entry, log_file):
146156

147157
diff = {}
148158
changes = 0
159+
160+
# We have only one log file, it is the first time we run the test.
161+
if len(logs_to_examine) == 1:
162+
diff = logs_to_examine[0]
163+
changes += 1
164+
165+
# We have more than one log files, it is not the first time we run the test.
149166
if len(logs_to_examine) >= 2:
167+
150168
old_log = logs_to_examine[0]
151169
new_log = logs_to_examine[1]
152170

171+
#Iterate over all new_log keys (urls).
153172
for url in new_log.keys():
173+
174+
# Check if key (url) is included in `old_log`. If not, add an entry to `diff` dictionary.
154175
if url not in old_log:
155176
diff[url] = new_log[url]
156177
changes += 1
157178
continue
158179

180+
"""Check if log comes from test test_libraries_fetch.py test.
181+
If yes, we check if the `old_log[url]`value is the same with
182+
`new_log[url]`value. If not, add an entry to `diff` dictionary."""
159183
if compile_type == 'fetch':
160184
if old_log[url] != new_log[url]:
161185
diff[url] = new_log[url]
162186
changes += 1
163187
continue
164188

165-
for result in new_log[url].keys():
166-
if result not in old_log[url]:
167-
if not url in diff:
189+
"""Iterate over all `new_log[url]` keys. Keys can have one of the following
190+
values: 'success', 'fail', 'open_fail', 'error', 'comment'."""
191+
for result in new_log[url].keys():
192+
193+
"""Check if for the specific url, result is included in
194+
`old_log[url]` keys. If not, check if specific url has an entry in
195+
`diff` dictionary and if not create one. Then add the `result` value.
196+
e.g. `result`: success
197+
`old_log[url].keys()`: ['fail', 'success']"""
198+
199+
if result not in old_log[url].keys():
200+
if url not in diff:
168201
diff[url] = {}
169202
diff[url][result] = new_log[url][result]
170203
changes += 1
171204
continue
172205

173-
if result == 'success' or result == 'fail':
174-
if result not in old_log[url]:
175-
if not url in diff:
206+
# Check if for the specific url, the result is `comment` or `open_fail` or `error`.
207+
if result == 'comment' or result == 'open_fail' or result == 'error':
208+
# Check if the value for the specific result is the same in both logs.
209+
if old_log[url][result] != new_log[url][result]:
210+
# Check if the url is on diff dictionary, if not I add it.
211+
if url not in diff:
176212
diff[url] = {}
177213
diff[url][result] = new_log[url][result]
178214
changes += 1
179-
continue
180215

216+
# Check if for the specific url, the result is `success` or `fail`.
217+
elif result == 'success' or result == 'fail':
181218
for board in new_log[url][result]:
182-
oposite = 'success'
183-
if result == 'success':
184-
oposite = 'fail'
185-
if oposite in old_log[url] and board in old_log[url][oposite]:
186-
if not url in diff:
219+
if board not in old_log[url][result]:
220+
if url not in diff:
187221
diff[url] = {}
188222
if result not in diff[url]:
189223
diff[url][result] = []
190224
diff[url][result].append(board)
191225
changes += 1
192-
elif result == 'open_fail' or result == 'error' or result == 'comment':
193-
if result not in old_log[url]:
194-
if not url in diff:
195-
diff[url] = {}
196-
diff[url][result] = new_log[url][result]
197-
changes += 1
198-
continue
199-
if old_log[url][result] != new_log[url][result]:
200-
if not url in diff:
201-
diff[url] = {}
202-
diff[url][result] = new_log[url][result]
203-
changes += 1
204-
elif len(logs_to_examine) == 1:
205-
diff = logs_to_examine[0]
206-
changes += 1
207-
else:
208-
diff = log_entry
209-
changes += 1
210226

227+
#Create report and write the results.
211228
filename_tokens = os.path.basename(log_file).split('.')
212229
filename = '.'.join(filename_tokens[0:-1])
213230
extension = filename_tokens[-1]

0 commit comments

Comments
 (0)