test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import os
  2. import os.path
  3. import glob
  4. import codecs
  5. import shutil
  6. import pytest
  7. import yql_utils
  8. from yqlrun import YQLRun
  9. import yatest.common
  10. project_path = yatest.common.context.project_path
  11. SOURCE_PATH = yql_utils.yql_source_path((project_path + '/cases').replace('\\', '/'))
  12. DATA_PATH = yatest.common.output_path('cases')
  13. ASTDIFF_PATH = yql_utils.yql_binary_path(os.getenv('YQL_ASTDIFF_PATH') or 'yql/essentials/tools/astdiff/astdiff')
  14. def pytest_generate_tests(metafunc):
  15. if os.path.exists(SOURCE_PATH):
  16. shutil.copytree(SOURCE_PATH, DATA_PATH)
  17. cases = sorted([os.path.basename(sql_query)[:-4] for sql_query in glob.glob(DATA_PATH + '/*.sql')])
  18. else:
  19. cases = []
  20. metafunc.parametrize(['case'], [(case, ) for case in cases])
  21. def test(case):
  22. program_file = os.path.join(DATA_PATH, case + '.sql')
  23. with codecs.open(program_file, encoding='utf-8') as f:
  24. program = f.readlines()
  25. header = program[0]
  26. canonize_ast = False
  27. if header.startswith('--ignore'):
  28. pytest.skip(header)
  29. elif header.startswith('--sanitizer ignore') and yatest.common.context.sanitize is not None:
  30. pytest.skip(header)
  31. elif header.startswith('--sanitizer ignore address') and yatest.common.context.sanitize == 'address':
  32. pytest.skip(header)
  33. elif header.startswith('--sanitizer ignore memory') and yatest.common.context.sanitize == 'memory':
  34. pytest.skip(header)
  35. elif header.startswith('--sanitizer ignore thread') and yatest.common.context.sanitize == 'thread':
  36. pytest.skip(header)
  37. elif header.startswith('--sanitizer ignore undefined') and yatest.common.context.sanitize == 'undefined':
  38. pytest.skip(header)
  39. elif header.startswith('--canonize ast'):
  40. canonize_ast = True
  41. program = '\n'.join(['use plato;'] + program)
  42. cfg = yql_utils.get_program_cfg(None, case, DATA_PATH)
  43. files = {}
  44. diff_tool = None
  45. scan_udfs = False
  46. for item in cfg:
  47. if item[0] == 'file':
  48. files[item[1]] = item[2]
  49. if item[0] == 'diff_tool':
  50. diff_tool = item[1:]
  51. if item[0] == 'scan_udfs':
  52. scan_udfs = True
  53. in_tables = yql_utils.get_input_tables(None, cfg, DATA_PATH, def_attr=yql_utils.KSV_ATTR)
  54. udfs_dir = yql_utils.get_udfs_path([
  55. yatest.common.build_path(os.path.join(yatest.common.context.project_path, ".."))
  56. ])
  57. xfail = yql_utils.is_xfail(cfg)
  58. if yql_utils.get_param('TARGET_PLATFORM') and xfail:
  59. pytest.skip('xfail is not supported on non-default target platform')
  60. extra_env = dict(os.environ)
  61. extra_env["YQL_UDF_RESOLVER"] = "1"
  62. extra_env["YQL_ARCADIA_BINARY_PATH"] = os.path.expandvars(yatest.common.build_path('.'))
  63. extra_env["YQL_ARCADIA_SOURCE_PATH"] = os.path.expandvars(yatest.common.source_path('.'))
  64. extra_env["Y_NO_AVX_IN_DOT_PRODUCT"] = "1"
  65. # this breaks tests using V0 syntax
  66. if "YA_TEST_RUNNER" in extra_env:
  67. del extra_env["YA_TEST_RUNNER"]
  68. yqlrun_res = YQLRun(udfs_dir=udfs_dir, prov='yt', use_sql2yql=False, cfg_dir=os.getenv('YQL_CONFIG_DIR') or 'yql/essentials/cfg/udf_test').yql_exec(
  69. program=program,
  70. run_sql=True,
  71. tables=in_tables,
  72. files=files,
  73. check_error=not xfail,
  74. extra_env=extra_env,
  75. require_udf_resolver=True,
  76. scan_udfs=scan_udfs
  77. )
  78. if xfail:
  79. assert yqlrun_res.execution_result.exit_code != 0
  80. results_path = os.path.join(yql_utils.yql_output_path(), case + '.results.txt')
  81. with open(results_path, 'w') as f:
  82. f.write(yqlrun_res.results)
  83. to_canonize = [yqlrun_res.std_err] if xfail else [yatest.common.canonical_file(yqlrun_res.results_file, local=True, diff_tool=diff_tool)]
  84. if canonize_ast:
  85. to_canonize += [yatest.common.canonical_file(yqlrun_res.opt_file, local=True, diff_tool=ASTDIFF_PATH)]
  86. return to_canonize