Skip to content

Commit 4ec4f6a

Browse files
authored
Merge pull request #20 from NativeScript/endarova/wait_for_log_method
feat: wait for log method
2 parents a5bc939 + 9209768 commit 4ec4f6a

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
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__/

.pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ max-attributes=7
519519
max-bool-expr=5
520520

521521
# Maximum number of branch for function / method body.
522-
max-branches=12
522+
max-branches=20
523523

524524
# Maximum number of locals for function / method body.
525525
max-locals=15

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
from core.enums.os_type import OSType
77
from core.settings import Settings
88
from core.utils.device.device_manager import DeviceManager
9-
from core.utils.file_utils import File
10-
from core.utils.wait import Wait
119
from products.nativescript.tns import Tns
10+
from products.nativescript.tns_helpers import TnsHelpers
1211

1312

1413
class TnsTests(TnsTest):
@@ -47,9 +46,7 @@ def test_001_tns_run_android(self):
4746

4847
# Wait until app is build and installed.
4948
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)
49+
TnsHelpers.wait_for_log(result.log_file, texts)
5350

5451
@timed(300)
5552
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,11 @@
11
# pylint: disable=unused-argument
22
# TODO: Implement it!
33
import os
4+
import time
45

6+
from core.log.log import Log
57
from core.settings import Settings
8+
from core.utils.file_utils import File
69

710

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

0 commit comments

Comments
 (0)