Skip to content

Instantly share code, notes, and snippets.

@chethann
Last active February 29, 2024 18:04
Show Gist options
  • Save chethann/65d7075d0a177f7015d0fdf585865b66 to your computer and use it in GitHub Desktop.
Save chethann/65d7075d0a177f7015d0fdf585865b66 to your computer and use it in GitHub Desktop.
from xml.dom import minidom
from pathlib import Path
import sys
import os
import re
import glob
import json
import argparse
path_to_konsist_report = os.getcwd() + '/konsistTest/build/test-results/testAppProductionDebugUnitTest'
baseline_file_name = 'konsist_baseline.txt'
def run(argv):
parser = argparse.ArgumentParser()
parser.add_argument("--task", help="Determine which task to run")
args=parser.parse_args()
if args.task == 'baseline':
print('running baseline')
generate_baseline()
else:
check_if_baseline_is_breached()
def generate_baseline():
os.chdir(path_to_konsist_report)
result = []
for file in glob.glob('*.xml'):
tests = get_current_konsist_test_and_issues_counts(file)
result.append({
'file': file,
'tests': tests
})
json_string = json.dumps(result)
os.chdir(Path(__file__).absolute().parent)
with open(baseline_file_name, 'w') as f:
f.write(json_string)
print(json_string)
def check_if_baseline_is_breached():
os.chdir(str(Path(__file__).absolute().parent))
with open(baseline_file_name, 'r') as file:
data = file.read().rstrip()
baseline = json.loads(data)
baseline_breaches = []
os.chdir(path_to_konsist_report)
for file in glob.glob('*.xml'):
tests = get_current_konsist_test_and_issues_counts(file)
tests_in_baseline = get_test_and_failure_count_list_in_baseline(file, baseline)
if len(tests) != len(tests_in_baseline):
print('Baseline doesn\'t have info about all the konsist tests Please run baseline')
sys.exit(2)
for test in tests:
test_in_baseline = list(filter(lambda baseline_test: baseline_test['testcase'] == test['testcase'], tests_in_baseline))
if len(test_in_baseline) == 1:
if (test.get('errors') > test_in_baseline[0].get('errors')): #fail
baseline_breaches.append({
'testcase': test_in_baseline[0].get('testcase'),
'baseline': test_in_baseline[0].get('errors'),
'new': test.get('errors')
})
else:
print('Baseline doesn\'t have info about all the konsist tests Please run baseline')
sys.exit(3)
if len(baseline_breaches) == 0:
print('Success')
sys.exit(0)
else:
print('Failed')
for baseline_breach in baseline_breaches:
print(baseline_breach.get('testcase') + ' baseline errors count: ' + baseline_breach.get('baseline') + ', new errors count ' + baseline_breach.get('new'))
sys.exit(4)
def get_test_and_failure_count_list_in_baseline(file, baseline):
baseline_for_file = list(filter(lambda baseline_for: baseline_for['file'] == file, baseline))
if len(baseline_for_file) == 0:
print('Coundn\'t fine baseline details for ' + file + ' make sure to run baseline whenever a new konsist rule is added')
sys.exit(1)
else:
return baseline_for_file[0].get('tests')
def get_current_konsist_test_and_issues_counts(filePath):
file = minidom.parse(filePath)
testSuit = file.getElementsByTagName('testsuite')
testcases = file.getElementsByTagName("testcase")
result = []
for testcase in testcases:
testcase_details = get_test_case_details(testcase)
result.append(testcase_details)
return result
def get_failure_count(text):
p = re.compile("was violated \((.*) time")
match = p.search(text)
return match.group(1)
def get_test_case_details(testcase):
test_case_name = testcase.getAttribute("name")
failure = testcase.getElementsByTagName("failure")
if len(failure) == 0:
return {
'testcase': test_case_name,
'errors': 0
}
else:
error_message = failure[0].getAttribute("message")
error_count = get_failure_count(error_message)
return {
'testcase': test_case_name,
'errors': error_count
}
if __name__ == '__main__':
run(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment