ytest.py 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. from __future__ import print_function
  2. import os
  3. import re
  4. import sys
  5. import six
  6. import json
  7. import copy
  8. import base64
  9. import shlex
  10. import _common
  11. import lib.test_const as consts
  12. import _requirements as reqs
  13. try:
  14. from StringIO import StringIO
  15. except ImportError:
  16. from io import StringIO
  17. import subprocess
  18. import collections
  19. import ymake
  20. CANON_DATA_DIR_NAME = 'canondata'
  21. CANON_OUTPUT_STORAGE = 'canondata_storage'
  22. CANON_RESULT_FILE_NAME = 'result.json'
  23. BLOCK_SEPARATOR = '============================================================='
  24. SPLIT_FACTOR_MAX_VALUE = 1000
  25. SPLIT_FACTOR_TEST_FILES_MAX_VALUE = 4250
  26. PARTITION_MODS = ('SEQUENTIAL', 'MODULO')
  27. DEFAULT_TIDY_CONFIG = "build/config/tests/clang_tidy/config.yaml"
  28. DEFAULT_TIDY_CONFIG_MAP_PATH = "build/yandex_specific/config/clang_tidy/tidy_default_map.json"
  29. PROJECT_TIDY_CONFIG_MAP_PATH = "build/yandex_specific/config/clang_tidy/tidy_project_map.json"
  30. KTLINT_CURRENT_EDITOR_CONFIG = "arcadia/build/platform/java/ktlint/.editorconfig"
  31. KTLINT_OLD_EDITOR_CONFIG = "arcadia/build/platform/java/ktlint_old/.editorconfig"
  32. tidy_config_map = None
  33. def ontest_data(unit, *args):
  34. ymake.report_configure_error("TEST_DATA is removed in favour of DATA")
  35. def prepare_recipes(data):
  36. data = data.replace('"USE_RECIPE_DELIM"', "\n")
  37. data = data.replace("$TEST_RECIPES_VALUE", "")
  38. return base64.b64encode(six.ensure_binary(data or ""))
  39. def prepare_env(data):
  40. data = data.replace("$TEST_ENV_VALUE", "")
  41. return serialize_list(shlex.split(data))
  42. def is_yt_spec_contain_pool_info(filename): # XXX switch to yson in ymake + perf test for configure
  43. pool_re = re.compile(r"""['"]*pool['"]*\s*?=""")
  44. cypress_root_re = re.compile(r"""['"]*cypress_root['"]*\s*=""")
  45. with open(filename, 'r') as afile:
  46. yt_spec = afile.read()
  47. return pool_re.search(yt_spec) and cypress_root_re.search(yt_spec)
  48. def validate_test(unit, kw):
  49. def get_list(key):
  50. return deserialize_list(kw.get(key, ""))
  51. valid_kw = copy.deepcopy(kw)
  52. errors = []
  53. warnings = []
  54. if valid_kw.get('SCRIPT-REL-PATH') == 'boost.test':
  55. project_path = valid_kw.get('BUILD-FOLDER-PATH', "")
  56. if not project_path.startswith(
  57. ("contrib", "mail", "maps", "tools/idl", "metrika", "devtools", "mds", "yandex_io", "smart_devices")
  58. ):
  59. errors.append("BOOSTTEST is not allowed here")
  60. size_timeout = collections.OrderedDict(sorted(consts.TestSize.DefaultTimeouts.items(), key=lambda t: t[1]))
  61. size = valid_kw.get('SIZE', consts.TestSize.Small).lower()
  62. tags = set(get_list("TAG"))
  63. requirements_orig = get_list("REQUIREMENTS")
  64. in_autocheck = consts.YaTestTags.NotAutocheck not in tags and consts.YaTestTags.Manual not in tags
  65. is_fat = consts.YaTestTags.Fat in tags
  66. is_force_sandbox = consts.YaTestTags.ForceDistbuild not in tags and is_fat
  67. is_ytexec_run = consts.YaTestTags.YtRunner in tags
  68. is_fuzzing = valid_kw.get("FUZZING", False)
  69. is_kvm = 'kvm' in requirements_orig
  70. requirements = {}
  71. secret_requirements = ('sb_vault', 'yav')
  72. list_requirements = secret_requirements
  73. for req in requirements_orig:
  74. if req in ('kvm',):
  75. requirements[req] = str(True)
  76. continue
  77. if ":" in req:
  78. req_name, req_value = req.split(":", 1)
  79. if req_name in list_requirements:
  80. requirements[req_name] = ",".join(filter(None, [requirements.get(req_name), req_value]))
  81. else:
  82. if req_name in requirements:
  83. if req_value in ["0"]:
  84. warnings.append(
  85. "Requirement [[imp]]{}[[rst]] is dropped [[imp]]{}[[rst]] -> [[imp]]{}[[rst]]".format(
  86. req_name, requirements[req_name], req_value
  87. )
  88. )
  89. del requirements[req_name]
  90. elif requirements[req_name] != req_value:
  91. warnings.append(
  92. "Requirement [[imp]]{}[[rst]] is redefined [[imp]]{}[[rst]] -> [[imp]]{}[[rst]]".format(
  93. req_name, requirements[req_name], req_value
  94. )
  95. )
  96. requirements[req_name] = req_value
  97. else:
  98. requirements[req_name] = req_value
  99. else:
  100. errors.append("Invalid requirement syntax [[imp]]{}[[rst]]: expect <requirement>:<value>".format(req))
  101. if not errors:
  102. for req_name, req_value in requirements.items():
  103. try:
  104. error_msg = reqs.validate_requirement(
  105. req_name,
  106. req_value,
  107. size,
  108. is_force_sandbox,
  109. in_autocheck,
  110. is_fuzzing,
  111. is_kvm,
  112. is_ytexec_run,
  113. requirements,
  114. )
  115. except Exception as e:
  116. error_msg = str(e)
  117. if error_msg:
  118. errors += [error_msg]
  119. invalid_requirements_for_distbuild = [
  120. requirement for requirement in requirements.keys() if requirement not in ('ram', 'ram_disk', 'cpu', 'network')
  121. ]
  122. sb_tags = []
  123. # XXX Unfortunately, some users have already started using colons
  124. # in their tag names. Use skip set to avoid treating their tag as system ones.
  125. # Remove this check when all such user tags are removed.
  126. skip_set = ('ynmt_benchmark', 'bert_models', 'zeliboba_map')
  127. # Verify the prefixes of the system tags to avoid pointless use of the REQUIREMENTS macro parameters in the TAG macro.
  128. for tag in tags:
  129. if tag.startswith('sb:'):
  130. sb_tags.append(tag)
  131. elif ':' in tag and not tag.startswith('ya:') and tag.split(':')[0] not in skip_set:
  132. errors.append(
  133. "Only [[imp]]sb:[[rst]] and [[imp]]ya:[[rst]] prefixes are allowed in system tags: {}".format(tag)
  134. )
  135. if is_fat:
  136. if size != consts.TestSize.Large:
  137. errors.append("Only LARGE test may have ya:fat tag")
  138. if in_autocheck and not is_force_sandbox:
  139. if invalid_requirements_for_distbuild:
  140. errors.append(
  141. "'{}' REQUIREMENTS options can be used only for FAT tests without ya:force_distbuild tag. Remove TAG(ya:force_distbuild) or an option.".format(
  142. invalid_requirements_for_distbuild
  143. )
  144. )
  145. if sb_tags:
  146. errors.append(
  147. "You can set sandbox tags '{}' only for FAT tests without ya:force_distbuild. Remove TAG(ya:force_sandbox) or sandbox tags.".format(
  148. sb_tags
  149. )
  150. )
  151. if consts.YaTestTags.SandboxCoverage in tags:
  152. errors.append("You can set 'ya:sandbox_coverage' tag only for FAT tests without ya:force_distbuild.")
  153. if is_ytexec_run:
  154. errors.append(
  155. "Running LARGE tests over YT (ya:yt) on Distbuild (ya:force_distbuild) is forbidden. Consider removing TAG(ya:force_distbuild)."
  156. )
  157. else:
  158. if is_force_sandbox:
  159. errors.append('ya:force_sandbox can be used with LARGE tests only')
  160. if consts.YaTestTags.NoFuse in tags:
  161. errors.append('ya:nofuse can be used with LARGE tests only')
  162. if consts.YaTestTags.Privileged in tags:
  163. errors.append("ya:privileged can be used with LARGE tests only")
  164. if in_autocheck and size == consts.TestSize.Large:
  165. errors.append("LARGE test must have ya:fat tag")
  166. if consts.YaTestTags.Privileged in tags and 'container' not in requirements:
  167. errors.append("Only tests with 'container' requirement can have 'ya:privileged' tag")
  168. if size not in size_timeout:
  169. errors.append(
  170. "Unknown test size: [[imp]]{}[[rst]], choose from [[imp]]{}[[rst]]".format(
  171. size.upper(), ", ".join([sz.upper() for sz in size_timeout.keys()])
  172. )
  173. )
  174. else:
  175. try:
  176. timeout = int(valid_kw.get('TEST-TIMEOUT', size_timeout[size]) or size_timeout[size])
  177. script_rel_path = valid_kw.get('SCRIPT-REL-PATH')
  178. if timeout < 0:
  179. raise Exception("Timeout must be > 0")
  180. skip_timeout_verification = script_rel_path in ('java.style', 'ktlint')
  181. if size_timeout[size] < timeout and in_autocheck and not skip_timeout_verification:
  182. suggested_size = None
  183. for s, t in size_timeout.items():
  184. if timeout <= t:
  185. suggested_size = s
  186. break
  187. if suggested_size:
  188. suggested_size = ", suggested size: [[imp]]{}[[rst]]".format(suggested_size.upper())
  189. else:
  190. suggested_size = ""
  191. errors.append(
  192. "Max allowed timeout for test size [[imp]]{}[[rst]] is [[imp]]{} sec[[rst]]{}".format(
  193. size.upper(), size_timeout[size], suggested_size
  194. )
  195. )
  196. except Exception as e:
  197. errors.append("Error when parsing test timeout: [[bad]]{}[[rst]]".format(e))
  198. requirements_list = []
  199. for req_name, req_value in six.iteritems(requirements):
  200. requirements_list.append(req_name + ":" + req_value)
  201. valid_kw['REQUIREMENTS'] = serialize_list(sorted(requirements_list))
  202. # Mark test with ya:external tag if it requests any secret from external storages
  203. # It's not stable and nonreproducible by definition
  204. for x in secret_requirements:
  205. if x in requirements:
  206. tags.add(consts.YaTestTags.External)
  207. if valid_kw.get("FUZZ-OPTS"):
  208. for option in get_list("FUZZ-OPTS"):
  209. if not option.startswith("-"):
  210. errors.append(
  211. "Unrecognized fuzzer option '[[imp]]{}[[rst]]'. All fuzzer options should start with '-'".format(
  212. option
  213. )
  214. )
  215. break
  216. eqpos = option.find("=")
  217. if eqpos == -1 or len(option) == eqpos + 1:
  218. errors.append(
  219. "Unrecognized fuzzer option '[[imp]]{}[[rst]]'. All fuzzer options should obtain value specified after '='".format(
  220. option
  221. )
  222. )
  223. break
  224. if option[eqpos - 1] == " " or option[eqpos + 1] == " ":
  225. errors.append("Spaces are not allowed: '[[imp]]{}[[rst]]'".format(option))
  226. break
  227. if option[:eqpos] in ("-runs", "-dict", "-jobs", "-workers", "-artifact_prefix", "-print_final_stats"):
  228. errors.append(
  229. "You can't use '[[imp]]{}[[rst]]' - it will be automatically calculated or configured during run".format(
  230. option
  231. )
  232. )
  233. break
  234. if valid_kw.get("YT-SPEC"):
  235. if not is_ytexec_run:
  236. errors.append("You can use YT_SPEC macro only tests marked with ya:yt tag")
  237. else:
  238. for filename in get_list("YT-SPEC"):
  239. filename = unit.resolve('$S/' + filename)
  240. if not os.path.exists(filename):
  241. errors.append("File '{}' specified in the YT_SPEC macro doesn't exist".format(filename))
  242. continue
  243. if not is_yt_spec_contain_pool_info(filename):
  244. tags.add(consts.YaTestTags.External)
  245. tags.add("ya:yt_research_pool")
  246. partition = valid_kw.get('TEST_PARTITION', 'SEQUENTIAL')
  247. if partition not in PARTITION_MODS:
  248. raise ValueError('partition mode should be one of {}, detected: {}'.format(PARTITION_MODS, partition))
  249. if valid_kw.get('SPLIT-FACTOR'):
  250. if valid_kw.get('FORK-MODE') == 'none':
  251. errors.append('SPLIT_FACTOR must be use with FORK_TESTS() or FORK_SUBTESTS() macro')
  252. value = 1
  253. try:
  254. value = int(valid_kw.get('SPLIT-FACTOR'))
  255. if value <= 0:
  256. raise ValueError("must be > 0")
  257. if value > SPLIT_FACTOR_MAX_VALUE:
  258. raise ValueError("the maximum allowed value is {}".format(SPLIT_FACTOR_MAX_VALUE))
  259. except ValueError as e:
  260. errors.append('Incorrect SPLIT_FACTOR value: {}'.format(e))
  261. if valid_kw.get('FORK-TEST-FILES') and size != consts.TestSize.Large:
  262. nfiles = count_entries(valid_kw.get('TEST-FILES'))
  263. if nfiles * value > SPLIT_FACTOR_TEST_FILES_MAX_VALUE:
  264. errors.append(
  265. 'Too much chunks generated:{} (limit: {}). Remove FORK_TEST_FILES() macro or reduce SPLIT_FACTOR({}).'.format(
  266. nfiles * value, SPLIT_FACTOR_TEST_FILES_MAX_VALUE, value
  267. )
  268. )
  269. if tags:
  270. valid_kw['TAG'] = serialize_list(sorted(tags))
  271. unit_path = _common.get_norm_unit_path(unit)
  272. if (
  273. not is_fat
  274. and consts.YaTestTags.Noretries in tags
  275. and not is_ytexec_run
  276. and not unit_path.startswith("devtools/dummy_arcadia/test/noretries")
  277. ):
  278. errors.append("Only LARGE tests can have 'ya:noretries' tag")
  279. if errors:
  280. return None, warnings, errors
  281. return valid_kw, warnings, errors
  282. def dump_test(unit, kw):
  283. valid_kw, warnings, errors = validate_test(unit, kw)
  284. for w in warnings:
  285. unit.message(['warn', w])
  286. for e in errors:
  287. ymake.report_configure_error(e)
  288. if valid_kw is None:
  289. return None
  290. string_handler = StringIO()
  291. for k, v in six.iteritems(valid_kw):
  292. print(k + ': ' + six.ensure_str(v), file=string_handler)
  293. print(BLOCK_SEPARATOR, file=string_handler)
  294. data = string_handler.getvalue()
  295. string_handler.close()
  296. return data
  297. def serialize_list(lst):
  298. lst = list(filter(None, lst))
  299. return '\"' + ';'.join(lst) + '\"' if lst else ''
  300. def deserialize_list(val):
  301. return list(filter(None, val.replace('"', "").split(";")))
  302. def get_correct_expression_for_group_var(varname):
  303. return r"\"${join=\;:" + varname + "}\""
  304. def count_entries(x):
  305. # see (de)serialize_list
  306. assert x is None or isinstance(x, str), type(x)
  307. if not x:
  308. return 0
  309. return x.count(";") + 1
  310. def get_values_list(unit, key):
  311. res = map(str.strip, (unit.get(key) or '').replace('$' + key, '').strip().split())
  312. return [r for r in res if r and r not in ['""', "''"]]
  313. def get_norm_paths(unit, key):
  314. # return paths without trailing (back)slash
  315. return [x.rstrip('\\/').replace('${ARCADIA_ROOT}/', '') for x in get_values_list(unit, key)]
  316. def get_unit_list_variable(unit, name):
  317. items = unit.get(name)
  318. if items:
  319. items = items.split(' ')
  320. assert items[0] == "${}".format(name), (items, name)
  321. return items[1:]
  322. return []
  323. def implies(a, b):
  324. return bool((not a) or b)
  325. def match_coverage_extractor_requirements(unit):
  326. # we shouldn't add test if
  327. return all(
  328. [
  329. # tests are not requested
  330. unit.get("TESTS_REQUESTED") == "yes",
  331. # build doesn't imply clang coverage, which supports segment extraction from the binaries
  332. unit.get("CLANG_COVERAGE") == "yes",
  333. # contrib wasn't requested
  334. implies(
  335. _common.get_norm_unit_path(unit).startswith("contrib/"), unit.get("ENABLE_CONTRIB_COVERAGE") == "yes"
  336. ),
  337. ]
  338. )
  339. def get_tidy_config_map(unit, map_path):
  340. config_map_path = unit.resolve(os.path.join("$S", map_path))
  341. config_map = {}
  342. try:
  343. with open(config_map_path, 'r') as afile:
  344. config_map = json.load(afile)
  345. except ValueError:
  346. ymake.report_configure_error("{} is invalid json".format(map_path))
  347. except Exception as e:
  348. ymake.report_configure_error(str(e))
  349. return config_map
  350. def get_default_tidy_config(unit):
  351. unit_path = _common.get_norm_unit_path(unit)
  352. tidy_default_config_map = get_tidy_config_map(unit, DEFAULT_TIDY_CONFIG_MAP_PATH)
  353. for project_prefix, config_path in tidy_default_config_map.items():
  354. if unit_path.startswith(project_prefix):
  355. return config_path
  356. return DEFAULT_TIDY_CONFIG
  357. ordered_tidy_map = None
  358. def get_project_tidy_config(unit):
  359. global ordered_tidy_map
  360. if ordered_tidy_map is None:
  361. ordered_tidy_map = list(reversed(sorted(get_tidy_config_map(unit, PROJECT_TIDY_CONFIG_MAP_PATH).items())))
  362. unit_path = _common.get_norm_unit_path(unit)
  363. for project_prefix, config_path in ordered_tidy_map:
  364. if unit_path.startswith(project_prefix):
  365. return config_path
  366. else:
  367. return get_default_tidy_config(unit)
  368. def onadd_ytest(unit, *args):
  369. keywords = {
  370. "DEPENDS": -1,
  371. "DATA": -1,
  372. "TIMEOUT": 1,
  373. "FORK_MODE": 1,
  374. "SPLIT_FACTOR": 1,
  375. "FORK_SUBTESTS": 0,
  376. "FORK_TESTS": 0,
  377. }
  378. flat_args, spec_args = _common.sort_by_keywords(keywords, args)
  379. is_implicit_data_needed = flat_args[1] in (
  380. "unittest.py",
  381. "gunittest",
  382. "g_benchmark",
  383. "go.test",
  384. "boost.test",
  385. "fuzz.test",
  386. )
  387. if is_implicit_data_needed and unit.get('ADD_SRCDIR_TO_TEST_DATA') == "yes":
  388. unit.ondata_files(_common.get_norm_unit_path(unit))
  389. if flat_args[1] == "fuzz.test":
  390. unit.ondata_files("fuzzing/{}/corpus.json".format(_common.get_norm_unit_path(unit)))
  391. if not flat_args[1] in ("unittest.py", "gunittest", "g_benchmark"):
  392. unit.ondata_files(get_unit_list_variable(unit, 'TEST_YT_SPEC_VALUE'))
  393. test_data = sorted(
  394. _common.filter_out_by_keyword(
  395. spec_args.get('DATA', []) + get_norm_paths(unit, 'TEST_DATA_VALUE'), 'AUTOUPDATED'
  396. )
  397. )
  398. if flat_args[1] == "go.test":
  399. data, _ = get_canonical_test_resources(unit)
  400. test_data += data
  401. elif flat_args[1] == "coverage.extractor" and not match_coverage_extractor_requirements(unit):
  402. # XXX
  403. # Current ymake implementation doesn't allow to call macro inside the 'when' body
  404. # that's why we add ADD_YTEST(coverage.extractor) to every PROGRAM entry and check requirements later
  405. return
  406. elif flat_args[1] == "clang_tidy" and unit.get("TIDY_ENABLED") != "yes":
  407. # Graph is not prepared
  408. return
  409. elif unit.get("TIDY") == "yes" and unit.get("TIDY_ENABLED") != "yes":
  410. # clang_tidy disabled for module
  411. return
  412. elif flat_args[1] == "no.test":
  413. return
  414. test_size = ''.join(spec_args.get('SIZE', [])) or unit.get('TEST_SIZE_NAME') or ''
  415. test_tags = serialize_list(sorted(_get_test_tags(unit, spec_args)))
  416. test_timeout = ''.join(spec_args.get('TIMEOUT', [])) or unit.get('TEST_TIMEOUT') or ''
  417. test_requirements = spec_args.get('REQUIREMENTS', []) + get_values_list(unit, 'TEST_REQUIREMENTS_VALUE')
  418. if flat_args[1] != "clang_tidy" and unit.get("TIDY_ENABLED") == "yes":
  419. # graph changed for clang_tidy tests
  420. if flat_args[1] in ("unittest.py", "gunittest", "g_benchmark", "boost.test"):
  421. flat_args[1] = "clang_tidy"
  422. test_size = 'SMALL'
  423. test_tags = ''
  424. test_timeout = "60"
  425. test_requirements = []
  426. unit.set(["TEST_YT_SPEC_VALUE", ""])
  427. else:
  428. return
  429. if flat_args[1] == "clang_tidy" and unit.get("TIDY_ENABLED") == "yes":
  430. if unit.get("TIDY_CONFIG"):
  431. default_config_path = unit.get("TIDY_CONFIG")
  432. project_config_path = unit.get("TIDY_CONFIG")
  433. else:
  434. default_config_path = get_default_tidy_config(unit)
  435. project_config_path = get_project_tidy_config(unit)
  436. unit.set(["DEFAULT_TIDY_CONFIG", default_config_path])
  437. unit.set(["PROJECT_TIDY_CONFIG", project_config_path])
  438. fork_mode = []
  439. if 'FORK_SUBTESTS' in spec_args:
  440. fork_mode.append('subtests')
  441. if 'FORK_TESTS' in spec_args:
  442. fork_mode.append('tests')
  443. fork_mode = fork_mode or spec_args.get('FORK_MODE', []) or unit.get('TEST_FORK_MODE').split()
  444. fork_mode = ' '.join(fork_mode) if fork_mode else ''
  445. unit_path = _common.get_norm_unit_path(unit)
  446. test_record = {
  447. 'TEST-NAME': flat_args[0],
  448. 'SCRIPT-REL-PATH': flat_args[1],
  449. 'TESTED-PROJECT-NAME': unit.name(),
  450. 'TESTED-PROJECT-FILENAME': unit.filename(),
  451. 'SOURCE-FOLDER-PATH': unit_path,
  452. # TODO get rid of BUILD-FOLDER-PATH
  453. 'BUILD-FOLDER-PATH': unit_path,
  454. 'BINARY-PATH': "{}/{}".format(unit_path, unit.filename()),
  455. 'GLOBAL-LIBRARY-PATH': unit.global_filename(),
  456. 'CUSTOM-DEPENDENCIES': ' '.join(spec_args.get('DEPENDS', []) + get_values_list(unit, 'TEST_DEPENDS_VALUE')),
  457. 'TEST-RECIPES': prepare_recipes(unit.get("TEST_RECIPES_VALUE")),
  458. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  459. # 'TEST-PRESERVE-ENV': 'da',
  460. 'TEST-DATA': serialize_list(sorted(test_data)),
  461. 'TEST-TIMEOUT': test_timeout,
  462. 'FORK-MODE': fork_mode,
  463. 'SPLIT-FACTOR': ''.join(spec_args.get('SPLIT_FACTOR', [])) or unit.get('TEST_SPLIT_FACTOR') or '',
  464. 'SIZE': test_size,
  465. 'TAG': test_tags,
  466. 'REQUIREMENTS': serialize_list(test_requirements),
  467. 'TEST-CWD': unit.get('TEST_CWD_VALUE') or '',
  468. 'FUZZ-DICTS': serialize_list(
  469. spec_args.get('FUZZ_DICTS', []) + get_unit_list_variable(unit, 'FUZZ_DICTS_VALUE')
  470. ),
  471. 'FUZZ-OPTS': serialize_list(spec_args.get('FUZZ_OPTS', []) + get_unit_list_variable(unit, 'FUZZ_OPTS_VALUE')),
  472. 'YT-SPEC': serialize_list(spec_args.get('YT_SPEC', []) + get_unit_list_variable(unit, 'TEST_YT_SPEC_VALUE')),
  473. 'BLOB': unit.get('TEST_BLOB_DATA') or '',
  474. 'SKIP_TEST': unit.get('SKIP_TEST_VALUE') or '',
  475. 'TEST_IOS_DEVICE_TYPE': unit.get('TEST_IOS_DEVICE_TYPE_VALUE') or '',
  476. 'TEST_IOS_RUNTIME_TYPE': unit.get('TEST_IOS_RUNTIME_TYPE_VALUE') or '',
  477. 'ANDROID_APK_TEST_ACTIVITY': unit.get('ANDROID_APK_TEST_ACTIVITY_VALUE') or '',
  478. 'TEST_PARTITION': unit.get("TEST_PARTITION") or 'SEQUENTIAL',
  479. 'GO_BENCH_TIMEOUT': unit.get('GO_BENCH_TIMEOUT') or '',
  480. }
  481. if flat_args[1] == "go.bench":
  482. if "ya:run_go_benchmark" not in test_record["TAG"]:
  483. return
  484. else:
  485. test_record["TEST-NAME"] += "_bench"
  486. elif flat_args[1] in ("g_benchmark", "y_benchmark"):
  487. benchmark_opts = get_unit_list_variable(unit, 'BENCHMARK_OPTS_VALUE')
  488. test_record['BENCHMARK-OPTS'] = serialize_list(benchmark_opts)
  489. elif flat_args[1] == 'fuzz.test' and unit.get('FUZZING') == 'yes':
  490. test_record['FUZZING'] = '1'
  491. # use all cores if fuzzing requested
  492. test_record['REQUIREMENTS'] = serialize_list(
  493. filter(None, deserialize_list(test_record['REQUIREMENTS']) + ["cpu:all", "ram:all"])
  494. )
  495. data = dump_test(unit, test_record)
  496. if data:
  497. unit.set_property(["DART_DATA", data])
  498. def java_srcdirs_to_data(unit, var):
  499. extra_data = []
  500. for srcdir in (unit.get(var) or '').replace('$' + var, '').split():
  501. if srcdir == '.':
  502. srcdir = unit.get('MODDIR')
  503. if srcdir.startswith('${ARCADIA_ROOT}/') or srcdir.startswith('$ARCADIA_ROOT/'):
  504. srcdir = srcdir.replace('${ARCADIA_ROOT}/', '$S/')
  505. srcdir = srcdir.replace('$ARCADIA_ROOT/', '$S/')
  506. if srcdir.startswith('${CURDIR}') or srcdir.startswith('$CURDIR'):
  507. srcdir = srcdir.replace('${CURDIR}', os.path.join('$S', unit.get('MODDIR')))
  508. srcdir = srcdir.replace('$CURDIR', os.path.join('$S', unit.get('MODDIR')))
  509. srcdir = unit.resolve_arc_path(srcdir)
  510. if not srcdir.startswith('$'):
  511. srcdir = os.path.join('$S', unit.get('MODDIR'), srcdir)
  512. if srcdir.startswith('$S'):
  513. extra_data.append(srcdir.replace('$S', 'arcadia'))
  514. return serialize_list(extra_data)
  515. def onadd_check(unit, *args):
  516. if unit.get("TIDY") == "yes":
  517. # graph changed for clang_tidy tests
  518. return
  519. flat_args, spec_args = _common.sort_by_keywords(
  520. {
  521. "DEPENDS": -1,
  522. "TIMEOUT": 1,
  523. "DATA": -1,
  524. "TAG": -1,
  525. "REQUIREMENTS": -1,
  526. "FORK_MODE": 1,
  527. "SPLIT_FACTOR": 1,
  528. "FORK_SUBTESTS": 0,
  529. "FORK_TESTS": 0,
  530. "SIZE": 1,
  531. },
  532. args,
  533. )
  534. check_type = flat_args[0]
  535. if check_type in ("check.data", "check.resource") and unit.get('VALIDATE_DATA') == "no":
  536. return
  537. if check_type == "check.external" and (len(flat_args) == 1 or not flat_args[1]):
  538. return
  539. test_dir = _common.get_norm_unit_path(unit)
  540. test_timeout = ''
  541. fork_mode = ''
  542. extra_test_data = ''
  543. extra_test_dart_data = {}
  544. ymake_java_test = unit.get('YMAKE_JAVA_TEST') == 'yes'
  545. use_arcadia_python = unit.get('USE_ARCADIA_PYTHON')
  546. uid_ext = ''
  547. script_rel_path = check_type
  548. test_files = flat_args[1:]
  549. if check_type in ["check.data", "check.resource"]:
  550. uid_ext = unit.get("SBR_UID_EXT").split(" ", 1)[-1] # strip variable name
  551. if check_type in ["flake8.py2", "flake8.py3", "black"]:
  552. fork_mode = unit.get('TEST_FORK_MODE') or ''
  553. elif check_type == "ktlint":
  554. test_timeout = '120'
  555. if unit.get('_USE_KTLINT_OLD') == 'yes':
  556. extra_test_data = serialize_list([KTLINT_OLD_EDITOR_CONFIG])
  557. extra_test_dart_data['KTLINT_BINARY'] = '$(KTLINT_OLD)/run.bat'
  558. extra_test_dart_data['USE_KTLINT_OLD'] = 'yes'
  559. else:
  560. data_list = [KTLINT_CURRENT_EDITOR_CONFIG]
  561. baseline_path_relative = unit.get('_KTLINT_BASELINE_FILE')
  562. if baseline_path_relative:
  563. baseline_path = unit.resolve_arc_path(baseline_path_relative).replace('$S', 'arcadia')
  564. data_list += [baseline_path]
  565. extra_test_dart_data['KTLINT_BASELINE_FILE'] = baseline_path_relative
  566. extra_test_data = serialize_list(data_list)
  567. extra_test_dart_data['KTLINT_BINARY'] = '$(KTLINT)/run.bat'
  568. elif check_type == "JAVA_STYLE":
  569. if ymake_java_test and not unit.get('ALL_SRCDIRS'):
  570. return
  571. if len(flat_args) < 2:
  572. raise Exception("Not enough arguments for JAVA_STYLE check")
  573. check_level = flat_args[1]
  574. allowed_levels = {
  575. 'base': '/yandex_checks.xml',
  576. 'strict': '/yandex_checks_strict.xml',
  577. 'extended': '/yandex_checks_extended.xml',
  578. 'library': '/yandex_checks_library.xml',
  579. }
  580. if check_level not in allowed_levels:
  581. raise Exception("'{}' is not allowed in LINT(), use one of {}".format(check_level, allowed_levels.keys()))
  582. test_files[0] = allowed_levels[check_level] # replace check_level with path to config file
  583. script_rel_path = "java.style"
  584. test_timeout = '240'
  585. fork_mode = unit.get('TEST_FORK_MODE') or ''
  586. if ymake_java_test:
  587. extra_test_data = java_srcdirs_to_data(unit, 'ALL_SRCDIRS')
  588. # jstyle should use the latest jdk
  589. unit.onpeerdir([unit.get('JDK_LATEST_PEERDIR')])
  590. extra_test_dart_data['JDK_LATEST_VERSION'] = unit.get('JDK_LATEST_VERSION')
  591. # TODO remove when ya-bin will be released (https://st.yandex-team.ru/DEVTOOLS-9611)
  592. extra_test_dart_data['JDK_RESOURCE'] = 'JDK' + (
  593. unit.get('JDK_VERSION') or unit.get('JDK_REAL_VERSION') or '_DEFAULT'
  594. )
  595. elif check_type == "gofmt":
  596. if test_files:
  597. test_dir = os.path.dirname(test_files[0]).lstrip("$S/")
  598. elif check_type == "check.data":
  599. data_re = re.compile(r"sbr:/?/?(\d+)=?.*")
  600. data = flat_args[1:]
  601. resources = []
  602. for f in data:
  603. matched = re.match(data_re, f)
  604. if matched:
  605. resources.append(matched.group(1))
  606. if resources:
  607. test_files = resources
  608. else:
  609. return
  610. serialized_test_files = serialize_list(test_files)
  611. test_record = {
  612. 'TEST-NAME': check_type.lower(),
  613. 'TEST-TIMEOUT': test_timeout,
  614. 'SCRIPT-REL-PATH': script_rel_path,
  615. 'TESTED-PROJECT-NAME': os.path.basename(test_dir),
  616. 'SOURCE-FOLDER-PATH': test_dir,
  617. 'CUSTOM-DEPENDENCIES': " ".join(spec_args.get('DEPENDS', [])),
  618. 'TEST-DATA': extra_test_data,
  619. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  620. 'SBR-UID-EXT': uid_ext,
  621. 'SPLIT-FACTOR': '',
  622. 'TEST_PARTITION': 'SEQUENTIAL',
  623. 'FORK-MODE': fork_mode,
  624. 'FORK-TEST-FILES': '',
  625. 'SIZE': 'SMALL',
  626. 'TAG': '',
  627. 'REQUIREMENTS': " ".join(spec_args.get('REQUIREMENTS', [])),
  628. 'USE_ARCADIA_PYTHON': use_arcadia_python or '',
  629. 'OLD_PYTEST': 'no',
  630. 'PYTHON-PATHS': '',
  631. # TODO remove FILES, see DEVTOOLS-7052
  632. 'FILES': serialized_test_files,
  633. 'TEST-FILES': serialized_test_files,
  634. }
  635. test_record.update(extra_test_dart_data)
  636. data = dump_test(unit, test_record)
  637. if data:
  638. unit.set_property(["DART_DATA", data])
  639. def on_register_no_check_imports(unit):
  640. s = unit.get('NO_CHECK_IMPORTS_FOR_VALUE')
  641. if s not in ('', 'None'):
  642. unit.onresource(['-', 'py/no_check_imports/{}="{}"'.format(_common.pathid(s), s)])
  643. def onadd_check_py_imports(unit, *args):
  644. if unit.get("TIDY") == "yes":
  645. # graph changed for clang_tidy tests
  646. return
  647. if unit.get('NO_CHECK_IMPORTS_FOR_VALUE').strip() == "":
  648. return
  649. unit.onpeerdir(['library/python/testing/import_test'])
  650. check_type = "py.imports"
  651. test_dir = _common.get_norm_unit_path(unit)
  652. use_arcadia_python = unit.get('USE_ARCADIA_PYTHON')
  653. test_files = serialize_list([_common.get_norm_unit_path(unit, unit.filename())])
  654. test_record = {
  655. 'TEST-NAME': "pyimports",
  656. 'TEST-TIMEOUT': '',
  657. 'SCRIPT-REL-PATH': check_type,
  658. 'TESTED-PROJECT-NAME': os.path.basename(test_dir),
  659. 'SOURCE-FOLDER-PATH': test_dir,
  660. 'CUSTOM-DEPENDENCIES': '',
  661. 'TEST-DATA': '',
  662. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  663. 'SPLIT-FACTOR': '',
  664. 'TEST_PARTITION': 'SEQUENTIAL',
  665. 'FORK-MODE': '',
  666. 'FORK-TEST-FILES': '',
  667. 'SIZE': 'SMALL',
  668. 'TAG': '',
  669. 'USE_ARCADIA_PYTHON': use_arcadia_python or '',
  670. 'OLD_PYTEST': 'no',
  671. 'PYTHON-PATHS': '',
  672. # TODO remove FILES, see DEVTOOLS-7052
  673. 'FILES': test_files,
  674. 'TEST-FILES': test_files,
  675. }
  676. if unit.get('NO_CHECK_IMPORTS_FOR_VALUE') != "None":
  677. test_record["NO-CHECK"] = serialize_list(get_values_list(unit, 'NO_CHECK_IMPORTS_FOR_VALUE') or ["*"])
  678. else:
  679. test_record["NO-CHECK"] = ''
  680. data = dump_test(unit, test_record)
  681. if data:
  682. unit.set_property(["DART_DATA", data])
  683. def onadd_pytest_script(unit, *args):
  684. if unit.get("TIDY") == "yes":
  685. # graph changed for clang_tidy tests
  686. return
  687. unit.set(["PYTEST_BIN", "no"])
  688. custom_deps = get_values_list(unit, 'TEST_DEPENDS_VALUE')
  689. timeout = list(filter(None, [unit.get(["TEST_TIMEOUT"])]))
  690. if unit.get('ADD_SRCDIR_TO_TEST_DATA') == "yes":
  691. unit.ondata_files(_common.get_norm_unit_path(unit))
  692. if timeout:
  693. timeout = timeout[0]
  694. else:
  695. timeout = '0'
  696. test_type = args[0]
  697. fork_mode = unit.get('TEST_FORK_MODE').split() or ''
  698. split_factor = unit.get('TEST_SPLIT_FACTOR') or ''
  699. test_size = unit.get('TEST_SIZE_NAME') or ''
  700. test_files = get_values_list(unit, 'TEST_SRCS_VALUE')
  701. tags = _get_test_tags(unit)
  702. requirements = get_values_list(unit, 'TEST_REQUIREMENTS_VALUE')
  703. test_data = get_norm_paths(unit, 'TEST_DATA_VALUE')
  704. data, data_files = get_canonical_test_resources(unit)
  705. test_data += data
  706. python_paths = get_values_list(unit, 'TEST_PYTHON_PATH_VALUE')
  707. binary_path = os.path.join(_common.get_norm_unit_path(unit), unit.filename())
  708. test_cwd = unit.get('TEST_CWD_VALUE') or ''
  709. _dump_test(
  710. unit,
  711. test_type,
  712. test_files,
  713. timeout,
  714. _common.get_norm_unit_path(unit),
  715. custom_deps,
  716. test_data,
  717. python_paths,
  718. split_factor,
  719. fork_mode,
  720. test_size,
  721. tags,
  722. requirements,
  723. binary_path,
  724. test_cwd=test_cwd,
  725. data_files=data_files,
  726. )
  727. def onadd_pytest_bin(unit, *args):
  728. if unit.get("TIDY") == "yes":
  729. # graph changed for clang_tidy tests
  730. return
  731. flat, kws = _common.sort_by_keywords({'RUNNER_BIN': 1}, args)
  732. if flat:
  733. ymake.report_configure_error('Unknown arguments found while processing add_pytest_bin macro: {!r}'.format(flat))
  734. runner_bin = kws.get('RUNNER_BIN', [None])[0]
  735. test_type = 'py3test.bin' if (unit.get("PYTHON3") == 'yes') else "pytest.bin"
  736. add_test_to_dart(unit, test_type, runner_bin=runner_bin)
  737. def add_test_to_dart(unit, test_type, binary_path=None, runner_bin=None):
  738. if unit.get("TIDY") == "yes":
  739. # graph changed for clang_tidy tests
  740. return
  741. if unit.get('ADD_SRCDIR_TO_TEST_DATA') == "yes":
  742. unit.ondata_files(_common.get_norm_unit_path(unit))
  743. custom_deps = get_values_list(unit, 'TEST_DEPENDS_VALUE')
  744. timeout = list(filter(None, [unit.get(["TEST_TIMEOUT"])]))
  745. if timeout:
  746. timeout = timeout[0]
  747. else:
  748. timeout = '0'
  749. fork_mode = unit.get('TEST_FORK_MODE').split() or ''
  750. split_factor = unit.get('TEST_SPLIT_FACTOR') or ''
  751. test_size = unit.get('TEST_SIZE_NAME') or ''
  752. test_cwd = unit.get('TEST_CWD_VALUE') or ''
  753. yt_spec = get_values_list(unit, 'TEST_YT_SPEC_VALUE')
  754. unit.ondata_files(yt_spec)
  755. unit_path = unit.path()
  756. test_files = get_values_list(unit, 'TEST_SRCS_VALUE')
  757. tags = _get_test_tags(unit)
  758. requirements = get_values_list(unit, 'TEST_REQUIREMENTS_VALUE')
  759. test_data = get_norm_paths(unit, 'TEST_DATA_VALUE')
  760. data, data_files = get_canonical_test_resources(unit)
  761. test_data += data
  762. python_paths = get_values_list(unit, 'TEST_PYTHON_PATH_VALUE')
  763. if not binary_path:
  764. binary_path = os.path.join(unit_path, unit.filename())
  765. _dump_test(
  766. unit,
  767. test_type,
  768. test_files,
  769. timeout,
  770. _common.get_norm_unit_path(unit),
  771. custom_deps,
  772. test_data,
  773. python_paths,
  774. split_factor,
  775. fork_mode,
  776. test_size,
  777. tags,
  778. requirements,
  779. binary_path,
  780. test_cwd=test_cwd,
  781. runner_bin=runner_bin,
  782. yt_spec=yt_spec,
  783. data_files=data_files,
  784. )
  785. def extract_java_system_properties(unit, args):
  786. if len(args) % 2:
  787. return [], 'Wrong use of SYSTEM_PROPERTIES in {}: odd number of arguments'.format(unit.path())
  788. props = []
  789. for x, y in zip(args[::2], args[1::2]):
  790. if x == 'FILE':
  791. if y.startswith('${BINDIR}') or y.startswith('${ARCADIA_BUILD_ROOT}') or y.startswith('/'):
  792. return [], 'Wrong use of SYSTEM_PROPERTIES in {}: absolute/build file path {}'.format(unit.path(), y)
  793. y = _common.rootrel_arc_src(y, unit)
  794. if not os.path.exists(unit.resolve('$S/' + y)):
  795. return [], 'Wrong use of SYSTEM_PROPERTIES in {}: can\'t resolve {}'.format(unit.path(), y)
  796. y = '${ARCADIA_ROOT}/' + y
  797. props.append({'type': 'file', 'path': y})
  798. else:
  799. props.append({'type': 'inline', 'key': x, 'value': y})
  800. return props, None
  801. def onjava_test(unit, *args):
  802. if unit.get("TIDY") == "yes":
  803. # graph changed for clang_tidy tests
  804. return
  805. assert unit.get('MODULE_TYPE') is not None
  806. if unit.get('MODULE_TYPE') == 'JTEST_FOR':
  807. if not unit.get('UNITTEST_DIR'):
  808. ymake.report_configure_error('skip JTEST_FOR in {}: no args provided'.format(unit.path()))
  809. return
  810. java_cp_arg_type = unit.get('JAVA_CLASSPATH_CMD_TYPE_VALUE') or 'MANIFEST'
  811. if java_cp_arg_type not in ('MANIFEST', 'COMMAND_FILE', 'LIST'):
  812. ymake.report_configure_error(
  813. '{}: TEST_JAVA_CLASSPATH_CMD_TYPE({}) are invalid. Choose argument from MANIFEST, COMMAND_FILE or LIST)'.format(
  814. unit.path(), java_cp_arg_type
  815. )
  816. )
  817. return
  818. unit_path = unit.path()
  819. path = _common.strip_roots(unit_path)
  820. if unit.get('ADD_SRCDIR_TO_TEST_DATA') == "yes":
  821. unit.ondata_files(_common.get_norm_unit_path(unit))
  822. yt_spec_values = get_unit_list_variable(unit, 'TEST_YT_SPEC_VALUE')
  823. unit.ondata_files(yt_spec_values)
  824. test_data = get_norm_paths(unit, 'TEST_DATA_VALUE')
  825. test_data.append('arcadia/build/scripts/run_junit.py')
  826. test_data.append('arcadia/build/scripts/unpacking_jtest_runner.py')
  827. data, data_files = get_canonical_test_resources(unit)
  828. test_data += data
  829. props, error_mgs = extract_java_system_properties(unit, get_values_list(unit, 'SYSTEM_PROPERTIES_VALUE'))
  830. if error_mgs:
  831. ymake.report_configure_error(error_mgs)
  832. return
  833. for prop in props:
  834. if prop['type'] == 'file':
  835. test_data.append(prop['path'].replace('${ARCADIA_ROOT}', 'arcadia'))
  836. props = base64.b64encode(six.ensure_binary(json.dumps(props)))
  837. test_cwd = unit.get('TEST_CWD_VALUE') or '' # TODO: validate test_cwd value
  838. if unit.get('MODULE_TYPE') == 'JUNIT5':
  839. script_rel_path = 'junit5.test'
  840. else:
  841. script_rel_path = 'junit.test'
  842. ymake_java_test = unit.get('YMAKE_JAVA_TEST') == 'yes'
  843. test_record = {
  844. 'SOURCE-FOLDER-PATH': path,
  845. 'TEST-NAME': '-'.join([os.path.basename(os.path.dirname(path)), os.path.basename(path)]),
  846. 'SCRIPT-REL-PATH': script_rel_path,
  847. 'TEST-TIMEOUT': unit.get('TEST_TIMEOUT') or '',
  848. 'TESTED-PROJECT-NAME': path,
  849. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  850. # 'TEST-PRESERVE-ENV': 'da',
  851. 'TEST-DATA': serialize_list(sorted(_common.filter_out_by_keyword(test_data, 'AUTOUPDATED'))),
  852. 'FORK-MODE': unit.get('TEST_FORK_MODE') or '',
  853. 'SPLIT-FACTOR': unit.get('TEST_SPLIT_FACTOR') or '',
  854. 'CUSTOM-DEPENDENCIES': ' '.join(get_values_list(unit, 'TEST_DEPENDS_VALUE')),
  855. 'TAG': serialize_list(sorted(_get_test_tags(unit))),
  856. 'SIZE': unit.get('TEST_SIZE_NAME') or '',
  857. 'REQUIREMENTS': serialize_list(get_values_list(unit, 'TEST_REQUIREMENTS_VALUE')),
  858. 'TEST-RECIPES': prepare_recipes(unit.get("TEST_RECIPES_VALUE")),
  859. # JTEST/JTEST_FOR only
  860. 'MODULE_TYPE': unit.get('MODULE_TYPE'),
  861. 'UNITTEST_DIR': unit.get('UNITTEST_DIR') or '',
  862. 'JVM_ARGS': serialize_list(get_values_list(unit, 'JVM_ARGS_VALUE')),
  863. 'SYSTEM_PROPERTIES': props,
  864. 'TEST-CWD': test_cwd,
  865. 'SKIP_TEST': unit.get('SKIP_TEST_VALUE') or '',
  866. 'JAVA_CLASSPATH_CMD_TYPE': java_cp_arg_type,
  867. 'JDK_RESOURCE': 'JDK' + (unit.get('JDK_VERSION') or unit.get('JDK_REAL_VERSION') or '_DEFAULT'),
  868. 'JDK_FOR_TESTS': 'JDK' + (unit.get('JDK_VERSION') or unit.get('JDK_REAL_VERSION') or '_DEFAULT') + '_FOR_TESTS',
  869. 'YT-SPEC': serialize_list(yt_spec_values),
  870. }
  871. test_classpath_origins = unit.get('TEST_CLASSPATH_VALUE')
  872. if test_classpath_origins:
  873. test_record['TEST_CLASSPATH_ORIGINS'] = test_classpath_origins
  874. test_record['TEST_CLASSPATH'] = '${TEST_CLASSPATH_MANAGED}'
  875. elif ymake_java_test:
  876. test_record['TEST_CLASSPATH'] = '${DART_CLASSPATH}'
  877. test_record['TEST_CLASSPATH_DEPS'] = '${DART_CLASSPATH_DEPS}'
  878. if unit.get('UNITTEST_DIR'):
  879. test_record['TEST_JAR'] = '${UNITTEST_MOD}'
  880. else:
  881. test_record['TEST_JAR'] = '{}/{}.jar'.format(unit.get('MODDIR'), unit.get('REALPRJNAME'))
  882. data = dump_test(unit, test_record)
  883. if data:
  884. unit.set_property(['DART_DATA', data])
  885. def onjava_test_deps(unit, *args):
  886. if unit.get("TIDY") == "yes":
  887. # graph changed for clang_tidy tests
  888. return
  889. assert unit.get('MODULE_TYPE') is not None
  890. assert len(args) == 1
  891. mode = args[0]
  892. path = _common.get_norm_unit_path(unit)
  893. ymake_java_test = unit.get('YMAKE_JAVA_TEST') == 'yes'
  894. test_record = {
  895. 'SOURCE-FOLDER-PATH': path,
  896. 'TEST-NAME': '-'.join([os.path.basename(os.path.dirname(path)), os.path.basename(path), 'dependencies']).strip(
  897. '-'
  898. ),
  899. 'SCRIPT-REL-PATH': 'java.dependency.test',
  900. 'TEST-TIMEOUT': '',
  901. 'TESTED-PROJECT-NAME': path,
  902. 'TEST-DATA': '',
  903. 'TEST_PARTITION': 'SEQUENTIAL',
  904. 'FORK-MODE': '',
  905. 'SPLIT-FACTOR': '',
  906. 'CUSTOM-DEPENDENCIES': ' '.join(get_values_list(unit, 'TEST_DEPENDS_VALUE')),
  907. 'TAG': '',
  908. 'SIZE': 'SMALL',
  909. 'IGNORE_CLASSPATH_CLASH': ' '.join(get_values_list(unit, 'JAVA_IGNORE_CLASSPATH_CLASH_VALUE')),
  910. # JTEST/JTEST_FOR only
  911. 'MODULE_TYPE': unit.get('MODULE_TYPE'),
  912. 'UNITTEST_DIR': '',
  913. 'SYSTEM_PROPERTIES': '',
  914. 'TEST-CWD': '',
  915. }
  916. if mode == 'strict':
  917. test_record['STRICT_CLASSPATH_CLASH'] = 'yes'
  918. if ymake_java_test:
  919. test_record['CLASSPATH'] = '$B/{}/{}.jar ${{DART_CLASSPATH}}'.format(
  920. unit.get('MODDIR'), unit.get('REALPRJNAME')
  921. )
  922. data = dump_test(unit, test_record)
  923. unit.set_property(['DART_DATA', data])
  924. def _get_test_tags(unit, spec_args=None):
  925. if spec_args is None:
  926. spec_args = {}
  927. tags = spec_args.get('TAG', []) + get_values_list(unit, 'TEST_TAGS_VALUE')
  928. tags = set(tags)
  929. if unit.get('EXPORT_SEM') == 'yes':
  930. filter_only_tags = sorted(t for t in tags if ':' not in t)
  931. unit.set(['FILTER_ONLY_TEST_TAGS', ' '.join(filter_only_tags)])
  932. # DEVTOOLS-7571
  933. if unit.get('SKIP_TEST_VALUE') and consts.YaTestTags.Fat in tags:
  934. tags.add(consts.YaTestTags.NotAutocheck)
  935. return tags
  936. def _dump_test(
  937. unit,
  938. test_type,
  939. test_files,
  940. timeout,
  941. test_dir,
  942. custom_deps,
  943. test_data,
  944. python_paths,
  945. split_factor,
  946. fork_mode,
  947. test_size,
  948. tags,
  949. requirements,
  950. binary_path='',
  951. old_pytest=False,
  952. test_cwd=None,
  953. runner_bin=None,
  954. yt_spec=None,
  955. data_files=None,
  956. ):
  957. if test_type == "PY_TEST":
  958. script_rel_path = "py.test"
  959. else:
  960. script_rel_path = test_type
  961. unit_path = unit.path()
  962. fork_test_files = unit.get('FORK_TEST_FILES_MODE')
  963. fork_mode = ' '.join(fork_mode) if fork_mode else ''
  964. use_arcadia_python = unit.get('USE_ARCADIA_PYTHON')
  965. if test_cwd:
  966. test_cwd = test_cwd.replace("$TEST_CWD_VALUE", "").replace('"MACRO_CALLS_DELIM"', "").strip()
  967. test_name = os.path.basename(binary_path)
  968. test_record = {
  969. 'TEST-NAME': os.path.splitext(test_name)[0],
  970. 'TEST-TIMEOUT': timeout,
  971. 'SCRIPT-REL-PATH': script_rel_path,
  972. 'TESTED-PROJECT-NAME': test_name,
  973. 'SOURCE-FOLDER-PATH': test_dir,
  974. 'CUSTOM-DEPENDENCIES': " ".join(custom_deps),
  975. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  976. # 'TEST-PRESERVE-ENV': 'da',
  977. 'TEST-DATA': serialize_list(sorted(_common.filter_out_by_keyword(test_data, 'AUTOUPDATED'))),
  978. 'TEST-RECIPES': prepare_recipes(unit.get("TEST_RECIPES_VALUE")),
  979. 'SPLIT-FACTOR': split_factor,
  980. 'TEST_PARTITION': unit.get('TEST_PARTITION') or 'SEQUENTIAL',
  981. 'FORK-MODE': fork_mode,
  982. 'FORK-TEST-FILES': fork_test_files,
  983. 'TEST-FILES': serialize_list(test_files),
  984. 'SIZE': test_size,
  985. 'TAG': serialize_list(sorted(tags)),
  986. 'REQUIREMENTS': serialize_list(requirements),
  987. 'USE_ARCADIA_PYTHON': use_arcadia_python or '',
  988. 'OLD_PYTEST': 'yes' if old_pytest else 'no',
  989. 'PYTHON-PATHS': serialize_list(python_paths),
  990. 'TEST-CWD': test_cwd or '',
  991. 'SKIP_TEST': unit.get('SKIP_TEST_VALUE') or '',
  992. 'BUILD-FOLDER-PATH': _common.strip_roots(unit_path),
  993. 'BLOB': unit.get('TEST_BLOB_DATA') or '',
  994. 'CANONIZE_SUB_PATH': unit.get('CANONIZE_SUB_PATH') or '',
  995. }
  996. if binary_path:
  997. test_record['BINARY-PATH'] = _common.strip_roots(binary_path)
  998. if runner_bin:
  999. test_record['TEST-RUNNER-BIN'] = runner_bin
  1000. if yt_spec:
  1001. test_record['YT-SPEC'] = serialize_list(yt_spec)
  1002. data = dump_test(unit, test_record)
  1003. if data:
  1004. unit.set_property(["DART_DATA", data])
  1005. def onsetup_pytest_bin(unit, *args):
  1006. use_arcadia_python = unit.get('USE_ARCADIA_PYTHON') == "yes"
  1007. if use_arcadia_python:
  1008. unit.onresource(['-', 'PY_MAIN={}'.format("library.python.pytest.main:main")]) # XXX
  1009. unit.onadd_pytest_bin(list(args))
  1010. else:
  1011. unit.onno_platform()
  1012. unit.onadd_pytest_script(["PY_TEST"])
  1013. def onrun(unit, *args):
  1014. exectest_cmd = unit.get(["EXECTEST_COMMAND_VALUE"]) or ''
  1015. exectest_cmd += "\n" + subprocess.list2cmdline(args)
  1016. unit.set(["EXECTEST_COMMAND_VALUE", exectest_cmd])
  1017. def onsetup_exectest(unit, *args):
  1018. command = unit.get(["EXECTEST_COMMAND_VALUE"])
  1019. if command is None:
  1020. ymake.report_configure_error("EXECTEST must have at least one RUN macro")
  1021. return
  1022. command = command.replace("$EXECTEST_COMMAND_VALUE", "")
  1023. if "PYTHON_BIN" in command:
  1024. unit.ondepends('contrib/tools/python')
  1025. unit.set(["TEST_BLOB_DATA", base64.b64encode(six.ensure_binary(command))])
  1026. add_test_to_dart(unit, "exectest", binary_path=os.path.join(unit.path(), unit.filename()).replace(".pkg", ""))
  1027. def onsetup_run_python(unit):
  1028. if unit.get("USE_ARCADIA_PYTHON") == "yes":
  1029. unit.ondepends('contrib/tools/python')
  1030. def get_canonical_test_resources(unit):
  1031. unit_path = unit.path()
  1032. if unit.get("CUSTOM_CANONDATA_PATH"):
  1033. path_to_canondata = unit_path.replace("$S", unit.get("CUSTOM_CANONDATA_PATH"))
  1034. else:
  1035. path_to_canondata = unit.resolve(unit_path)
  1036. canon_data_dir = os.path.join(path_to_canondata, CANON_DATA_DIR_NAME, unit.get('CANONIZE_SUB_PATH') or '')
  1037. try:
  1038. _, dirs, files = next(os.walk(canon_data_dir))
  1039. except StopIteration:
  1040. # path doesn't exist
  1041. return [], []
  1042. if CANON_RESULT_FILE_NAME in files:
  1043. return _get_canonical_data_resources_v2(os.path.join(canon_data_dir, CANON_RESULT_FILE_NAME), unit_path)
  1044. return [], []
  1045. def _load_canonical_file(filename, unit_path):
  1046. try:
  1047. with open(filename, 'rb') as results_file:
  1048. return json.load(results_file)
  1049. except Exception as e:
  1050. print("malformed canonical data in {}: {} ({})".format(unit_path, e, filename), file=sys.stderr)
  1051. return {}
  1052. def _get_resource_from_uri(uri):
  1053. m = consts.CANON_MDS_RESOURCE_REGEX.match(uri)
  1054. if m:
  1055. key = m.group(1)
  1056. return "{}:{}".format(consts.MDS_SCHEME, key)
  1057. m = consts.CANON_BACKEND_RESOURCE_REGEX.match(uri)
  1058. if m:
  1059. key = m.group(1)
  1060. return "{}:{}".format(consts.MDS_SCHEME, key)
  1061. m = consts.CANON_SBR_RESOURCE_REGEX.match(uri)
  1062. if m:
  1063. # There might be conflict between resources, because all resources in sandbox have 'resource.tar.gz' name
  1064. # That's why we use notation with '=' to specify specific path for resource
  1065. uri = m.group(1)
  1066. res_id = m.group(2)
  1067. return "{}={}".format(uri, '/'.join([CANON_OUTPUT_STORAGE, res_id]))
  1068. def _get_external_resources_from_canon_data(data):
  1069. # Method should work with both canonization versions:
  1070. # result.json: {'uri':X 'checksum':Y}
  1071. # result.json: {'testname': {'uri':X 'checksum':Y}}
  1072. # result.json: {'testname': [{'uri':X 'checksum':Y}]}
  1073. # Also there is a bug - if user returns {'uri': 1} from test - machinery will fail
  1074. # That's why we check 'uri' and 'checksum' fields presence
  1075. # (it's still a bug - user can return {'uri':X, 'checksum': Y}, we need to unify canonization format)
  1076. res = set()
  1077. if isinstance(data, dict):
  1078. if 'uri' in data and 'checksum' in data:
  1079. resource = _get_resource_from_uri(data['uri'])
  1080. if resource:
  1081. res.add(resource)
  1082. else:
  1083. for k, v in six.iteritems(data):
  1084. res.update(_get_external_resources_from_canon_data(v))
  1085. elif isinstance(data, list):
  1086. for e in data:
  1087. res.update(_get_external_resources_from_canon_data(e))
  1088. return res
  1089. def _get_canonical_data_resources_v2(filename, unit_path):
  1090. return (_get_external_resources_from_canon_data(_load_canonical_file(filename, unit_path)), [filename])
  1091. def on_add_linter_check(unit, *args):
  1092. if unit.get("TIDY") == "yes":
  1093. return
  1094. source_root_from_prefix = '${ARCADIA_ROOT}/'
  1095. source_root_to_prefix = '$S/'
  1096. unlimited = -1
  1097. no_lint_value = _common.get_no_lint_value(unit)
  1098. if no_lint_value in ("none", "none_internal"):
  1099. return
  1100. keywords = {
  1101. "DEPENDS": unlimited,
  1102. "FILES": unlimited,
  1103. "CONFIGS": unlimited,
  1104. "GLOBAL_RESOURCES": unlimited,
  1105. "FILE_PROCESSING_TIME": 1,
  1106. "EXTRA_PARAMS": unlimited,
  1107. }
  1108. flat_args, spec_args = _common.sort_by_keywords(keywords, args)
  1109. if len(flat_args) != 2:
  1110. unit.message(['ERROR', '_ADD_LINTER_CHECK params: expected 2 free parameters'])
  1111. return
  1112. configs = []
  1113. for cfg in spec_args.get('CONFIGS', []):
  1114. filename = unit.resolve(source_root_to_prefix + cfg)
  1115. if not os.path.exists(filename):
  1116. unit.message(['ERROR', 'Configuration file {} is not found'.format(filename)])
  1117. return
  1118. configs.append(cfg)
  1119. deps = []
  1120. lint_name, linter = flat_args
  1121. deps.append(os.path.dirname(linter))
  1122. test_files = []
  1123. for path in spec_args.get('FILES', []):
  1124. if path.startswith(source_root_from_prefix):
  1125. test_files.append(path.replace(source_root_from_prefix, source_root_to_prefix, 1))
  1126. elif path.startswith(source_root_to_prefix):
  1127. test_files.append(path)
  1128. if not test_files:
  1129. unit.message(['WARN', 'No files to lint for {}'.format(lint_name)])
  1130. return
  1131. for arg in spec_args.get('EXTRA_PARAMS', []):
  1132. if '=' not in arg:
  1133. unit.message(['WARN', 'Wrong EXTRA_PARAMS value: "{}". Values must have format "name=value".'.format(arg)])
  1134. return
  1135. deps += spec_args.get('DEPENDS', [])
  1136. for dep in deps:
  1137. unit.ondepends(dep)
  1138. for resource in spec_args.get('GLOBAL_RESOURCES', []):
  1139. unit.onpeerdir(resource)
  1140. test_record = {
  1141. 'TEST-NAME': lint_name,
  1142. 'SCRIPT-REL-PATH': 'custom_lint',
  1143. 'TESTED-PROJECT-NAME': unit.name(),
  1144. 'SOURCE-FOLDER-PATH': _common.get_norm_unit_path(unit),
  1145. 'CUSTOM-DEPENDENCIES': " ".join(deps),
  1146. 'TEST-DATA': '',
  1147. 'TEST-ENV': prepare_env(unit.get("TEST_ENV_VALUE")),
  1148. 'TEST-TIMEOUT': '',
  1149. 'SPLIT-FACTOR': '',
  1150. 'TEST_PARTITION': 'SEQUENTIAL',
  1151. 'FORK-MODE': '',
  1152. 'FORK-TEST-FILES': '',
  1153. 'SIZE': 'SMALL',
  1154. 'TAG': '',
  1155. 'USE_ARCADIA_PYTHON': unit.get('USE_ARCADIA_PYTHON') or '',
  1156. 'OLD_PYTEST': 'no',
  1157. 'PYTHON-PATHS': '',
  1158. # TODO remove FILES, see DEVTOOLS-7052
  1159. 'FILES': serialize_list(test_files),
  1160. 'TEST-FILES': serialize_list(test_files),
  1161. # Linter specific parameters
  1162. # TODO Add configs to DATA. See YMAKE-427
  1163. 'LINT-CONFIGS': serialize_list(configs),
  1164. 'LINT-NAME': lint_name,
  1165. 'LINT-FILE-PROCESSING-TIME': spec_args.get('FILE_PROCESSING_TIME', [''])[0],
  1166. 'LINT-EXTRA-PARAMS': serialize_list(spec_args.get('EXTRA_PARAMS', [])),
  1167. 'LINTER': linter,
  1168. }
  1169. data = dump_test(unit, test_record)
  1170. if data:
  1171. unit.set_property(["DART_DATA", data])