2020-11-23 09:34:13 +01:00
|
|
|
import behave.formatter.pretty # Needed for pyinstaller to package it
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2020-11-25 07:28:10 +01:00
|
|
|
import sys
|
2020-11-23 09:34:13 +01:00
|
|
|
import tempfile
|
2020-11-25 07:28:10 +01:00
|
|
|
import webbrowser
|
2020-11-23 09:34:13 +01:00
|
|
|
|
|
|
|
|
|
2021-01-15 10:43:54 +01:00
|
|
|
# TODO: if the ifc file name or path contains special character
|
|
|
|
|
# like German Umlaute behave gives an error
|
|
|
|
|
|
|
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
# get bimtester source code module path
|
|
|
|
|
bimtester_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
# print(bimtester_path)
|
2020-12-18 07:14:42 +01:00
|
|
|
locale_path = os.path.join(bimtester_path, "locale")
|
|
|
|
|
|
|
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
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):
|
2021-01-15 10:43:54 +01:00
|
|
|
|
|
|
|
|
print("# Run tests.")
|
|
|
|
|
|
2021-01-14 19:45:07 +01:00
|
|
|
report_file = os.path.join("report", "report.json")
|
|
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
if "copyintemprun" in args and args["copyintemprun"] is True:
|
|
|
|
|
print("copyintemprun")
|
|
|
|
|
is_copyintemprun = True
|
|
|
|
|
args, copy_base_path = copy_intmp_tests(args)
|
|
|
|
|
features_path = os.path.join(copy_base_path, "features")
|
|
|
|
|
report_file = os.path.join(copy_base_path, report_file)
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print("No copyintemprun")
|
|
|
|
|
is_copyintemprun = False
|
|
|
|
|
if not get_features(args):
|
|
|
|
|
print("No features could be found to check.")
|
|
|
|
|
return False
|
|
|
|
|
features_path = get_resource_path("features")
|
2021-01-14 16:06:26 +01:00
|
|
|
|
|
|
|
|
# get behave args
|
2021-01-14 19:45:07 +01:00
|
|
|
behave_args = get_behave_args(args, features_path, report_file)
|
2021-01-14 16:06:26 +01:00
|
|
|
|
|
|
|
|
# run tests
|
|
|
|
|
if behave_args != []:
|
|
|
|
|
run_behave(behave_args)
|
|
|
|
|
else:
|
|
|
|
|
print("Error, not able to run behave because of empty behave args.")
|
|
|
|
|
return False
|
|
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
if is_copyintemprun is True:
|
|
|
|
|
return report_file
|
|
|
|
|
else:
|
|
|
|
|
return True
|
2021-01-14 16:06:26 +01:00
|
|
|
|
|
|
|
|
|
2021-01-14 19:45:07 +01:00
|
|
|
def get_behave_args(args, features_path, report_file):
|
|
|
|
|
|
|
|
|
|
if os.path.isdir(features_path):
|
|
|
|
|
behave_args = [features_path]
|
|
|
|
|
else:
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
if os.path.isdir(locale_path):
|
|
|
|
|
behave_args.extend([
|
|
|
|
|
# path for translation files
|
|
|
|
|
# next two lines are one arg
|
|
|
|
|
"--define",
|
|
|
|
|
"localedir={}".format(locale_path)
|
|
|
|
|
])
|
|
|
|
|
else:
|
|
|
|
|
print(
|
|
|
|
|
"Error, translation locals path '{}' does not exist."
|
|
|
|
|
.format(locale_path)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if args["advanced_arguments"]:
|
|
|
|
|
behave_args.extend(args["advanced_arguments"].split())
|
|
|
|
|
|
|
|
|
|
if args["ifcfile"]:
|
|
|
|
|
behave_args.extend([
|
|
|
|
|
# next two lines are one arg
|
|
|
|
|
"--define",
|
|
|
|
|
"ifcfile={}".format(args["ifcfile"])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
if args["path"]:
|
|
|
|
|
behave_args.extend([
|
|
|
|
|
# next two lines are one arg
|
|
|
|
|
"--define",
|
|
|
|
|
"path={}".format(args["path"])
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
if not args["console"]:
|
|
|
|
|
behave_args.extend([
|
|
|
|
|
# redirect prints in step methods
|
|
|
|
|
# if step fails some output is catched, thus might not be printed
|
|
|
|
|
# https://github.com/behave/behave/issues/346
|
|
|
|
|
"--no-capture",
|
|
|
|
|
# next two lines are one arg
|
|
|
|
|
"--format",
|
|
|
|
|
"json.pretty",
|
|
|
|
|
# report file, if relative, than relative to current shell path
|
|
|
|
|
# next two lines are one arg
|
|
|
|
|
"--outfile",
|
|
|
|
|
report_file,
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
return behave_args
|
|
|
|
|
|
|
|
|
|
|
2021-01-14 16:06:26 +01:00
|
|
|
def run_behave(behave_args):
|
|
|
|
|
|
|
|
|
|
from json import dumps
|
|
|
|
|
print(dumps(behave_args, indent=4))
|
|
|
|
|
|
|
|
|
|
from behave.__main__ import main as behave_main
|
2020-11-23 09:34:13 +01:00
|
|
|
behave_main(behave_args)
|
|
|
|
|
print("# All tests are finished.")
|
2021-01-14 16:06:26 +01:00
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
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"]:
|
2020-12-18 07:06:14 +01:00
|
|
|
shutil.copyfile(
|
|
|
|
|
args["feature"],
|
|
|
|
|
os.path.join(
|
|
|
|
|
get_resource_path("features"),
|
|
|
|
|
os.path.basename(args["feature"])
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-11-23 09:34:13 +01:00
|
|
|
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
|
2020-12-18 07:06:14 +01:00
|
|
|
shutil.copyfile(
|
|
|
|
|
f,
|
|
|
|
|
os.path.join(get_resource_path("features"), os.path.basename(f))
|
|
|
|
|
)
|
2020-11-23 09:34:13 +01:00
|
|
|
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
|
2020-11-25 07:28:10 +01:00
|
|
|
# copy manually this code, run bimtester again,
|
|
|
|
|
# does not work on two directories
|
2020-11-23 09:34:13 +01:00
|
|
|
from behave.runner_util import reset_runtime
|
|
|
|
|
reset_runtime()
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
def copy_intmp_tests(args={}):
|
2020-11-23 09:34:13 +01:00
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
print("# Copy features and steps to temp.")
|
2021-01-14 15:45:11 +01:00
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
from behave import __version__ as behave_version
|
|
|
|
|
# https://github.com/behave/behave/issues/871
|
|
|
|
|
if behave_version == "1.2.5":
|
2020-12-18 07:06:14 +01:00
|
|
|
print(
|
|
|
|
|
"At least behave version 1.2.6 is needed, but version {} found."
|
|
|
|
|
.format(behave_version)
|
|
|
|
|
)
|
2020-11-23 09:34:13 +01:00
|
|
|
return False
|
|
|
|
|
|
2021-01-14 15:45:11 +01:00
|
|
|
# print(args)
|
2021-01-12 20:33:01 +01:00
|
|
|
# get the features_path, the dir where the feature files to test are in
|
|
|
|
|
if ("featuresdir" in args and args["featuresdir"] != ""):
|
2021-01-12 10:40:23 +01:00
|
|
|
is_features = True
|
2021-01-12 20:33:01 +01:00
|
|
|
the_features_path = os.path.join(args["featuresdir"], "features")
|
2021-01-05 08:48:48 +01:00
|
|
|
if not os.path.isdir(the_features_path):
|
2020-12-18 07:06:14 +01:00
|
|
|
print(
|
2021-01-12 10:40:23 +01:00
|
|
|
"Error, the features directory '{}' does not exist."
|
2021-01-05 08:48:48 +01:00
|
|
|
.format(the_features_path)
|
2020-12-18 07:06:14 +01:00
|
|
|
)
|
|
|
|
|
return False
|
2021-01-12 20:33:01 +01:00
|
|
|
else:
|
|
|
|
|
is_features = False
|
2020-11-23 09:34:13 +01:00
|
|
|
|
2021-01-12 20:33:01 +01:00
|
|
|
# get ifc path and ifc filename
|
|
|
|
|
if ("ifcfile" in args and args["ifcfile"] != ""):
|
|
|
|
|
is_ifcfile = True
|
2020-11-27 17:52:23 +01:00
|
|
|
ifcfile = args["ifcfile"]
|
2021-01-14 15:45:11 +01:00
|
|
|
if os.path.isfile(ifcfile) is not True:
|
|
|
|
|
print("Error, the ifc file '{}' does not exist.".format(ifcfile))
|
|
|
|
|
return False
|
2020-11-27 17:52:23 +01:00
|
|
|
ifc_path = os.path.dirname(os.path.realpath(ifcfile))
|
|
|
|
|
if os.path.isdir(ifc_path) is False:
|
|
|
|
|
print("ifc path does not exist.")
|
|
|
|
|
return False
|
2021-01-12 20:33:01 +01:00
|
|
|
else:
|
|
|
|
|
is_ifcfile = False
|
|
|
|
|
|
|
|
|
|
# print(is_features)
|
|
|
|
|
# print(is_ifcfile)
|
|
|
|
|
|
|
|
|
|
if is_features is True and is_ifcfile is True:
|
|
|
|
|
print("features given, ifcfile given.")
|
2021-01-12 10:40:23 +01:00
|
|
|
|
|
|
|
|
elif is_features is False and is_ifcfile is True:
|
2021-01-12 20:33:01 +01:00
|
|
|
print("features given, ifcfile NOT given.")
|
2021-01-12 10:40:23 +01:00
|
|
|
# features = ifcfile directory
|
2021-01-12 20:33:01 +01:00
|
|
|
the_features_path = os.path.join(ifc_path, "features")
|
2021-01-12 10:40:23 +01:00
|
|
|
|
|
|
|
|
elif is_features is True and is_ifcfile is False:
|
2021-01-12 20:33:01 +01:00
|
|
|
print("features given, ifcfile NOT given.")
|
2021-01-12 10:40:23 +01:00
|
|
|
# the ifcfile provided in the feature files is used
|
2021-01-14 15:45:11 +01:00
|
|
|
# TODO What will be passed as ifcfile arg?
|
2021-01-12 20:33:01 +01:00
|
|
|
print("Not yet implemented.")
|
2021-01-12 10:40:23 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
elif is_features is False and is_ifcfile is False:
|
2021-01-12 20:33:01 +01:00
|
|
|
print("features NOT given, ifcfile NOT given.")
|
2021-01-12 10:40:23 +01:00
|
|
|
# the current directory = features
|
|
|
|
|
# the ifcfile provided in the feature files is used
|
2021-01-14 15:45:11 +01:00
|
|
|
# TODO What will be passed as ifcfile arg?
|
2021-01-12 20:33:01 +01:00
|
|
|
print("Not yet implemented.")
|
2021-01-12 10:40:23 +01:00
|
|
|
return False
|
|
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
else:
|
2021-01-12 10:40:23 +01:00
|
|
|
print("Error: this should never happen, please debug.")
|
2020-11-27 17:52:23 +01:00
|
|
|
return False
|
2020-11-23 09:34:13 +01:00
|
|
|
|
|
|
|
|
# set up paths
|
|
|
|
|
# a unique temp path should not be used
|
|
|
|
|
# behave raises an ambiguous step exception
|
2021-01-12 13:44:11 +01:00
|
|
|
# copy_base_path = tempfile.mkdtemp()
|
2020-11-23 09:34:13 +01:00
|
|
|
# thus use the same path on every run
|
|
|
|
|
# but delete it if exists
|
2021-01-12 13:44:11 +01:00
|
|
|
copy_base_path = os.path.join(tempfile.gettempdir(), "bimtesterfc")
|
|
|
|
|
if os.path.isdir(copy_base_path):
|
2020-11-23 09:34:13 +01:00
|
|
|
from shutil import rmtree
|
2021-01-12 13:44:11 +01:00
|
|
|
rmtree(copy_base_path) # fails on read only files
|
|
|
|
|
if os.path.isdir(copy_base_path):
|
2021-01-12 20:33:01 +01:00
|
|
|
print(
|
|
|
|
|
"Delete former beimtester run dir '{}' failed"
|
|
|
|
|
.format(copy_base_path)
|
|
|
|
|
)
|
2020-11-23 09:34:13 +01:00
|
|
|
return False
|
2021-01-12 13:44:11 +01:00
|
|
|
os.mkdir(copy_base_path)
|
|
|
|
|
copy_features_path = os.path.join(copy_base_path, "features")
|
2021-01-05 08:48:48 +01:00
|
|
|
|
|
|
|
|
# copy features path from bimtester source code
|
|
|
|
|
srccode_features_path = os.path.join(
|
|
|
|
|
bimtester_path,
|
|
|
|
|
"features",
|
|
|
|
|
)
|
|
|
|
|
# print(srccode_features_path)
|
|
|
|
|
if os.path.exists(srccode_features_path):
|
|
|
|
|
shutil.copytree(srccode_features_path, copy_features_path)
|
|
|
|
|
else:
|
|
|
|
|
print(
|
|
|
|
|
"Bimtester source code features directory {} not found."
|
|
|
|
|
.format(srccode_features_path)
|
|
|
|
|
)
|
|
|
|
|
return False
|
2020-11-23 09:34:13 +01:00
|
|
|
|
|
|
|
|
# copy features files
|
2021-01-05 08:48:48 +01:00
|
|
|
# print(the_features_path)
|
2020-11-23 09:34:13 +01:00
|
|
|
# print(copy_features_path)
|
2021-01-05 08:48:48 +01:00
|
|
|
# parameter dirs_exist_ok=True, from py 3.8
|
|
|
|
|
# if os.path.exists(the_features_path):
|
|
|
|
|
# shutil.copytree(
|
|
|
|
|
# the_features_path,
|
|
|
|
|
# copy_features_path,
|
|
|
|
|
# dirs_exist_ok=True
|
|
|
|
|
# )
|
|
|
|
|
|
2021-01-14 15:45:11 +01:00
|
|
|
# copy feature files
|
2021-01-05 08:48:48 +01:00
|
|
|
feature_files = os.listdir(the_features_path)
|
2020-11-23 09:34:13 +01:00
|
|
|
# print(feature_files)
|
|
|
|
|
for feature_file in feature_files:
|
2021-01-05 08:48:48 +01:00
|
|
|
cp_feature_file = os.path.join(copy_features_path, feature_file)
|
2020-11-23 09:34:13 +01:00
|
|
|
# print(feature_file)
|
2021-01-05 08:48:48 +01:00
|
|
|
# copy file
|
|
|
|
|
shutil.copyfile(
|
|
|
|
|
os.path.join(the_features_path, feature_file),
|
|
|
|
|
cp_feature_file
|
|
|
|
|
)
|
2020-11-23 09:34:13 +01:00
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
return args, copy_base_path
|
2020-11-23 09:34:13 +01:00
|
|
|
|
|
|
|
|
|
2021-01-14 15:53:14 +01:00
|
|
|
def run_all(args):
|
2020-11-23 09:34:13 +01:00
|
|
|
|
2021-01-15 10:43:54 +01:00
|
|
|
print("# Run all.")
|
|
|
|
|
|
2020-11-23 09:34:13 +01:00
|
|
|
# run bimtester
|
2021-01-14 22:05:58 +01:00
|
|
|
report_file = run_tests(args)
|
|
|
|
|
print(report_file)
|
2020-11-27 17:52:23 +01:00
|
|
|
|
|
|
|
|
# check if it worked out well
|
2021-01-14 22:05:58 +01:00
|
|
|
if report_file is False:
|
2020-11-27 17:52:23 +01:00
|
|
|
print("BIMTester behave tests returned False.")
|
|
|
|
|
return False
|
|
|
|
|
|
2021-01-14 22:05:58 +01:00
|
|
|
if not os.path.isfile(report_file):
|
|
|
|
|
print("Report directory does not exist. This should not happen. Debug")
|
2020-11-27 17:52:23 +01:00
|
|
|
return False
|
2020-11-23 09:34:13 +01:00
|
|
|
|
2020-11-27 17:52:23 +01:00
|
|
|
# create html report and open in webbrowser
|
2020-11-23 09:34:13 +01:00
|
|
|
from .reports import generate_report
|
2021-01-14 22:05:58 +01:00
|
|
|
generate_report(report_file=report_file)
|
2020-11-27 17:52:23 +01:00
|
|
|
# get the feature files
|
2020-12-18 07:06:14 +01:00
|
|
|
feature_files = os.listdir(
|
2021-01-14 15:53:14 +01:00
|
|
|
os.path.join(args["featuresdir"], "features")
|
2020-12-18 07:06:14 +01:00
|
|
|
)
|
2020-11-27 17:52:23 +01:00
|
|
|
# print(feature_files)
|
2020-11-23 09:34:13 +01:00
|
|
|
for ff in feature_files:
|
2020-12-18 07:06:14 +01:00
|
|
|
webbrowser.open(os.path.join(
|
2021-01-14 22:05:58 +01:00
|
|
|
os.path.dirname(report_file),
|
2020-12-18 07:06:14 +01:00
|
|
|
ff + ".html"
|
|
|
|
|
))
|
2020-11-23 09:34:13 +01:00
|
|
|
|
|
|
|
|
return True
|