Coverage for teaser/data/output/runUnitTests.py: 0%

81 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-04-29 16:01 +0000

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3####################################################### 

4# Script that runs all unit tests or, optionally, 

5# only checks the html syntax or the validity of 

6# the simulation parameters of the models 

7# 

8# To run the unit tests, this script 

9# - creates temporary directories for each processor, 

10# - copies the library directory into these 

11# temporary directories, 

12# - creates run scripts that run all unit tests, 

13# - runs these unit tests, 

14# - collects the dymola log files from each process, 

15# - writes the combined log file 'unitTests.log' 

16# in the current directory, 

17# - checks whether all unit tests run successfully, 

18# and produced the same results as the reference 

19# results, and 

20# - exits with the message 

21# 'Unit tests completed successfully.' or with 

22# an error message. 

23# 

24# If no errors occurred during the unit tests, then 

25# this script returns 0. Otherwise, it returns a 

26# non-zero exit value. 

27# 

28# MWetter@lbl.gov 2011-02-23 

29# TSNouidui@lbl.gov 2017-04-11 

30####################################################### 

31from __future__ import absolute_import 

32from __future__ import division 

33from __future__ import print_function 

34 

35 

36def _validate_experiment_setup(path): 

37 import buildingspy.development.validator as v 

38 

39 val = v.Validator() 

40 retVal = val.validateExperimentSetup(path) 

41 

42 return retVal 

43 

44 

45def _validate_html(path): 

46 import buildingspy.development.validator as v 

47 

48 val = v.Validator() 

49 errMsg = val.validateHTMLInPackage(path) 

50 n_msg = len(errMsg) 

51 for i in range(n_msg): 

52 if i == 0: 

53 print( 

54 "The following malformed html syntax has been found:\n{}".format( 

55 errMsg[i] 

56 ) 

57 ) 

58 else: 

59 print(errMsg[i]) 

60 

61 if n_msg == 0: 

62 return 0 

63 else: 

64 return 1 

65 

66 

67def _setEnvironmentVariables(var, value): 

68 """ Add to the environment variable `var` the value `value` 

69 """ 

70 import os 

71 import platform 

72 

73 if var in os.environ: 

74 if platform.system() == "Windows": 

75 os.environ[var] = value + ";" + os.environ[var] 

76 else: 

77 os.environ[var] = value + ":" + os.environ[var] 

78 else: 

79 os.environ[var] = value 

80 

81 

82def _runUnitTests(batch, tool, package, path, n_pro, show_gui, skip_verification): 

83 import buildingspy.development.regressiontest as u 

84 

85 ut = u.Tester(tool=tool, skip_verification=skip_verification) 

86 ut.batchMode(batch) 

87 ut.setLibraryRoot(path) 

88 if package is not None: 

89 ut.setSinglePackage(package) 

90 ut.setNumberOfThreads(n_pro) 

91 ut.pedanticModelica(False) 

92 ut.showGUI(show_gui) 

93 # Below are some option that may occassionally be used. 

94 # These are currently not exposed as command line arguments. 

95 # ut.setNumberOfThreads(1) 

96 # ut.deleteTemporaryDirectories(False) 

97 # ut.useExistingResults(['/tmp/tmp-Buildings-0-fagmeZ']) 

98 

99 # ut.writeOpenModelicaResultDictionary() 

100 # Run the regression tests 

101 

102 retVal = ut.run() 

103 

104 # Display HTML report if not run in batch mode. 

105 # (For buildingspy.__version__ >= 2) 

106 if not batch: 

107 try: 

108 if not skip_verification: 

109 ut.report() 

110 except AttributeError: 

111 pass 

112 

113 return retVal 

114 

115 

116if __name__ == "__main__": 

117 import multiprocessing 

118 import platform 

119 import argparse 

120 import os 

121 import sys 

122 

123 # Configure the argument parser 

124 parser = argparse.ArgumentParser( 

125 description="Run the unit tests or the html validation only." 

126 ) 

127 unit_test_group = parser.add_argument_group("arguments to run unit tests") 

128 

129 unit_test_group.add_argument( 

130 "-b", 

131 "--batch", 

132 action="store_true", 

133 help="Run in batch mode without user interaction", 

134 ) 

135 unit_test_group.add_argument( 

136 "-t", 

137 "--tool", 

138 metavar="dymola", 

139 default="dymola", 

140 help="Tool for the regression tests. Set to dymola or jmodelica", 

141 ) 

142 unit_test_group.add_argument( 

143 "-s", 

144 "--single-package", 

145 metavar="Modelica.Package", 

146 help="Test only the Modelica package Modelica.Package", 

147 ) 

148 unit_test_group.add_argument( 

149 "-p", 

150 "--path", 

151 default=".", 

152 help="Path where top-level package.mo of the library is located", 

153 ) 

154 unit_test_group.add_argument( 

155 "-n", 

156 "--number-of-processors", 

157 type=int, 

158 default=multiprocessing.cpu_count(), 

159 help="Maximum number of processors to be used", 

160 ) 

161 unit_test_group.add_argument( 

162 "--show-gui", help="Show the GUI of the simulator", action="store_true" 

163 ) 

164 unit_test_group.add_argument( 

165 "--skip-verification", 

166 help="If specified, do not verify simulation results against reference points", 

167 action="store_true", 

168 ) 

169 

170 html_group = parser.add_argument_group("arguments to check html syntax only") 

171 html_group.add_argument("--validate-html-only", action="store_true") 

172 

173 experiment_setup_group = parser.add_argument_group( 

174 "arguments to check validity of .mos and .mo experiment setup only" 

175 ) 

176 experiment_setup_group.add_argument( 

177 "--validate-experiment-setup", action="store_true" 

178 ) 

179 

180 # Set environment variables 

181 if platform.system() == "Windows": 

182 _setEnvironmentVariables( 

183 "PATH", os.path.join(os.path.abspath("."), "Resources", "Library", "win32") 

184 ) 

185 else: 

186 # For https://github.com/lbl-srg/modelica-buildings/issues/559, we add 

187 # 32 and 64 bit resources to run the Utilities.IO.Python27 regression tests. 

188 _setEnvironmentVariables( 

189 "LD_LIBRARY_PATH", 

190 os.path.join(os.path.abspath("."), "Resources", "Library", "linux32") 

191 + ":" 

192 + os.path.join(os.path.abspath("."), "Resources", "Library", "linux64"), 

193 ) 

194 

195 # The path to buildingspy must be added to sys.path to work on Linux. 

196 # If only added to os.environ, the Python interpreter won't find buildingspy 

197 sys.path.append(os.path.join(os.path.abspath("."), "..", "..", "BuildingsPy")) 

198 

199 # Parse the arguments 

200 args = parser.parse_args() 

201 

202 if args.validate_html_only: 

203 # Validate the html syntax only, and then exit 

204 ret_val = _validate_html(args.path) 

205 exit(ret_val) 

206 

207 if args.validate_experiment_setup: 

208 # Match the mos file parameters with the mo files only, and then exit 

209 ret_val = _validate_experiment_setup(args.path) 

210 exit(ret_val) 

211 

212 if args.single_package: 

213 single_package = args.single_package 

214 else: 

215 single_package = None 

216 

217 retVal = _runUnitTests( 

218 batch=args.batch, 

219 tool=args.tool, 

220 package=single_package, 

221 path=args.path, 

222 n_pro=args.number_of_processors, 

223 show_gui=args.show_gui, 

224 skip_verification=args.skip_verification, 

225 ) 

226 exit(retVal) 

227 

228# _runOpenModelicaUnitTests()