run_tests.py 3.1 KB

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