Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """Usage:
- * ./run_py3.py folder -> add Py3 to BUILD and run all unit tests
- * ./run_py3.py folder --no-build -> don't update BUILD
- * ./run_py3.py folder --contrib -> operate on contrib folder
- """
- import argparse
- import subprocess
- from textwrap import dedent
- def main() -> None:
- parser = create_parser()
- args = parser.parse_args()
- folder_path = determine_folder_path(args)
- if not args.no_build:
- update_build(folder_path)
- call_pants_test(folder_path)
- print_instructions_for_specific_test(folder_path)
- # --------------------------------------------------
- # Setup
- # -------------------------------------------------
- def create_parser() -> argparse.ArgumentParser:
- parser = argparse.ArgumentParser()
- parser.add_argument('folder')
- parser.add_argument('-n', '--no-build', action='store_true')
- parser.add_argument('-c', '--contrib', action='store_true')
- return parser
- TEST_BASE_ROOT = 'tests/python/pants_test'
- def determine_folder_path(args) -> str:
- if args.contrib:
- target_folder_root = args.folder.split('/')[0]
- base_root = f'contrib/{target_folder_root}/{TEST_BASE_ROOT}/contrib'
- else:
- base_root = TEST_BASE_ROOT
- return f'{base_root}/{args.folder}'
- # --------------------------------------------------
- # Update BUILD
- # -------------------------------------------------
- def update_build(folder_path: str) -> None:
- build_file = f'{folder_path}/BUILD'
- # import ipdb; ipdb.set_trace()
- with open(build_file, 'r') as f:
- lines = list(f.readlines())
- for i, line in enumerate(lines):
- if 'python_tests' in line:
- lines.insert(i + 1, " compatibility='CPython>=3.5',\n")
- with open(build_file, 'w') as f:
- f.writelines(lines)
- # --------------------------------------------------
- # Call tests
- # -------------------------------------------------
- def call_pants_test(folder_path: str) -> None:
- subprocess.run([
- './pants',
- 'test',
- f'{folder_path}:' # TODO: skip integration tests
- ])
- def print_instructions_for_specific_test(folder_path: str) -> None:
- print(dedent(f"""\
- ----------------------------------------------------------------------
- To run a specific test, run './pants test {folder_path}:my_target'
- """))
- if __name__ == '__main__':
- try:
- main()
- except KeyboardInterrupt:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement