run_tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. import os
  3. import subprocess
  4. import os.path as p
  5. import sys
  6. DIR_OF_THIS_SCRIPT = p.dirname( p.abspath( __file__ ) )
  7. DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
  8. DIR_OF_YCMD_THIRD_PARTY = p.join( DIR_OF_THIRD_PARTY, 'ycmd', 'third_party' )
  9. python_path = []
  10. for folder in os.listdir( DIR_OF_THIRD_PARTY ):
  11. python_path.append( p.abspath( p.join( DIR_OF_THIRD_PARTY, folder ) ) )
  12. for folder in os.listdir( DIR_OF_YCMD_THIRD_PARTY ):
  13. # We skip python-future because it needs to be inserted in sys.path AFTER
  14. # the standard library imports but we can't do that with PYTHONPATH because
  15. # the std lib paths are always appended to PYTHONPATH. We do it correctly in
  16. # prod in ycmd/utils.py because we have access to the right sys.path.
  17. # So for dev, we rely on python-future being installed correctly with
  18. # pip install -r test_requirements.txt
  19. #
  20. # Pip knows how to install this correctly so that it doesn't matter where in
  21. # sys.path the path is.
  22. if folder == 'python-future':
  23. continue
  24. python_path.append( p.abspath( p.join( DIR_OF_YCMD_THIRD_PARTY, folder ) ) )
  25. if os.environ.get( 'PYTHONPATH' ):
  26. python_path.append( os.environ[ 'PYTHONPATH' ] )
  27. os.environ[ 'PYTHONPATH' ] = os.pathsep.join( python_path )
  28. sys.path.insert( 1, p.abspath( p.join( DIR_OF_YCMD_THIRD_PARTY,
  29. 'argparse' ) ) )
  30. import argparse
  31. def RunFlake8():
  32. print( 'Running flake8' )
  33. subprocess.check_call( [
  34. sys.executable,
  35. # __main__ is required on Python 2.6.
  36. '-m', 'flake8.__main__',
  37. p.join( DIR_OF_THIS_SCRIPT, 'python' )
  38. ] )
  39. def ParseArguments():
  40. parser = argparse.ArgumentParser()
  41. parser.add_argument( '--skip-build', action = 'store_true',
  42. help = 'Do not build ycmd before testing' )
  43. parser.add_argument( '--coverage', action = 'store_true',
  44. help = 'Enable coverage report' )
  45. parser.add_argument( '--no-flake8', action = 'store_true',
  46. help = 'Do not run flake8' )
  47. parsed_args, nosetests_args = parser.parse_known_args()
  48. if 'COVERAGE' in os.environ:
  49. parsed_args.coverage = ( os.environ[ 'COVERAGE' ] == 'true' )
  50. return parsed_args, nosetests_args
  51. def BuildYcmdLibs( args ):
  52. if not args.skip_build:
  53. subprocess.check_call( [
  54. sys.executable,
  55. p.join( DIR_OF_THIS_SCRIPT, 'third_party', 'ycmd', 'build.py' )
  56. ] )
  57. def NoseTests( parsed_args, extra_nosetests_args ):
  58. # Always passing --with-id to nosetests enables non-surprising usage of
  59. # its --failed flag.
  60. nosetests_args = [ '-v', '--with-id' ]
  61. if parsed_args.coverage:
  62. nosetests_args += [ '--with-coverage',
  63. '--cover-erase',
  64. '--cover-package=ycm',
  65. '--cover-html' ]
  66. if extra_nosetests_args:
  67. nosetests_args.extend( extra_nosetests_args )
  68. else:
  69. nosetests_args.append( p.join( DIR_OF_THIS_SCRIPT, 'python' ) )
  70. subprocess.check_call( [ sys.executable,
  71. # __main__ is required on Python 2.6.
  72. '-m', 'nose.__main__' ] + nosetests_args )
  73. def Main():
  74. ( parsed_args, nosetests_args ) = ParseArguments()
  75. if not parsed_args.no_flake8:
  76. RunFlake8()
  77. BuildYcmdLibs( parsed_args )
  78. NoseTests( parsed_args, nosetests_args )
  79. if __name__ == "__main__":
  80. Main()