Skip to content

Commit 490c460

Browse files
committed
Add wait for log method. Add methods for append and extract text. Add vscode files to gitignore
1 parent a4d3465 commit 490c460

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
@@ -106,6 +106,11 @@ def write(path, text):
106106
with open(path, 'w+') as text_file:
107107
text_file.write(text)
108108

109+
@staticmethod
110+
def append(path, text):
111+
with open(path, 'a') as text_file:
112+
text_file.write(text)
113+
109114
@staticmethod
110115
def replace(path, old_string, new_string):
111116
content = File.read(path=path)
@@ -158,3 +163,13 @@ def find_by_extension(folder, extension):
158163
Log.debug('File with {0} extension found: {1}'.format(extension, os.path.abspath(f)))
159164
matches.append(os.path.join(root, f))
160165
return matches
166+
167+
@staticmethod
168+
def extract_part_of_text(text, key_word):
169+
"""
170+
That method will extract text from last occurance of key word
171+
to the end of the file
172+
"""
173+
index = text.rfind(key_word)
174+
text = text[index:]
175+
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,6 +1,8 @@
11
import os
2+
import time
23

34
from core.settings import Settings
5+
from core.utils.file_utils import File
46

57

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

0 commit comments

Comments
 (0)