run_tests.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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, '-m', 'flake8', p.join( DIR_OF_THIS_SCRIPT, 'python' )
  35. ] )
  36. def ParseArguments():
  37. parser = argparse.ArgumentParser()
  38. parser.add_argument( '--skip-build', action = 'store_true',
  39. help = 'Do not build ycmd before testing' )
  40. parser.add_argument( '--coverage', action = 'store_true',
  41. help = 'Enable coverage report' )
  42. parser.add_argument( '--no-flake8', action = 'store_true',
  43. help = 'Do not run flake8' )
  44. parsed_args, nosetests_args = parser.parse_known_args()
  45. if 'COVERAGE' in os.environ:
  46. parsed_args.coverage = ( os.environ[ 'COVERAGE' ] == 'true' )
  47. return parsed_args, nosetests_args
  48. def BuildYcmdLibs( args ):
  49. if not args.skip_build:
  50. subprocess.check_call( [
  51. sys.executable,
  52. p.join( DIR_OF_THIS_SCRIPT, 'third_party', 'ycmd', 'build.py' )
  53. ] )
  54. def NoseTests( parsed_args, extra_nosetests_args ):
  55. # Always passing --with-id to nosetests enables non-surprising usage of
  56. # its --failed flag.
  57. nosetests_args = [ '-v', '--with-id' ]
  58. if parsed_args.coverage:
  59. nosetests_args += [ '--with-coverage',
  60. '--cover-erase',
  61. '--cover-package=ycm',
  62. '--cover-html' ]
  63. if extra_nosetests_args:
  64. nosetests_args.extend( extra_nosetests_args )
  65. else:
  66. nosetests_args.append( p.join( DIR_OF_THIS_SCRIPT, 'python' ) )
  67. subprocess.check_call( [ sys.executable, '-m', 'nose' ] + nosetests_args )
  68. def Main():
  69. ( parsed_args, nosetests_args ) = ParseArguments()
  70. if not parsed_args.no_flake8:
  71. RunFlake8()
  72. BuildYcmdLibs( parsed_args )
  73. NoseTests( parsed_args, nosetests_args )
  74. if __name__ == "__main__":
  75. Main()