2020-11-23 09:34:13 +01:00
|
|
|
import os
|
2020-11-25 07:28:10 +01:00
|
|
|
import sys
|
2021-02-02 12:34:14 +11:00
|
|
|
import shutil
|
2020-11-23 09:34:13 +01:00
|
|
|
import tempfile
|
2021-02-02 12:34:14 +11:00
|
|
|
import ifcopenshell
|
2021-02-03 21:58:49 +11:00
|
|
|
try:
|
|
|
|
|
import ifcopenshell.express
|
|
|
|
|
except:
|
|
|
|
|
pass # They are using an old version of IfcOpenShell. Gracefully degrade for now.
|
2021-02-02 12:34:14 +11:00
|
|
|
import behave.formatter.pretty # Needed for pyinstaller to package it
|
|
|
|
|
from bimtester.ifc import IfcStore
|
2021-02-03 21:58:49 +11:00
|
|
|
from distutils.dir_util import copy_tree
|
2021-02-02 12:34:14 +11:00
|
|
|
from behave.__main__ import main as behave_main
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRunner:
|
2021-02-02 16:11:38 +11:00
|
|
|
def __init__(self, ifc_path, ifc=None, schema=None):
|
2021-02-02 12:34:14 +11:00
|
|
|
IfcStore.path = ifc_path
|
|
|
|
|
IfcStore.file = ifc if ifc else ifcopenshell.open(ifc_path)
|
|
|
|
|
|
2021-02-02 16:11:38 +11:00
|
|
|
if schema:
|
|
|
|
|
schema = ifcopenshell.express.parse(path)
|
|
|
|
|
ifcopenshell.register_schema(schema)
|
|
|
|
|
|
2021-02-02 12:34:14 +11:00
|
|
|
try:
|
|
|
|
|
# PyInstaller creates a temp folder and stores path in _MEIPASS
|
|
|
|
|
self.base_path = sys._MEIPASS
|
|
|
|
|
except Exception:
|
|
|
|
|
self.base_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
|
|
self.locale_path = os.path.join(self.base_path, "locale")
|
|
|
|
|
|
|
|
|
|
def run(self, args):
|
|
|
|
|
tmpdir = tempfile.mkdtemp()
|
|
|
|
|
features_path = os.path.join(tmpdir, "features")
|
2021-02-03 21:58:49 +11:00
|
|
|
steps_path = os.path.join(features_path, "steps")
|
2021-02-02 12:34:14 +11:00
|
|
|
report_json = os.path.join(tmpdir, "report.json")
|
|
|
|
|
shutil.copytree(os.path.join(self.base_path, "features"), features_path)
|
|
|
|
|
shutil.copy(args["feature"], features_path)
|
2021-02-03 21:58:49 +11:00
|
|
|
if args["steps"]:
|
|
|
|
|
if os.path.isfile(args["steps"]):
|
|
|
|
|
shutil.copy(args["steps"], steps_path)
|
|
|
|
|
elif os.path.isdir(args["steps"]):
|
|
|
|
|
copy_tree(args["steps"], steps_path)
|
2021-02-02 12:34:14 +11:00
|
|
|
behave_main(self.get_behave_args(args, features_path, report_json))
|
|
|
|
|
return report_json
|
|
|
|
|
|
|
|
|
|
def get_behave_args(self, args, features_path, report_json):
|
2021-01-14 19:45:07 +01:00
|
|
|
behave_args = [features_path]
|
2021-02-02 12:34:14 +11:00
|
|
|
behave_args.extend(["--define", "localedir={}".format(self.locale_path)])
|
|
|
|
|
if args["advanced_arguments"]:
|
|
|
|
|
behave_args.extend(args["advanced_arguments"].split())
|
|
|
|
|
if args["ifc"]:
|
|
|
|
|
behave_args.extend(["--define", "ifc={}".format(args["ifc"])])
|
|
|
|
|
if args["path"]:
|
|
|
|
|
behave_args.extend(["--define", "path={}".format(args["path"])])
|
|
|
|
|
if args["lang"]:
|
|
|
|
|
behave_args.extend(["--lang={}".format(args["lang"])])
|
|
|
|
|
if not args["console"]:
|
|
|
|
|
# https://github.com/behave/behave/issues/346
|
|
|
|
|
behave_args.extend(["--no-capture", "--format", "json.pretty", "--outfile", report_json])
|
|
|
|
|
return behave_args
|