Skip to content

Commit 9db091e

Browse files
committed
Add wait for log method. Add methods for append and extract text. Add vscode files to gitignore
1 parent a5bc939 commit 9db091e

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sample-Groceries
1212
master-detail*
1313
hello-world*
1414
tab-navigation*
15+
.vscode
1516

1617
# Byte-compiled / optimized / DLL files
1718
__pycache__/

core/utils/file_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ def write(path, text):
107107
with open(path, 'w+') as text_file:
108108
text_file.write(text)
109109

110+
@staticmethod
111+
def append(path, text):
112+
with open(path, 'a') as text_file:
113+
text_file.write(text)
114+
110115
@staticmethod
111116
def replace(path, old_string, new_string):
112117
content = File.read(path=path)
@@ -160,3 +165,13 @@ def find_by_extension(folder, extension):
160165
Log.debug('File with {0} extension found: {1}'.format(extension, os.path.abspath(f)))
161166
matches.append(os.path.join(root, f))
162167
return matches
168+
169+
@staticmethod
170+
def extract_part_of_text(text, key_word):
171+
"""
172+
That method will extract text from last occurance of key word
173+
to the end of the file
174+
"""
175+
index = text.rfind(key_word)
176+
text = text[index:]
177+
return text

core_tests/products/tns_tests.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from core.utils.file_utils import File
1010
from core.utils.wait import Wait
1111
from products.nativescript.tns import Tns
12+
from products.nativescript.tns_helpers import TnsHelpers
1213

1314

1415
class TnsTests(TnsTest):
@@ -47,9 +48,7 @@ def test_001_tns_run_android(self):
4748

4849
# Wait until app is build and installed.
4950
texts = ['Project successfully built', 'Successfully installed']
50-
for text in texts:
51-
found = Wait.until(lambda: text in File.read(result.log_file), timeout=180, period=10)
52-
assert found, '"{0}" not found in logs.'.format(text)
51+
TnsHelpers.wait_for_log(result.log_file, texts)
5352

5453
@timed(300)
5554
def test_002_tns_run_android_with_justlaunch(self):

products/nativescript/tns_helpers.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# pylint: disable=unused-argument
22
# TODO: Implement it!
33
import os
4+
import time
45

56
from core.settings import Settings
7+
from core.utils.file_utils import File
68

79

810
# noinspection PyUnusedLocal
@@ -24,3 +26,63 @@ def get_ipa(app_name):
2426
@staticmethod
2527
def get_app(app_name):
2628
return ''
29+
30+
@staticmethod
31+
def wait_for_log(log_file, string_list, not_existing_string_list=None, timeout=45, check_interval=3):
32+
"""
33+
Wait until log file contains list of string.
34+
:param log_file: Path to log file.
35+
:param string_list: List of strings.
36+
:param not_existing_string_list: List of string that should not be in logs.
37+
:param timeout: Timeout.
38+
:param check_interval: Check interval.
39+
"""
40+
end_time = time.time() + timeout
41+
all_items_found = False
42+
not_found_list = []
43+
log = ""
44+
verified_flag = '[VERIFIED]'
45+
while time.time() < end_time:
46+
not_found_list = []
47+
log = File.read(log_file)
48+
# Extract the part of the log that hasn't been previously verified
49+
if verified_flag in log:
50+
log = File.extract_part_of_text(log, verified_flag)
51+
for item in string_list:
52+
if item in log:
53+
print "'{0}' found.".format(item)
54+
else:
55+
not_found_list.append(item)
56+
if not_found_list == []:
57+
all_items_found = True
58+
print "Log contains: {0}".format(string_list)
59+
break
60+
else:
61+
print "'{0}' NOT found. Wait...".format(not_found_list)
62+
time.sleep(check_interval)
63+
if 'BUILD FAILED' in log:
64+
print 'BUILD FAILED. No need to wait more time!'
65+
break
66+
if 'Unable to sync files' in log:
67+
print 'Sync process failed. No need to wait more time!'
68+
break
69+
if 'errors were thrown' in log:
70+
print 'Multiple errors were thrown. No need to wait more time!'
71+
break
72+
73+
# Mark that part of the log as verified by appending a flag at the end.
74+
# The second time we read the file we will verify only the text after that flag
75+
File.append(log_file, verified_flag)
76+
77+
if all_items_found:
78+
if not_existing_string_list is None:
79+
pass
80+
else:
81+
for item in not_existing_string_list:
82+
assert item not in log, "{0} found! It should not be in logs.\nLog:\n{1}".format(item, log)
83+
else:
84+
print "##### OUTPUT BEGIN #####\n"
85+
print log
86+
print "##### OUTPUT END #####\n"
87+
print ""
88+
assert False, "Output does not contain {0}".format(not_found_list)

0 commit comments

Comments
 (0)