SHARE
TWEET

./run_py3.py

a guest Jul 29th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2.  
  3. """Usage:
  4.  
  5. * ./run_py3.py folder  -> add Py3 to BUILD and run all unit tests
  6. * ./run_py3.py folder --no-build -> don't update BUILD
  7. * ./run_py3.py folder --contrib  -> operate on contrib folder
  8. """
  9.  
  10. import argparse
  11. import subprocess
  12. from textwrap import dedent
  13.  
  14.  
  15. def main() -> None:
  16.   parser = create_parser()
  17.   args = parser.parse_args()
  18.   folder_path = determine_folder_path(args)
  19.   if not args.no_build:
  20.     update_build(folder_path)
  21.   call_pants_test(folder_path)
  22.   print_instructions_for_specific_test(folder_path)
  23.  
  24.  
  25. # --------------------------------------------------
  26. # Setup
  27. # -------------------------------------------------
  28.  
  29. def create_parser() -> argparse.ArgumentParser:
  30.   parser = argparse.ArgumentParser()
  31.   parser.add_argument('folder')
  32.   parser.add_argument('-n', '--no-build', action='store_true')
  33.   parser.add_argument('-c', '--contrib', action='store_true')
  34.   return parser
  35.  
  36.  
  37. TEST_BASE_ROOT = 'tests/python/pants_test'
  38.  
  39. def determine_folder_path(args) -> str:
  40.   if args.contrib:
  41.     target_folder_root = args.folder.split('/')[0]
  42.     base_root = f'contrib/{target_folder_root}/{TEST_BASE_ROOT}/contrib'
  43.   else:
  44.     base_root = TEST_BASE_ROOT
  45.   return f'{base_root}/{args.folder}'
  46.  
  47.  
  48. # --------------------------------------------------
  49. # Update BUILD
  50. # -------------------------------------------------
  51.  
  52. def update_build(folder_path: str) -> None:
  53.   build_file = f'{folder_path}/BUILD'
  54.   # import ipdb; ipdb.set_trace()
  55.   with open(build_file, 'r') as f:
  56.     lines = list(f.readlines())
  57.   for i, line in enumerate(lines):
  58.     if 'python_tests' in line:
  59.       lines.insert(i + 1, "  compatibility='CPython>=3.5',\n")
  60.   with open(build_file, 'w') as f:
  61.     f.writelines(lines)
  62.  
  63.  
  64.  
  65. # --------------------------------------------------
  66. # Call tests
  67. # -------------------------------------------------
  68.  
  69. def call_pants_test(folder_path: str) -> None:
  70.   subprocess.run([
  71.     './pants',
  72.     'test',
  73.     f'{folder_path}:'  # TODO: skip integration tests
  74.   ])
  75.  
  76.  
  77. def print_instructions_for_specific_test(folder_path: str) -> None:
  78.   print(dedent(f"""\
  79.  
  80.  ----------------------------------------------------------------------
  81.  
  82.  To run a specific test, run './pants test {folder_path}:my_target'
  83.  
  84.  """))
  85.  
  86.  
  87. if __name__ == '__main__':
  88.   try:
  89.     main()
  90.   except KeyboardInterrupt:
  91.     pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top