diff --git a/src/ifcbimtester/bimtester.py b/src/ifcbimtester/bimtester.py index 3cac23554b..0c7b1acc2e 100644 --- a/src/ifcbimtester/bimtester.py +++ b/src/ifcbimtester/bimtester.py @@ -1,4 +1,7 @@ -# This can be packaged with `pyinstaller --onefile --clean --icon=icon.ico bimtester.py` +# Unix: +# $ pyinstaller --onefile --clean --icon=icon.ico --add-data "features:features" bimtester.py` +# Windows: +# $ pyinstaller --onefile --clean --icon=icon.ico --add-data "features;features" bimtester.py` from behave.__main__ import main as behave_main import behave.formatter.pretty # Needed for pyinstaller to package it @@ -11,26 +14,59 @@ import json import argparse import csv import re +import shutil from pathlib import Path + +def get_resource_path(relative_path): + try: + # PyInstaller creates a temp folder and stores path in _MEIPASS + base_path = sys._MEIPASS + except Exception: + base_path = os.path.abspath(".") + return os.path.join(base_path, relative_path) + + def run_tests(args): - behave_args = [args.feature, '--junit', '--junit-directory', args.junit_directory] + if not get_features(args): + sys.exit('No requirements could be found to check.') + behave_args = [get_resource_path('features')] if args.advanced_arguments: - behave_args = args.advanced_arguments.split() + behave_args.extend(args.advanced_arguments.split()) + else: + behave_args.extend(['--junit', '--junit-directory', args.junit_directory]) behave_main(behave_args) print('# All tests are finished.') + +def get_features(args): + has_features = False + if os.path.exists('features'): + shutil.copytree('features', get_resource_path('features')) + has_features = True + for f in os.listdir('.'): + if not f.endswith('.requirement'): + continue + if args.feature and args.feature != f: + continue + has_features = True + shutil.copyfile(f, os.path.join( + get_resource_path('features'), + os.path.basename(f)[0:-len('.requirement')] + '.feature')) + return has_features + + def generate_report(args): print('# Generating HTML reports now.') if not os.path.exists('report'): os.mkdir('report') if not os.path.exists(args.junit_directory): os.mkdir(args.junit_directory) - for file in os.listdir(args.junit_directory): - if not file.endswith('.xml'): + for f in os.listdir(args.junit_directory): + if not f.endswith('.xml'): continue - print(f'Processing {file} ...') - root = ET.parse('{}{}'.format(args.junit_directory, file)).getroot() + print(f'Processing {f} ...') + root = ET.parse('{}{}'.format(args.junit_directory, f)).getroot() data = { 'report_name': root.get('name'), 'testcases': [] @@ -39,13 +75,17 @@ def generate_report(args): steps = [] system_out = testcase.findall('system-out')[0].text.splitlines() for line in system_out: - if line.strip()[0:4] in ['Give', 'Then', 'When', 'And ']: + if line.strip()[0:4] in ['Give', 'Then', 'When', 'And '] \ + or line.strip()[0:2] == '* ': is_success = True if ' ... passed in ' in line else False + name, time = line.strip().split(' ... ') + if name[0:2] == '* ': + name = name[2:] steps.append({ - 'name': line.strip().split(' ... ')[0], - 'time': line.strip().split(' ... ')[1], + 'name': name, + 'time': time, 'is_success': is_success - }) + }) total_passes = len([s for s in steps if s['is_success'] == True]) total_steps = len(steps) pass_rate = round((total_passes / total_steps) * 100) @@ -58,8 +98,8 @@ def generate_report(args): 'total_steps': total_steps, 'pass_rate': pass_rate }) - with open('report/{}.html'.format(file[0:-4]), 'w') as out: - with open('features/template.html') as template: + with open('report/{}.html'.format(f[0:-4]), 'w') as out: + with open(get_resource_path('features/template.html')) as template: out.write(pystache.render(template.read(), data)) @@ -98,6 +138,7 @@ class TestPurger: except: return False + parser = argparse.ArgumentParser( description='Runs unit tests for BIM data') parser.add_argument( @@ -120,8 +161,8 @@ parser.add_argument( '-f', '--feature', type=str, - help='Specify a feature to test', - default='features') + help='Specify a requirements feature file to test', + default='') parser.add_argument( '-a', '--advanced-arguments', @@ -130,12 +171,6 @@ parser.add_argument( default='') args = parser.parse_args() -if not os.path.exists('features'): - sys.exit(''' - BIMTester requires a features folder to exist within the current folder. - Visit https://blenderbim.org/ to learn more about how to use BIMTester. - ''') - if args.purge: TestPurger().purge() elif args.report: diff --git a/src/ifcbimtester/features/steps/steps.py b/src/ifcbimtester/features/steps/steps.py index 5316f91285..f1604dc7bd 100644 --- a/src/ifcbimtester/features/steps/steps.py +++ b/src/ifcbimtester/features/steps/steps.py @@ -1,5 +1,5 @@ import ifcopenshell -from behave import given, when, then, step +from behave import step class IfcFile(object): file = None @@ -31,47 +31,47 @@ class IfcFile(object): return prop -@given('the IFC file "{file}"') +@step('The IFC file "{file}" must be provided') def step_impl(context, file): IfcFile.load(file) -@given('the IFC file "{file}" exists') +@step('The IFC file "{file}" is exempt from being provided') def step_impl(context, file): pass -@then('the file should be an {schema} file') +@step('IFC data must use the {schema} schema') def step_impl(context, schema): assert IfcFile.get().schema == schema -@then('the element {id} is an {ifc_class}') +@step('the element {id} is an {ifc_class}') def step_impl(context, id, ifc_class): assert IfcFile.get().by_id(id).is_a(ifc_class) -@then('the element {id} should not exist because {reason}') +@step('the element {id} should not exist because {reason}') def step_impl(context, id, reason): assert not IfcFile.get().by_id(id) -@then('the file is exempt from auditing because {reason}') +@step('No further requirements are specified because {reason}') def step_impl(context, reason): pass -@given(u'there is at least one {ifc_class} element') +@step(u'there is at least one {ifc_class} element') def step_impl(context, ifc_class): assert len(IfcFile.get().by_type(ifc_class)) >= 1 -@then(u'there are no {ifc_class} elements because {reason}') +@step(u'there are no {ifc_class} elements because {reason}') def step_impl(context, ifc_class, reason): assert len(IfcFile.get().by_type(ifc_class)) == 0 -@then('all {ifc_class} elements have a name matching the pattern "{pattern}"') +@step('all {ifc_class} elements have a name matching the pattern "{pattern}"') def step_impl(context, ifc_class, pattern): import re elements = IfcFile.get().by_type(ifc_class) @@ -80,7 +80,7 @@ def step_impl(context, ifc_class, pattern): assert False -@then('all {ifc_class} elements have an {representation_class} representation') +@step('all {ifc_class} elements have an {representation_class} representation') def step_impl(context, ifc_class, representation_class): def is_item_a_representation(item, representation): if '/' in representation: @@ -109,7 +109,7 @@ def step_impl(context, ifc_class, representation_class): assert False use_step_matcher('re') -@then('all (?P.*) elements have an? (?P.*) attribute') +@step('all (?P.*) elements have an? (?P.*) attribute') def step_impl(context, ifc_class, attribute): elements = IfcFile.get().by_type(ifc_class) for element in elements: @@ -117,7 +117,7 @@ def step_impl(context, ifc_class, attribute): assert False -@then('all (?P.*) elements have an? (?P.*\..*) property') +@step('all (?P.*) elements have an? (?P.*\..*) property') def step_impl(context, ifc_class, property_path): pset_name, property_name = property_path.split('.') elements = IfcFile.get().by_type(ifc_class) @@ -126,7 +126,7 @@ def step_impl(context, ifc_class, property_path): assert False -@then('all (?P.*) elements have an? (?P.*\..*) property value matching the pattern "(?P.*)"') +@step('all (?P.*) elements have an? (?P.*\..*) property value matching the pattern "(?P.*)"') def step_impl(context, ifc_class, property_path, pattern): import re pset_name, property_name = property_path.split('.') @@ -142,7 +142,7 @@ def step_impl(context, ifc_class, property_path, pattern): assert False -@then('all (?P.*) elements have an? (?P.*) matching the pattern "(?P.*)"') +@step('all (?P.*) elements have an? (?P.*) matching the pattern "(?P.*)"') def step_impl(context, ifc_class, attribute, pattern): import re elements = IfcFile.get().by_type(ifc_class) @@ -152,7 +152,7 @@ def step_impl(context, ifc_class, attribute, pattern): assert re.search(pattern, value) -@then('all (?P.*) elements have an? (?P.*) taken from the list in "(?P.*)"') +@step('all (?P.*) elements have an? (?P.*) taken from the list in "(?P.*)"') def step_impl(context, ifc_class, attributes, list_file): import csv values = [] @@ -172,7 +172,7 @@ def step_impl(context, ifc_class, attributes, list_file): use_step_matcher('parse') -@then('all {ifc_class} elements have a {qto_name}.{quantity_name} quantity') +@step('all {ifc_class} elements have a {qto_name}.{quantity_name} quantity') def step_impl(context, ifc_class, qto_name, quantity_name): elements = IfcFile.get().by_type(ifc_class) for element in elements: @@ -187,7 +187,7 @@ def step_impl(context, ifc_class, qto_name, quantity_name): if not is_successful: assert False -@then(u'the project should have geolocation data') +@step(u'the project should have geolocation data') def step_impl(context): if IfcFile.get().schema == 'IFC2X3': for site in IfcFile.get().by_type('IfcSite'): @@ -203,7 +203,7 @@ def step_impl(context): return assert False -@then(u'the project geolocation uses the "{crs_name}" CRS') +@step(u'the project geolocation uses the "{crs_name}" CRS') def step_impl(context, crs_name): if IfcFile.get().schema == 'IFC2X3': for site in IfcFile.get().by_type('IfcSite'): @@ -214,24 +214,24 @@ def step_impl(context, crs_name): use_step_matcher('re') -@then(u'the geolocated datum has an? (?P.*) of "(?P.*)"') +@step(u'the geolocated datum has an? (?P.*) of "(?P.*)"') def step_impl(context, attribute, value): if IfcFile.get().schema == 'IFC2X3': site = IfcFile.get().by_type('IfcSite')[0] actual_value = IfcFile.get_property(site, 'EPset_MapConversion', attribute).NominalValue.wrappedValue else: actual_value = getattr(IfcFile.get().by_id(IfcFile.bookmarks['geolocation']), attribute) - assert str(actual_value) == value, f'The value was {actual_value}' + assert str(actual_value) == value, f'The value was {actual_value}' use_step_matcher('parse') -@then(u'the project has a {attribute_name} attribute with a value of "{attribute_value}"') +@step(u'the project has a {attribute_name} attribute with a value of "{attribute_value}"') def step_impl(context, attribute_name, attribute_value): project = IfcFile.get().by_type('IfcProject')[0] assert getattr(project, attribute_name) == attribute_value -@then(u'there is an {ifc_class} element with a {attribute_name} attribute with a value of "{attribute_value}"') +@step(u'there is an {ifc_class} element with a {attribute_name} attribute with a value of "{attribute_value}"') def step_impl(context, ifc_class, attribute_name, attribute_value): elements = IfcFile.get().by_type(ifc_class) for element in elements: @@ -241,7 +241,7 @@ def step_impl(context, ifc_class, attribute_name, attribute_value): assert False -@then(u'all buildings have an address') +@step(u'all buildings have an address') def step_impl(context): for building in IfcFile.get().by_type('IfcBuilding'): if not building.BuildingAddress: