bimtester: move code into a package directory

This commit is contained in:
Bernd Hahnebach
2020-11-25 17:21:20 +01:00
committed by Dion Moult
parent acb518613b
commit 32f99428c3
19 changed files with 32 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
from os.path import dirname
from os.path import realpath
code_bimtester_path = dirname(realpath(__file__))
+47
View File
@@ -0,0 +1,47 @@
import ifcopenshell
import os
from pathlib import Path
class TestPurger:
def __init__(self):
self.file = None
def purge(self):
filenames = []
if os.path.exists("features"):
for filename in Path("features/").glob("*.feature"):
filenames.append(filename)
for f in os.listdir("."):
if f.endswith(".feature"):
filenames.append(f)
for filename in filenames:
with open(filename, "r") as feature_file:
old_file = feature_file.readlines()
with open(filename, "w") as new_file:
for line in old_file:
is_purged = False
if 'The IFC file "' in line and '" must be provided' in line:
filename = line.split('"')[1]
print("Loading file {} ...".format(filename))
self.file = ifcopenshell.open(filename)
if line.strip()[0:2] == "* ":
words = line.strip().split()
for word in words:
if self.is_a_global_id(word):
if not self.does_global_id_exist(word):
print("Test for {} purged ...".format(word))
is_purged = True
if not is_purged:
new_file.write(line)
def is_a_global_id(self, word):
return word[0] in ["0", "1", "2", "3"] and len(word) == 22
def does_global_id_exist(self, global_id):
try:
self.file.by_guid(global_id)
return True
except Exception:
return False
@@ -0,0 +1,7 @@
from behave.model import Scenario
def before_all(context):
userdata = context.config.userdata
continue_after_failed = True
Scenario.continue_after_failed_step = continue_after_failed
@@ -0,0 +1,70 @@
import json
from behave import step
from utils import IfcFile, assert_attribute, assert_type
def get_classification(name):
classifications = [c for c in IfcFile.get().by_type("IfcClassification") if c.Name == name]
if len(classifications) != 1:
assert False, f'The classification "{name}" was not found'
return classifications[0]
@step("The classification {name} must be used")
def step_impl(context, name):
get_classification(name)
@step("The classification {name} is published by {source}")
def step_impl(context, name, source):
assert_attribute(get_classification(name), "Source", source)
@step("The classification {name} is the edition {edition} on {edition_date}")
def step_impl(context, name, edition, edition_date):
element = get_classification(name)
assert_attribute(element, "Edition", edition)
assert_attribute(element, "EditionDate", edition_date)
@step('The classification {name} has the description "{description}"')
def step_impl(context, name, description):
assert_attribute(get_classification(name), "Description", description)
@step("The classification {name} is referenced by the website {location}")
def step_impl(context, name, location):
assert_attribute(get_classification(name), "Location", location)
@step("The classification {name} has a hierarchy denoted by the tokens {tokens}")
def step_impl(context, name, tokens):
try:
tokens = json.loads(tokens)
except:
assert False, f"Tokens {tokens} are not specified as a JSON list"
assert_attribute(get_classification(name), "ReferenceTokens", tokens)
@step('The element {guid} is classified as a "{identification}" with name "{reference_name}"')
def step_impl(context, guid, identification, reference_name):
element = IfcFile.by_guid(guid)
if not hasattr(element, "HasAssociations") or not element.HasAssociations:
assert False, f"The element {element} has no associations."
references = [a.RelatingClassification for a in element.HasAssociations if a.is_a("IfcRelAssociatesClassification")]
if not references:
assert False, f"The element {element} has no associated classification references."
is_success = False
for reference in references:
try:
assert_attribute(reference, "Identification", identification)
assert_attribute(reference, "Name", reference_name)
is_success = True
except:
pass
if not is_success:
assert (
False
), "No classification references met the requirement for an identification {} and name {} for the element {}. The references we found were: {}".format(
identification, reference_name, element, references
)
@@ -0,0 +1,38 @@
from behave import step
from utils import IfcFile, assert_attribute, assert_type
@step("The element {guid} is an {ifc_class} only")
def step_impl(context, guid, ifc_class):
element = IfcFile.by_guid(guid)
assert_type(element, ifc_class, is_exact=True)
@step("The element {guid} is an {ifc_class}")
def step_impl(context, guid, ifc_class):
element = IfcFile.by_guid(guid)
assert_type(element, ifc_class)
@step("The element {guid} is further defined as a {predefined_type}")
def step_impl(context, guid, predefined_type):
element = IfcFile.by_guid(guid)
if (
hasattr(element, "PredefinedType")
and element.PredefinedType == "USERDEFINED"
and hasattr(element, "ObjectType")
):
assert_attribute(element, "ObjectType", predefined_type)
elif hasattr(element, "PredefinedType"):
assert_attribute(element, "PredefinedType", predefined_type)
else:
assert False, "The element {} does not have a PredefinedType or ObjectType attribute".format(element)
@step("The element {guid} should not exist because {reason}")
def step_impl(context, guid, reason):
try:
element = IfcFile.get().by_id(guid)
except:
return
assert False, "This element {} should be reevaluated.".format(element)
@@ -0,0 +1,81 @@
from behave import step
from utils import IfcFile, assert_attribute, assert_type
def get_ifc_class_from_spatial_type(spatial_type):
if spatial_type == "site":
return "IfcSite"
elif spatial_type == "building":
return "IfcBuilding"
return "IfcFacility"
def check_geocode_attribute(guid, spatial_type, name, value):
element = IfcFile.by_guid(guid)
assert_type(element, get_ifc_class_from_spatial_type(spatial_type))
assert_attribute(element, name, value)
def check_geocode_address(guid, spatial_type, name, value):
element = IfcFile.by_guid(guid)
ifc_class = get_ifc_class_from_spatial_type(spatial_type)
assert_type(element, ifc_class)
if ifc_class == "IfcSite":
address_name = "SiteAddress"
elif ifc_class == "IfcBuilding":
address_name = "BuildingAddress"
assert_attribute(element, address_name)
assert_attribute(getattr(element, address_name), name, value)
use_step_matcher("re")
@step("The (site|building|facility) (?P<guid>.*) has a name of (?P<name>.*)")
def step_impl(context, spatial_type, guid, name):
check_geocode_attribute(guid, spatial_type, "Name", name)
@step('The (site|building|facility) (?P<guid>.*) has a description of "(?P<description>.*)"')
def step_impl(context, spatial_type, guid, description):
check_geocode_attribute(guid, spatial_type, "Description", description)
@step("The site (?P<guid>.*) has a land title number of (?P<land_title_number>.*)")
def step_impl(context, guid, land_title_number):
check_geocode_attribute(guid, "site", "LandTitleNumber", land_title_number)
@step('The (site|building) (?P<guid>.*) has the address "(?P<address_lines>.*)"')
def step_impl(context, spatial_type, guid, address_lines):
check_geocode_address(guid, spatial_type, "AddressLines", address_lines.split("\\n"))
@step("The (site|building) (?P<guid>.*) has a postal box of (?P<postal_box>.*)")
def step_impl(context, spatial_type, guid, postal_box):
check_geocode_address(guid, spatial_type, "PostalBox", postal_box)
@step("The (site|building) (?P<guid>.*) is in the town (?P<town>.*)")
def step_impl(context, spatial_type, guid, town):
check_geocode_address(guid, spatial_type, "Town", town)
@step("The (site|building) (?P<guid>.*) is in the region (?P<region>.*)")
def step_impl(context, spatial_type, guid, region):
check_geocode_address(guid, spatial_type, "Region", region)
@step("The (site|building) (?P<guid>.*) has a post code of (?P<post_code>.*)")
def step_impl(context, spatial_type, guid, post_code):
check_geocode_address(guid, spatial_type, "PostalCode", post_code)
@step("The (site|building) (?P<guid>.*) is in the country (?P<country>.*)")
def step_impl(context, spatial_type, guid, country):
check_geocode_address(guid, spatial_type, "Country", country)
@step('The (site|building) (?P<guid>.*) has an address description of "(?P<description>.*)"')
def step_impl(context, spatial_type, guid, description):
check_geocode_address(guid, spatial_type, "Description", description)
@@ -0,0 +1,215 @@
from behave import step
from utils import IfcFile, assert_number, assert_pset, assert_attribute
import math
import ifcopenshell.util
import ifcopenshell.util.element
import ifcopenshell.util.geolocation
@step(u"There must be at least one {ifc_class} element")
def step_impl(context, ifc_class):
assert len(IfcFile.get().by_type(ifc_class)) >= 1, "An element of {} could not be found".format(ifc_class)
def check_ifc4_geolocation(entity_name, prop_name=None, value=None, should_assert=True):
if entity_name not in IfcFile.bookmarks:
has_entity = False
project = IfcFile.get().by_type("IfcProject")[0]
for context in project.RepresentationContexts:
if entity_name == "IfcMapConversion":
if (
context.is_a("IfcGeometricRepresentationContext")
and context.ContextType == "Model"
and context.HasCoordinateOperation
):
IfcFile.bookmarks[entity_name] = context.HasCoordinateOperation[0]
has_entity = True
elif entity_name == "IfcProjectedCRS":
if (
context.is_a("IfcGeometricRepresentationContext")
and context.ContextType == "Model"
and context.HasCoordinateOperation
and context.HasCoordinateOperation[0].TargetCRS
):
IfcFile.bookmarks[entity_name] = context.HasCoordinateOperation[0].TargetCRS
has_entity = True
if not has_entity:
assert False, "No model geometric representation contexts refer to an {}".format(entity_name)
if not prop_name:
return
actual_value = getattr(IfcFile.bookmarks[entity_name], prop_name)
if should_assert:
assert actual_value == value, 'We expected a value of "{}" but instead got "{}"'.format(value, actual_value)
else:
return actual_value
@step(u"The project must have coordinate reference system data")
def step_impl(context):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS")
return
check_ifc4_geolocation("IfcProjectedCRS")
@step(u"The name of the CRS must be {coordinate_reference_name}")
def step_impl(context, coordinate_reference_name):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "Name", coordinate_reference_name)
return
check_ifc4_geolocation("IfcProjectedCRS", "Name", coordinate_reference_name)
@step(u"The description of the CRS must be {value}")
def step_impl(context, value):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "Description", value)
return
check_ifc4_geolocation("IfcProjectedCRS", "Description", value)
@step(u"The geodetic datum must be {coordinate_reference_name}")
def step_impl(context, coordinate_reference_name):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "GeodeticDatum", coordinate_reference_name)
return
check_ifc4_geolocation("IfcProjectedCRS", "GeodeticDatum", coordinate_reference_name)
@step(u"The vertical datum must be {coordinate_reference_name}")
def step_impl(context, coordinate_reference_name):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "VerticalDatum", coordinate_reference_name)
return
check_ifc4_geolocation("IfcProjectedCRS", "VerticalDatum", coordinate_reference_name)
@step(u"The map projection must be {coordinate_reference_name}")
def step_impl(context, coordinate_reference_name):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "MapProjection", coordinate_reference_name)
return
check_ifc4_geolocation("IfcProjectedCRS", "MapProjection", coordinate_reference_name)
@step(u"The map zone must be {coordinate_reference_name}")
def step_impl(context, coordinate_reference_name):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "MapZone", coordinate_reference_name)
return
check_ifc4_geolocation("IfcProjectedCRS", "MapZone", coordinate_reference_name)
@step(u"The map unit must be {unit}")
def step_impl(context, unit):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_ProjectedCRS", "MapUnit", unit)
return
actual_value = check_ifc4_geolocation("IfcProjectedCRS", "MapUnit", should_assert=False)
if not actual_value:
assert False, "A unit was not provided in the projected CRS"
if actual_value.is_a("IfcSIUnit"):
prefix = actual_value.Prefix if actual_value.Prefix else ""
actual_value = prefix + actual_value.Name
elif actual_value.is_a("IfcConversionBasedUnit"):
actual_value = actual_value.Name
assert actual_value == unit, 'We expected a value of "{}" but instead got "{}"'.format(unit, actual_value)
@step(u"The project must have coordinate transformations to convert from local to global coordinates")
def step_impl(context):
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_MapConversion")
check_ifc4_geolocation("IfcMapConversion")
@step(u"The eastings of the model must be offset by {number} to derive its global coordinates")
def step_impl(context, number):
number = assert_number(number)
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_MapConversion", "Eastings", number)
return
check_ifc4_geolocation("IfcMapConversion", "Eastings", number)
@step(u"The northings of the model must be offset by {number} to derive its global coordinates")
def step_impl(context, number):
number = assert_number(number)
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_MapConversion", "Northings", number)
return
check_ifc4_geolocation("IfcMapConversion", "Northings", number)
@step(u"The height of the model must be offset by {number} to derive its global coordinates")
def step_impl(context, number):
number = assert_number(number)
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_MapConversion", "OrthogonalHeight", number)
return
check_ifc4_geolocation("IfcMapConversion", "OrthogonalHeight", number)
@step(u"The model must be rotated clockwise by {number} to derive its global coordinates")
def step_impl(context, number):
number = assert_number(number)
if IfcFile.get().schema == "IFC2X3":
return check_ifc2x3_geolocation("EPset_MapConversion", "Height", number)
abscissa = check_ifc4_geolocation("IfcMapConversion", "XAxisAbscissa", should_assert=False)
ordinate = check_ifc4_geolocation("IfcMapConversion", "XAxisOrdinate", should_assert=False)
actual_value = round(ifcopenshell.util.geolocation.xy2angle(abscissa, ordinate), 3)
value = round(number, 3)
assert actual_value == value, 'We expected a value of "{}" but instead got "{}"'.format(value, actual_value)
@step(u"The model must be scaled along the horizontal axis by {number} to derive its global coordinates")
def step_impl(context, number):
number = assert_number(number)
if IfcFile.get().schema == "IFC2X3":
for site in IfcFile.get().by_type("IfcSite"):
assert_pset(site, "EPset_MapConversion", "Scale", number)
return
check_ifc4_geolocation("IfcMapConversion", "Scale", number)
@step(u"The site {guid} has a longitude of {number}")
def step_impl(context, guid, number):
number = assert_number(number)
site = IfcFile.by_guid(guid)
if not site.is_a("IfcSite"):
assert False, "The element {} is not an IfcSite".format(site)
ref = assert_attribute(site, "RefLongitude")
number = ifcopenshell.util.geolocation.dd2dms(number, use_ms=(len(ref) == 4))
assert_attribute(site, "RefLongitude", number)
@step(u"The site {guid} has a latitude of {number}")
def step_impl(context, guid, number):
number = assert_number(number)
site = IfcFile.by_guid(guid)
if not site.is_a("IfcSite"):
assert False, "The element {} is not an IfcSite".format(site)
ref = assert_attribute(site, "RefLatitude")
number = ifcopenshell.util.geolocation.dd2dms(number, use_ms=(len(ref) == 4))
assert_attribute(site, "RefLatitude", number)
@step(u"The site {guid} has an elevation of {number}")
def step_impl(context, guid, number):
number = assert_number(number)
site = IfcFile.by_guid(guid)
if not site.is_a("IfcSite"):
assert False, "The element {} is not an IfcSite".format(site)
assert_attribute(site, "RefElevation", number)
@@ -0,0 +1,28 @@
from behave import step
from utils import IfcFile
from utils import IfcFile, assert_attribute
@step("All elements must be under {number} polygons")
def step_impl(context, number):
number = int(number)
errors = []
for element in IfcFile.get().by_type("IfcElement"):
if not element.Representation:
continue
total_polygons = 0
tree = IfcFile.get().traverse(element.Representation)
for e in tree:
if e.is_a("IfcFace"):
total_polygons += 1
elif e.is_a("IfcPolygonalFaceSet"):
total_polygons += len(e.Faces)
elif e.is_a("IfcTriangulatedFaceSet"):
total_polygons += len(e.CoordIndex)
if total_polygons > number:
errors.append((total_polygons, element))
if errors:
message = "The following {} elements are over 500 polygons:\n".format(len(errors))
for error in errors:
message += "Polygons: {} - {}\n".format(error[0], error[1])
assert False, message
@@ -0,0 +1,119 @@
import numpy as np
import ifcopenshell.util.geolocation
from behave import step
from utils import IfcFile
from utils import IfcFile, assert_number, assert_type
def a2p(o, z, x):
y = np.cross(z, x)
r = np.eye(4)
r[:-1, :-1] = x, y, z
r[-1, :-1] = o
return r.T
def get_axis2placement(plc):
z = np.array(plc.Axis.DirectionRatios if plc.Axis else (0, 0, 1))
x = np.array(plc.RefDirection.DirectionRatios if plc.RefDirection else (1, 0, 0))
o = plc.Location.Coordinates
return a2p(o, z, x)
def get_local_placement(plc):
if plc is None:
return np.eye(4)
if plc.PlacementRelTo is None:
parent = np.eye(4)
else:
parent = get_local_placement(plc.PlacementRelTo)
return np.dot(get_axis2placement(plc.RelativePlacement), parent)
def get_decimal_points(value):
try:
return len(value.split(".")[1])
except:
return 0
def get_containing_spatial_elements(element):
results = []
if element.is_a("IfcSpatialElement"):
results.append(element)
for rel in element.Decomposes:
if rel.is_a("IfcRelAggregates"):
results.append(get_containing_spatial_elements(rel.RelatingObject))
elif element.is_a("IfcElement"):
for rel in element.ContainedInStructure:
if rel.is_a("ifcRelContainedInSpatialStructure"):
results.append(get_containing_spatial_elements(rel.RelatingStructure))
return results
@step("There is a datum element {guid} as an {ifc_class}")
def step_impl(context, guid, ifc_class):
element = IfcFile.by_guid(guid)
assert_type(element, ifc_class)
@step(
"The element {guid} has a global easting, northing, and elevation of {easting}, {northing}, and {elevation} respectively"
)
def step_impl(context, guid, easting, northing, elevation):
if IfcFile.get().schema == "IFC2X3":
if element.is_a("IfcSite"):
site = element
else:
potential_sites = [s for s in get_containing_spatial_elements(element) if s.is_a("IfcSite")]
if potential_sites:
site = potential_sites[0]
else:
assert False, "The datum element does not belong to a geolocated site"
map_conversion = assert_pset(site, "EPset_MapConversion")
else:
map_conversion = IfcFile.get().by_type("IfcMapConversion")
if map_conversion:
map_conversion = map_conversion[0].get_info()
else:
assert False, "No map conversion was found in the file"
element = IfcFile.by_guid(guid)
if not element.ObjectPlacement:
assert False, "The element does not have an object placement: {}".format(element)
m = get_local_placement(element.ObjectPlacement)
e, n, h = ifcopenshell.util.geolocation.xyz2enh(
m[0][3],
m[1][3],
m[2][3],
float(map_conversion["Eastings"]),
float(map_conversion["Northings"]),
float(map_conversion["OrthogonalHeight"]),
float(map_conversion["XAxisAbscissa"]),
float(map_conversion["XAxisOrdinate"]),
float(map_conversion["Scale"]),
)
element_x = round(e, get_decimal_points(easting))
element_y = round(n, get_decimal_points(northing))
element_z = round(h, get_decimal_points(elevation))
expected_placement = (assert_number(easting), assert_number(northing), assert_number(elevation))
if (element_x, element_y, element_z) != expected_placement:
assert False, "The element {} is meant to have a location of {} but instead we found {}".format(
element, expected_placement, (element_x, element_y, element_z)
)
@step("The element {guid} has a local X, Y, and Z coordinate of {x}, {y}, and {z} respectively")
def step_impl(context, guid, x, y, z):
element = IfcFile.by_guid(guid)
if not element.ObjectPlacement:
assert False, "The element does not have an object placement: {}".format(element)
m = get_local_placement(element.ObjectPlacement)
element_x = round(m[0][3], get_decimal_points(x))
element_y = round(m[1][3], get_decimal_points(y))
element_z = round(m[2][3], get_decimal_points(z))
expected_placement = (assert_number(x), assert_number(y), assert_number(z))
if (element_x, element_y, element_z) != expected_placement:
assert False, "The element {} is meant to have a location of {} but instead we found {}".format(
element, expected_placement, (element_x, element_y, element_z)
)
@@ -0,0 +1,93 @@
from behave import step
from utils import IfcFile
from utils import IfcFile, assert_attribute
@step('The IFC file "{file}" must be provided')
def step_impl(context, file):
try:
IfcFile.load(file)
except:
assert False, f"The file {file} could not be loaded"
@step("IFC data must use the {schema} schema")
def step_impl(context, schema):
assert IfcFile.get().schema == schema, "We expected a schema of {} but instead got {}".format(
schema, IfcFile.get().schema
)
@step('The IFC file "{file}" is exempt from being provided')
def step_impl(context, file):
pass
@step("No further requirements are specified because {reason}")
def step_impl(context, reason):
pass
@step("The project must have an identifier of {guid}")
def step_impl(context, guid):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "GlobalId", guid)
@step('The project name, code, or short identifier must be "{value}"')
def step_impl(context, value):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "Name", value)
@step('The project must have a longer form name of "{value}"')
def step_impl(context, value):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "LongName", value)
@step('The project must be described as "{value}"')
def step_impl(context, value):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "Description", value)
@step('The project must be categorised under "{value}"')
def step_impl(context, value):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "ObjectType", value)
@step('The project must contain information about the "{value}" phase')
def step_impl(context, value):
assert_attribute(IfcFile.get().by_type("IfcProject")[0], "Phase", value)
@step("The project must contain 3D geometry representing the shape of objects")
def step_impl(context):
assert get_subcontext("Body", "Model", "MODEL_VIEW")
@step("The project must contain 3D geometry representing clearance zones")
def step_impl(context):
assert get_subcontext("Clearance", "Model", "MODEL_VIEW")
@step("The project must contain 3D geometry representing the center of gravity of objects")
def step_impl(context):
assert get_subcontext("CoG", "Model", "MODEL_VIEW")
@step("The project must contain 3D geometry representing the object bounding boxes")
def step_impl(context):
assert get_subcontext("Box", "Model", "MODEL_VIEW")
def get_subcontext(identifier, type, target_view):
project = IfcFile.get().by_type("IfcProject")[0]
for rep_context in project.RepresentationContexts:
for subcontext in rep_context.HasSubContexts:
if (
subcontext.ContextIdentifier == identifier
and subcontext.ContextType == type
and subcontext.TargetView == target_view
):
return True
assert False, "The subcontext with identifier {}, type {}, and target view {} could not be found".format(
identifier, type, target_view
)
@@ -0,0 +1,159 @@
from behave import step
from utils import IfcFile
@step("there are no {ifc_class} elements because {reason}")
def step_impl(context, ifc_class, reason):
assert len(IfcFile.get().by_type(ifc_class)) == 0
@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)
for element in elements:
if not re.search(pattern, element.Name):
assert False
@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:
for cls in representation.split("/"):
if item.is_a(cls):
return True
elif item.is_a(representation):
return True
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
if not element.Representation:
continue
has_representation = False
for representation in element.Representation.Representations:
for item in representation.Items:
if item.is_a("IfcMappedItem"):
# We only check one more level deep.
for item2 in item.MappingSource.MappedRepresentation.Items:
if is_item_a_representation(item2, representation_class):
has_representation = True
else:
if is_item_a_representation(item, representation_class):
has_representation = True
if not has_representation:
assert False
use_step_matcher("re")
@step("all (?P<ifc_class>.*) elements have an? (?P<attribute>.*) attribute")
def step_impl(context, ifc_class, attribute):
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
if not getattr(element, attribute):
assert False
@step("all (?P<ifc_class>.*) elements have an? (?P<property_path>.*\..*) property")
def step_impl(context, ifc_class, property_path):
pset_name, property_name = property_path.split(".")
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
if not IfcFile.get_property(element, pset_name, property_name):
assert False
@step(
'all (?P<ifc_class>.*) elements have an? (?P<property_path>.*\..*) property value matching the pattern "(?P<pattern>.*)"'
)
def step_impl(context, ifc_class, property_path, pattern):
import re
pset_name, property_name = property_path.split(".")
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
prop = IfcFile.get_property(element, pset_name, property_name)
if not prop:
assert False
# For now, we only check single values
if prop.is_a("IfcPropertySingleValue"):
if not (prop.NominalValue and re.search(pattern, prop.NominalValue.wrappedValue)):
assert False
@step('all (?P<ifc_class>.*) elements have an? (?P<attribute>.*) matching the pattern "(?P<pattern>.*)"')
def step_impl(context, ifc_class, attribute, pattern):
import re
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
value = getattr(element, attribute)
print(f'Checking value "{value}" for {element}')
assert re.search(pattern, value)
@step('all (?P<ifc_class>.*) elements have an? (?P<attributes>.*) taken from the list in "(?P<list_file>.*)"')
def step_impl(context, ifc_class, attributes, list_file):
import csv
values = []
with open(list_file) as csvfile:
reader = csv.reader(csvfile)
for row in reader:
values.append(row)
elements = IfcFile.get().by_type(ifc_class)
for element in elements:
attribute_values = []
for attribute in attributes.split(","):
if not hasattr(element, attribute):
assert False, f"Failed at element {element.GlobalId}"
attribute_values.append(getattr(element, attribute))
if attribute_values not in values:
assert False, f"Failed at element {element.GlobalId}"
use_step_matcher("parse")
@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:
is_successful = False
if not element.IsDefinedBy:
assert False
for relationship in element.IsDefinedBy:
if relationship.RelatingPropertyDefinition.Name == qto_name:
for quantity in relationship.RelatingPropertyDefinition.Quantities:
if quantity.Name == quantity_name:
is_successful = True
if not is_successful:
assert False
use_step_matcher("parse")
@step('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
@step('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:
if hasattr(element, attribute_name) and getattr(element, attribute_name) == attribute_value:
return
assert False
@step("all buildings have an address")
def step_impl(context):
for building in IfcFile.get().by_type("IfcBuilding"):
if not building.BuildingAddress:
assert False, f'The building "{building.Name}" has no address.'
@@ -0,0 +1,80 @@
import ifcopenshell
import ifcopenshell.util
import ifcopenshell.util.element
class IfcFile(object):
file = None
bookmarks = {}
@classmethod
def load(cls, path=None):
cls.file = ifcopenshell.open(path)
@classmethod
def get(cls):
if not cls.file:
assert False, "No file was loaded, so this requirement cannot be checked"
return cls.file
@classmethod
def by_guid(cls, guid):
try:
return cls.get().by_guid(guid)
except:
assert False, "An element with the ID {} could not be found.".format(guid)
def assert_number(number):
try:
return float(number)
except ValueError:
assert False, "A number should be specified, not {}".format(number)
def assert_type(element, ifc_class, is_exact=False):
if is_exact:
assert element.is_a() == ifc_class, "The element {} is an {} instead of {}.".format(
element, element.is_a(), ifc_class
)
else:
assert element.is_a(ifc_class), "The element {} is an {} instead of {}.".format(
element, element.is_a(), ifc_class
)
def assert_attribute(element, name, value=None):
if not hasattr(element, name):
assert False, "The element {} does not have the attribute {}".format(element, name)
if not value:
if getattr(element, name) is None:
assert False, "The element {} does not have a value for the attribute {}".format(element, name)
return getattr(element, name)
if value == "NULL":
value = None
actual_value = getattr(element, name)
if isinstance(value, list) and actual_value:
actual_value = list(actual_value)
assert actual_value == value, 'We expected a value of "{}" but instead got "{}" for the element {}'.format(
value, actual_value, element
)
def assert_pset(element, pset_name, prop_name=None, value=None):
if value == "NULL":
value = None
psets = ifcopenshell.util.element.get_psets(site)
if pset_name not in psets:
assert False, "The element {} does not have a property set named {}".format(element, pset_name)
if prop_name is None:
return psets[pset_name]
if prop_name not in psets[pset_name]:
assert False, 'The element {} does not have a property named "{}" in the pset "{}"'.format(
element, prop_name, pset_name
)
if value is None:
return psets[pset_name][prop_name]
actual_value = psets[pset_name][prop_name]
assert actual_value == value, 'We expected a value of "{}" but instead got "{}" for the element {}'.format(
value, actual_value, element
)
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="foobaro">
<title>BlenderBIM</title>
<link href="https://fonts.googleapis.com/css?family=Comfortaa|Inconsolata|Open+Sans&display=swap" rel="stylesheet">
<style>
body { font-family: 'Arial', sans-serif; padding: 40px; }
span.time { color: #999; font-style: italic; float: right; }
span.step-time { float: right; color: #555; font-size: 0.8em; font-style: italic; }
span.success { background-color: #97cc64; padding: 5px; border-radius: 5px; color: #FFF; font-weight: bold; }
span.failure { background-color: #fb5a3e; padding: 5px; border-radius: 5px; color: #FFF; font-weight: bold; }
p.failure { background-color: #fb5a3e; padding: 5px; border-radius: 5px; color: #fff; }
p.unspecified { background-color: #994f00; padding: 5px; border-radius: 5px; color: #fff; }
p.description { background-color: #eee; border-radius: 5px; padding: 20px; margin-left: auto; margin-right: auto; display: inline-block; font-weight: bold;}
li { padding: 10px; font-family: monospace; }
li.success { background-color: #b6cca1; color: #333; }
li.failure { background-color: #fbb4a8; color: #900; }
li.unspecified { background-color: #ffd37f; color: #a30; }
li p { margin-bottom: 0px; }
footer { color: #999; font-size: 0.8em; }
header { text-align: center; }
hr { margin: 20px; margin-left: 0px; margin-right: 0px; border: none; border-top: 1px solid #ccc; }
</style>
</head>
<body>
<header>
<h1>{{name}}</h1>
<p><strong>{{time}} {{file_name}}</strong></p>
<hr>
<span class="{{#is_success}}success{{/is_success}}{{^is_success}}failure{{/is_success}}">{{#is_success}}Success{{/is_success}}{{^is_success}}Failure{{/is_success}}</span>
Tests passed: <strong>{{total_passes}} / {{total_steps}}</strong> ({{pass_rate}}%)
<br />
<p class="description">
{{#description}}
{{.}}<br />
{{/description}}
</p>
<hr>
</header>
{{#scenarios}}
<section>
<h2>{{name}}</h2>
<p>
<span class="{{#is_success}}success{{/is_success}}{{^is_success}}failure{{/is_success}}">{{#is_success}}Success{{/is_success}}{{^is_success}}Failure{{/is_success}}</span>
Tests passed: <strong>{{total_passes}} / {{total_steps}}</strong> ({{pass_rate}}%)
<span class="time">
Duration: {{time}}s
</span>
</p>
<ol>
{{#steps}}
<li class="{{#is_success}}success{{/is_success}}{{^is_success}}failure{{/is_success}}{{#is_unspecified}} unspecified{{/is_unspecified}}">
{{{name}}}
<span class="step-time">{{time}}s</span>
{{^is_success}}
<p class="failure{{#is_unspecified}} unspecified{{/is_unspecified}}">
{{#error_message}}
{{.}}<br />
{{/error_message}}
</p>
{{/is_success}}
</li>
{{/steps}}
</ol>
</section>
{{/scenarios}}
<hr>
<footer>
<p>
OpenBIM auditing is a feature of <a href="https://blenderbim.org/">BlenderBIM</a> and <a href="http://ifcopenshell.org/">IfcOpenShell</a>.
</p>
</footer>
</body>
</html>
+214
View File
@@ -0,0 +1,214 @@
# TODO: improve layout, start with feature file path and beside button !!!!!
# TODO: if browse widgets will be canceled, last QLineEdit should be restored
import os
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
from .run import run_all
class GuiWidgetBimTester(QtWidgets.QWidget):
# get some initial values
this_path = os.path.join(os.path.dirname(__file__))
desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
initial_ifcfile = "/home/hugo/Documents/zeug_sort/z_some_ifc/example_model.ifc"
if not os.path.isfile(initial_ifcfile):
initial_ifcfile = desktop_path
initial_featurespath = os.path.join(this_path, "..", "..", "features_bimtester", "fea_min")
if not os.path.isdir(initial_featurespath):
initial_featurespath = desktop_path
def __init__(self):
super(GuiWidgetBimTester, self).__init__()
self._setup_ui()
def __del__(self,):
# need as fix for qt event error
# http://forum.freecadweb.org/viewtopic.php?f=18&t=10732&start=10#p86493
return
def _setup_ui(self):
# a lot code is taken from FreeCAD FEM solver frame work task panel
# https://forum.freecadweb.org/viewtopic.php?f=10&t=51419
# use a browse button, and a line edit
# the browse button opens a file dialog, which will set the line edit
# icon
# print(__file__)
package_path = os.path.dirname(os.path.realpath(__file__))
iconpath = os.path.join(
package_path, "resources", "icons", "bimtester.ico"
)
"""
# as svg
# https://stackoverflow.com/a/35138314
theicon = QtSvg.QSvgWidget(iconpath)
# none works ...
#theicon.setGeometry(20,20,200,200)
#theicon.setSizePolicy(QtGui.QSizePolicy.Policy.Maximum, QtGui.QSizePolicy.Policy.Maximum)
#theicon.sizeHint()
"""
# as pixmap
theicon = QtWidgets.QLabel(self)
iconpixmap = QtGui.QPixmap(iconpath)
iconpixmap = iconpixmap.scaled(100, 100, QtCore.Qt.KeepAspectRatio)
theicon.setPixmap(iconpixmap)
# ifc file
_ifcfile_label = QtWidgets.QLabel("IFC file", self)
self.ifcfile_text = QtWidgets.QLineEdit()
self.set_ifcfile(self.initial_ifcfile)
_ifcfile_browse_btn = QtWidgets.QToolButton()
_ifcfile_browse_btn.setText("...")
_ifcfile_browse_btn.clicked.connect(self.select_ifcfile)
# feature files path
# use a layout with a frame and a title, see solver framework tp
# beside button
ffifc_str = (
"Feature files beside IFC file. "
"Feature files in directory features."
)
featuredirfromifc_label = QtWidgets.QLabel(ffifc_str, self)
self.featuredirfromifc_cb = QtWidgets.QCheckBox(self)
self.featuredirfromifc_cb.stateChanged.connect(
self.featuredirfromifc_clicked
)
# path browser and line edit
_ffdir_str = (
"Feature files directory. "
"Feature files in directory features."
)
_featurefilesdir_label = QtWidgets.QLabel(_ffdir_str, self)
self.featurefilesdir_text = QtWidgets.QLineEdit()
self.set_featurefilesdir(self.initial_featurespath)
self.feafilesdir_browse_btn = QtWidgets.QToolButton()
self.feafilesdir_browse_btn.setText("...")
self.feafilesdir_browse_btn.clicked.connect(
self.select_featurefilesdir
)
# buttons
self.run_button = QtWidgets.QPushButton(
QtGui.QIcon.fromTheme("document-new"), "Run"
)
self.close_button = QtWidgets.QPushButton(
QtGui.QIcon.fromTheme("window-close"), "Close"
)
self.run_button.clicked.connect(self.run_bimtester)
self.close_button.clicked.connect(self.close_widget)
_buttons = QtWidgets.QHBoxLayout()
_buttons.addWidget(self.run_button)
_buttons.addWidget(self.close_button)
# Layout:
layout = QtWidgets.QGridLayout()
layout.addWidget(theicon, 1, 0, alignment=QtCore.Qt.AlignRight)
layout.addWidget(_ifcfile_label, 2, 0)
layout.addWidget(self.ifcfile_text, 3, 0)
layout.addWidget(_ifcfile_browse_btn, 3, 1)
layout.addWidget(_featurefilesdir_label, 4, 0)
layout.addWidget(self.featurefilesdir_text, 5, 0)
layout.addWidget(self.feafilesdir_browse_btn, 5, 1)
layout.addWidget(featuredirfromifc_label, 6, 0)
layout.addWidget(self.featuredirfromifc_cb, 6, 1)
layout.addLayout(_buttons, 7, 0)
# row stretches by 10 compared to the others, std is 0
# first parameter is the row number
# second is the stretch factor.
layout.setRowStretch(0, 10)
self.setLayout(layout)
# **********************************************************
def select_ifcfile(self):
# print(self.get_ifcfile())
# print(os.path.isfile(self.get_ifcfile()))
ifcfile = QtWidgets.QFileDialog.getOpenFileName(
self,
dir=self.get_ifcfile()
)[0]
self.set_ifcfile(ifcfile)
def set_ifcfile(self, a_file):
self.ifcfile_text.setText(a_file)
def get_ifcfile(self):
return self.ifcfile_text.text()
def featuredirfromifc_clicked(self):
if self.featuredirfromifc_cb.isChecked() is True:
self.set_featurefilesdir("")
# TODO
self.featurefilesdir_text.setEnabled(False)
self.feafilesdir_browse_btn.setEnabled(False)
# deactivate feature path browser button
# deactivate lineedit text
else:
self.set_featurefilesdir(self.initial_featurespath)
self.featurefilesdir_text.setEnabled(True)
self.feafilesdir_browse_btn.setEnabled(True)
def select_featurefilesdir(self):
thedir = self.featurefilesdir_text.text()
# print(thedir)
# print(os.path.isdir(thedir))
# hidden directories are only shown if the option is set
features_path = QtWidgets.QFileDialog.getExistingDirectory(
self,
caption="Choose features directory ...",
dir=thedir,
options=QtWidgets.QFileDialog.HideNameFilterDetails
)
self.set_featurefilesdir(features_path)
def set_featurefilesdir(self, a_directory):
self.featurefilesdir_text.setText(a_directory)
def get_featurefilesdir(self):
return self.featurefilesdir_text.text()
# **********************************************************
def run_bimtester(self):
print("Run BIMTester")
QtWidgets.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor)
# get input values
splitifcpath = os.path.split(self.get_ifcfile())
the_ifcfile_path, the_ifcfile_name = splitifcpath[0], splitifcpath[1]
if self.featuredirfromifc_cb.isChecked() is True:
the_features_path = the_ifcfile_path
print(
"Make sure the feature files are beside "
"the ifc file in a directory named 'features'."
)
else:
the_features_path = self.get_featurefilesdir()
print(the_features_path)
print(the_ifcfile_path)
print(the_ifcfile_name)
# run bimtester
status = run_all(
the_features_path,
the_ifcfile_path,
the_ifcfile_name
)
print(status)
QtWidgets.QApplication.restoreOverrideCursor()
def close_widget(self):
print("Close BIMTester Gui")
self.close()
def closeEvent(self, ev):
pw = self.parentWidget()
if pw and pw.inherits("QDockWidget"):
pw.deleteLater()
+99
View File
@@ -0,0 +1,99 @@
import datetime
import json
import os
import pystache
def generate_report(adir="."):
print("# Generating HTML reports now.")
# get html template
html_template_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"features/template.html"
)
# get report file
report_dir = os.path.join(adir, "report")
if not os.path.exists(report_dir):
return print("No report directory was found.")
report_path = os.path.join(report_dir, "report.json")
# print(report_path)
if not os.path.exists(report_path):
return print("No report data was found.")
# read json report and create html report for each feature
report = json.loads(open(report_path).read())
for feature in report:
file_name = os.path.basename(feature["location"]).split(":")[0]
data = {
"file_name": file_name,
"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"name": feature["name"],
"description": feature["description"],
"is_success": feature["status"] == "passed",
"scenarios": [],
}
if "elements" not in feature:
if "status" in feature and feature["status"] == "skipped":
print("Feature was skipped. No html report will be created.")
else:
print("For a unknown reason no html report well be created.")
# happens if the feature file does not consist of any valid Scenario
continue
for scenario in feature["elements"]:
steps = []
total_duration = 0
if len(scenario["steps"]) == 0:
print(
"Scenario '{}' in feature '{}' has no steps. "
"Thus skipped in report."
.format(scenario["name"], feature["name"])
)
continue
for step in scenario["steps"]:
if "result" in step:
total_duration += step["result"]["duration"]
name = step["name"]
if "match" in step and "arguments" in step["match"]:
for a in step["match"]["arguments"]:
name = name.replace(a["value"], "<b>" + a["value"] + "</b>")
if "result" not in step or step["result"]["status"] == "undefined":
step["result"] = {}
step["result"]["status"] = "undefined"
step["result"]["duration"] = 0
step["result"]["error_message"] = "This requirement has not yet been specified."
steps.append(
{
"name": name,
"time": round(step["result"]["duration"], 2),
"is_success": step["result"]["status"] == "passed",
"is_unspecified": "result" not in step or step["result"]["status"] == "undefined",
"error_message": None
if step["result"]["status"] == "passed"
else step["result"]["error_message"],
}
)
total_passes = len([s for s in steps if s["is_success"] is True])
total_steps = len(steps)
pass_rate = round((total_passes / total_steps) * 100)
data["scenarios"].append(
{
"name": scenario["name"],
# on behave < 1.2.6 there is no 'status' thus report fails
"is_success": scenario["status"] == "passed",
"time": round(total_duration, 2),
"steps": steps,
"total_passes": total_passes,
"total_steps": total_steps,
"pass_rate": pass_rate,
}
)
data["total_passes"] = sum([s["total_passes"] for s in data["scenarios"]])
data["total_steps"] = sum([s["total_steps"] for s in data["scenarios"]])
data["pass_rate"] = round((data["total_passes"] / data["total_steps"]) * 100)
html_report_file = os.path.join(report_dir, "{}.html".format(file_name))
with open(html_report_file, "w") as out:
with open(html_template_file) as template:
out.write(pystache.render(template.read(), data))
Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

+295
View File
@@ -0,0 +1,295 @@
import behave.formatter.pretty # Needed for pyinstaller to package it
import fileinput
import os
import shutil
import sys
import tempfile
import webbrowser
from behave.__main__ import main as behave_main
# get bimtester source code module path
bimtester_path = os.path.dirname(os.path.realpath(__file__))
# print(bimtester_path)
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(os.path.realpath(__file__))
def get_resource_path(relative_path):
return os.path.join(base_path, relative_path)
def run_tests(args):
if not get_features(args):
print("No features could be found to check.")
return False
behave_args = [get_resource_path("features")]
if args["advanced_arguments"]:
behave_args.extend(args["advanced_arguments"].split())
elif not args["console"]:
behave_args.extend([
"--format",
"json.pretty",
"--outfile",
"report/report.json"
])
behave_main(behave_args)
print("# All tests are finished.")
return True
def get_features(args):
# current_path = os.path.abspath(".")
features_dir = get_resource_path("features")
for f in os.listdir(features_dir):
if f.endswith(".feature"):
os.remove(os.path.join(features_dir, f))
if args["feature"]:
shutil.copyfile(
args["feature"],
os.path.join(
get_resource_path("features"),
os.path.basename(args["feature"])
)
)
return True
if os.path.exists("features"):
shutil.copytree("features", get_resource_path("features"))
return True
has_features = False
for f in os.listdir("."):
if not f.endswith(".feature"):
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))
)
return has_features
"""
# clean logs to be able to run tests
# once again but on another building model and in another directory
# somehow does not work, thus test will be run in the same directory
# on each new run, directory will be deleted before each new run
# https://github.com/behave/behave/issues/871
# run bimtester
# copy manually this code, run bimtester again,
# does not work on two directories
from behave.runner_util import reset_runtime
reset_runtime()
"""
"""
from code_bimtester import run
myfeatures_path = "/home/hugo/.FreeCAD/Mod/bimtester/features_bimtester/fea_min/"
myifcfile_path = "/home/hugo/Documents/zeug_sort/z_some_ifc/"
ifcfilename = "example_model.ifc"
run.run_all(myfeatures_path, myifcfile_path, ifcfilename)
from code_bimtester import run
myfeatures_path = "/home/hugo/Documents/zeug_sort/ifcos_bimtester/myrun/"
run.run_all(myfeatures_path, myfeatures_path)
"""
# TODO: if the ifc file name or path contains special character
# like German Umlaute behave gives an error
def run_intmp_tests(args={}):
from behave import __version__ as behave_version
# https://github.com/behave/behave/issues/871
if behave_version == "1.2.5":
print(
"At least behave version 1.2.6 is needed, but version {} found."
.format(behave_version)
)
return False
# mandatory parameter: ifcdir, featuredir
# optional parameter: ifcfilename
# copy features and steps to tmp, replace ifcdir in features files
# run
# get ifcpath, this is the path the ifc file is in
if "ifcpath" in args and args["ifcpath"] != "":
# TODO check if path exists
ifc_path = args["ifcpath"]
else:
print("No ifc path was given.")
return False
# get the features_path, the feature files where the tests are in
if "features" in args and args["features"] != "":
# TODO check if path exists, and if features dir is inside
features_path = os.path.join(args["features"], "features")
else:
print("No features path was given.")
return False
if "ifcfilename" in args and args["ifcfilename"] != "":
# TODO check if file
ifc_filename = args["ifcfilename"]
else:
ifc_filename = None
# set up paths
# a unique temp path should not be used
# behave raises an ambiguous step exception
# run_path = tempfile.mkdtemp()
# thus use the same path on every run
# but delete it if exists
run_path = os.path.join(tempfile.gettempdir(), "bimtesterfc")
if os.path.isdir(run_path):
from shutil import rmtree
rmtree(run_path) # fails on read only files
if os.path.isdir(run_path):
print("Delete former beimtester run dir {} failed".format(run_path))
return False
os.mkdir(run_path)
report_path = os.path.join(run_path, "report")
copy_features_path = os.path.join(run_path, "features")
copy_steps_path = os.path.join(copy_features_path, "steps")
# copy features files
# print(features_path)
# print(copy_features_path)
if os.path.exists(features_path):
shutil.copytree(features_path, copy_features_path)
# replace ifcpath in feature files
# IMHO better than copy the ifc file which could be 500 MB
feature_files = os.listdir(copy_features_path)
# print(feature_files)
for feature_file in feature_files:
feature_file = os.path.join(copy_features_path, feature_file)
# print(feature_file)
# search the line
ff = open(feature_file, "r")
lines = ff.readlines()
ff.close()
theline = ""
for line in lines:
if "* The IFC file" in line and "must be provided" in line:
theline = line
if ifc_filename is None:
ifc_filename = os.path.basename(theline.split('"')[1])
newifcline = (
' * The IFC file "{}" must be provided\n'
.format(os.path.join(ifc_path, ifc_filename))
)
# print(newifcline)
break
else:
print("The line which sets the ifc file to test was not found.")
newifcline = ""
# replace the line
if newifcline != "":
# https://stackoverflow.com/a/290494
for line in fileinput.input(feature_file, inplace=True):
# the print replaces the line in the file
print(line.replace(theline, newifcline), end="")
# copy step files and environment file
steps_path = os.path.join(
bimtester_path,
"features",
"steps"
)
# print(steps_path)
# print(copy_steps_path)
if os.path.exists(steps_path):
shutil.copytree(steps_path, copy_steps_path)
environment_file = os.path.join(
bimtester_path,
"features",
"environment.py"
)
if os.path.isfile(environment_file):
shutil.copyfile(
environment_file,
os.path.join(copy_features_path, "environment.py")
)
# get advanced args
# print to console from inside step files, add "--no-capture" flag
# https://github.com/behave/behave/issues/346
behave_args = [copy_features_path]
if "advanced_arguments" in args:
behave_args.extend(args["advanced_arguments"].split())
elif "console" not in args:
behave_args.extend([
# redirect prints in step methods
# if step fails some output is catched, thus might not be printed
"--no-capture",
# next two lines are one arg
"--format",
"json.pretty",
# next two lines are one arg
"--outfile",
os.path.join(report_path, "report.json"),
# next two lines are one arg
"--define",
"ifcbasename={}".format(os.path.splitext(ifc_filename)[0])
])
print(behave_args)
# run tests
from behave.__main__ import main as behave_main
behave_main(behave_args)
print("All tests are finished.")
# delete steps
# shutil.rmtree(steps_path)
return run_path
def run_all(the_features_path, the_ifcfile_path, the_ifcfile_name=None):
# feature files
feature_files = os.listdir(
os.path.join(the_features_path, "features")
)
# print(feature_files)
# run bimtester
if the_ifcfile_name is None:
runpath = run_intmp_tests({
"features": the_features_path,
"ifcpath": the_ifcfile_path
})
else:
runpath = run_intmp_tests({
"features": the_features_path,
"ifcpath": the_ifcfile_path,
"ifcfilename": the_ifcfile_name
})
# create html report
from .reports import generate_report
generate_report(runpath)
# print(runpath)
# open the webbrowser
for ff in feature_files:
webbrowser.open(os.path.join(
runpath,
"report",
ff + ".html"
))
return True