run_tests.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python3
  2. import argparse
  3. import glob
  4. import os
  5. import os.path as p
  6. import subprocess
  7. import sys
  8. DIR_OF_THIS_SCRIPT = p.dirname( p.abspath( __file__ ) )
  9. DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
  10. # We don't include python-future (not to be confused with pythonfutures) because
  11. # it needs to be inserted in sys.path AFTER the standard library imports but we
  12. # can't do that with PYTHONPATH because the std lib paths are always appended to
  13. # PYTHONPATH. We do it correctly inside Vim because we have access to the right
  14. # sys.path. So for dev, we rely on python-future being installed correctly with
  15. #
  16. # pip install -r python/test_requirements.txt
  17. #
  18. # Pip knows how to install this correctly so that it doesn't matter where in
  19. # sys.path the path is.
  20. python_path = [ p.join( DIR_OF_THIS_SCRIPT, 'python' ),
  21. p.join( DIR_OF_THIRD_PARTY, 'ycmd' ) ]
  22. if os.environ.get( 'PYTHONPATH' ):
  23. python_path.append( os.environ[ 'PYTHONPATH' ] )
  24. os.environ[ 'PYTHONPATH' ] = os.pathsep.join( python_path )
  25. def RunFlake8():
  26. print( 'Running flake8' )
  27. args = [ sys.executable,
  28. '-m',
  29. 'flake8',
  30. p.join( DIR_OF_THIS_SCRIPT, 'python' ) ]
  31. root_dir_scripts = glob.glob( p.join( DIR_OF_THIS_SCRIPT, '*.py' ) )
  32. args.extend( root_dir_scripts )
  33. subprocess.check_call( args )
  34. def ParseArguments():
  35. parser = argparse.ArgumentParser()
  36. parser.add_argument( '--skip-build', action = 'store_true',
  37. help = 'Do not build ycmd before testing' )
  38. parser.add_argument( '--coverage', action = 'store_true',
  39. help = 'Enable coverage report' )
  40. parser.add_argument( '--no-flake8', action = 'store_true',
  41. help = 'Do not run flake8' )
  42. parser.add_argument( '--dump-path', action = 'store_true',
  43. help = 'Dump the PYTHONPATH required to run tests '
  44. 'manually, then exit.' )
  45. parsed_args, unittest_args = parser.parse_known_args()
  46. if 'COVERAGE' in os.environ:
  47. parsed_args.coverage = ( os.environ[ 'COVERAGE' ] == 'true' )
  48. return parsed_args, unittest_args
  49. def BuildYcmdLibs( args ):
  50. if not args.skip_build:
  51. subprocess.check_call( [
  52. sys.executable,
  53. p.join( DIR_OF_THIS_SCRIPT, 'third_party', 'ycmd', 'build.py' ),
  54. '--quiet'
  55. ] )
  56. def UnittestTests( parsed_args, extra_unittest_args ):
  57. # if any extra arg is a specific file, or the '--' token, then assume the
  58. # arguments are unittest-aware test selection:
  59. # - don't use discover
  60. # - don't set the pattern to search for
  61. unittest_args = [ '-cb' ]
  62. prefer_regular = any( arg == '--' or p.isfile( arg )
  63. for arg in extra_unittest_args )
  64. if not prefer_regular:
  65. unittest_args += [ '-p', '*_test.py' ]
  66. if extra_unittest_args:
  67. unittest_args.extend( extra_unittest_args )
  68. if not ( prefer_regular and extra_unittest_args ):
  69. unittest_args.append( '-s' )
  70. test_directory = p.join( DIR_OF_THIS_SCRIPT, 'python', 'ycm', 'tests' )
  71. unittest_args.append( test_directory )
  72. if parsed_args.coverage:
  73. executable = [ sys.executable,
  74. '-m',
  75. 'coverage',
  76. 'run' ]
  77. else:
  78. executable = [ sys.executable, '-We' ]
  79. unittest = [ '-m', 'unittest' ]
  80. if not prefer_regular:
  81. unittest.append( 'discover' )
  82. subprocess.check_call( executable + unittest + unittest_args )
  83. def Main():
  84. ( parsed_args, unittest_args ) = ParseArguments()
  85. if parsed_args.dump_path:
  86. print( os.environ[ 'PYTHONPATH' ] )
  87. sys.exit()
  88. if not parsed_args.no_flake8:
  89. RunFlake8()
  90. BuildYcmdLibs( parsed_args )
  91. UnittestTests( parsed_args, unittest_args )
  92. if __name__ == "__main__":
  93. Main()