def DebugInfo_ExtraConf_UseDatabaseOverGlobal_test(): with TemporaryTestDir() as tmp_dir: database = [{ 'directory': tmp_dir, 'command': 'clang++ -x c++ -I test foo.cpp', 'file': os.path.join(tmp_dir, 'foo.cpp'), }] with TemporaryClangProject(tmp_dir, database): @IsolatedYcmd({ 'global_ycm_extra_conf': PathToTestFile('extra_conf', 'global_extra_conf.py'), }) def Test(app): request_data = BuildRequest(filepath=os.path.join( tmp_dir, 'foo.cpp'), filetype='cpp') request_data['contents'] = '' test = {'request': request_data} RunAfterInitialized(app, test) assert_that( app.post_json('/debug_info', request_data).json, has_entry( 'completer', has_entries({ 'name': 'C-family', 'servers': contains( has_entries({ 'name': 'Clangd', 'is_running': True, 'extras': contains( has_entries({ 'key': 'Server State', 'value': 'Initialized', }), has_entries({ 'key': 'Project Directory', 'value': tmp_dir, }), has_entries({ 'key': 'Settings', 'value': '{}', }), has_entries({ 'key': 'Compilation Command', 'value': False }), ), })), 'items': empty() }))) yield Test
def CppBindings_CompilationDatabase_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -I. -I/absolute/path -Wall', 'file': os.path.join( tmp_dir, 'test.cc' ), }, ] with TemporaryClangProject( tmp_dir, compile_commands ): db = ycm_core.CompilationDatabase( tmp_dir ) db_successful = db.DatabaseSuccessfullyLoaded() db_busy = db.AlreadyGettingFlags() db_dir = db.database_directory compilation_info = db.GetCompilationInfoForFile( compile_commands[ 0 ][ 'file' ] ) del db del compile_commands eq_( db_successful, True ) eq_( db_busy, False ) eq_( db_dir, tmp_dir ) assert_that( compilation_info, has_properties( { 'compiler_working_dir_': tmp_dir, 'compiler_flags_': contains( 'clang++', '--driver-mode=g++', '-x', 'c++', '-I.', '-I/absolute/path', '-Wall' ) } ) )
def CompilationDatabase_UseFlagsFromSameDir_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): f = flags.Flags() # If we now ask for a file _not_ in the DB, we get [] eq_( f.FlagsForFile(os.path.join(tmp_dir, 'test1.cc'), add_extra_clang_flags=False), ([], os.path.join(tmp_dir, 'test1.cc'))) # Then, we ask for a file that _is_ in the db. It will cache these flags # against the files' directory. assert_that( f.FlagsForFile(os.path.join(tmp_dir, 'test.cc'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-x', 'c++', '-Wall')) # If we now ask for a file _not_ in the DB, but in the same dir, we should # get the same flags assert_that( f.FlagsForFile(os.path.join(tmp_dir, 'test2.cc'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-x', 'c++', '-Wall'))
def CompilationDatabase_InvalidDatabase_test(): with TemporaryTestDir() as tmp_dir: with TemporaryClangProject(tmp_dir, 'this is junk'): assert_that( calling(flags.Flags().FlagsForFile).with_args( os.path.join(tmp_dir, 'test.cc')), raises(NoExtraConfDetected))
def DebugInfo_FlagsWhenNoExtraConfAndInvalidCompilationDatabase_test( app ): with TemporaryTestDir() as tmp_dir: compile_commands = 'garbage' with TemporaryClangProject( tmp_dir, compile_commands ): request_data = BuildRequest( filepath = os.path.join( tmp_dir, 'test.cc' ), filetype = 'cpp' ) assert_that( app.post_json( '/debug_info', request_data ).json, has_entry( 'completer', has_entries( { 'name': 'C-family', 'servers': empty(), 'items': contains( has_entries( { 'key': 'compilation database path', 'value': 'None' } ), has_entries( { 'key': 'flags', 'value': '[]' } ), has_entries( { 'key': 'translation unit', 'value': os.path.join( tmp_dir, 'test.cc' ) } ) ) } ) ) )
def CompilationDatabase_UseFlagsFromSameDir_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): f = flags.Flags() # If we ask for a file that is not in the DB but is in the same directory # of another file present in the DB, we get its flags. assert_that( f.FlagsForFile(os.path.join(tmp_dir, 'test1.cc'), add_extra_clang_flags=False), contains(contains('clang++', '-x', 'c++', '-Wall'), os.path.join(tmp_dir, 'test1.cc'))) # If we ask for a file that is not in the DB but in a subdirectory # of another file present in the DB, we get its flags. assert_that( f.FlagsForFile(os.path.join(tmp_dir, 'some_dir', 'test1.cc'), add_extra_clang_flags=False), contains(contains('clang++', '-x', 'c++', '-Wall'), os.path.join(tmp_dir, 'some_dir', 'test1.cc')))
def test_DebugInfo_ExtraConf_UseLocalOverDatabase( self, app ): with TemporaryTestDir() as tmp_dir: database = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -I test foo.cpp' , 'file': os.path.join( tmp_dir, 'foo.cpp' ), } ] with TemporaryClangProject( tmp_dir, database ): extra_conf = os.path.join( tmp_dir, '.ycm_extra_conf.py' ) with open( extra_conf, 'w' ) as f: f.write( ''' def Settings( **kwargs ): return { 'flags': [ '-x', 'c++', '-I', 'ycm' ] } ''' ) try: request_data = BuildRequest( filepath = os.path.join( tmp_dir, 'foo.cpp' ), filetype = 'cpp' ) request_data[ 'contents' ] = '' test = { 'request': request_data } RunAfterInitialized( app, test ) assert_that( app.post_json( '/debug_info', request_data ).json, has_entry( 'completer', has_entries( { 'name': 'C-family', 'servers': contains_exactly( has_entries( { 'name': 'Clangd', 'is_running': True, 'extras': contains_exactly( has_entries( { 'key': 'Server State', 'value': 'Initialized', } ), has_entries( { 'key': 'Project Directory', 'value': tmp_dir, } ), has_entries( { 'key': 'Settings', 'value': '{}', } ), has_entries( { 'key': 'Compilation Command', 'value': has_items( '-x', 'c++', '-I', 'ycm' ) } ), ), } ) ), 'items': empty() } ) ) ) finally: os.remove( extra_conf )
def CompilationDatabase_CUDALanguageFlags_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -Wall {}'.format('./test.cu'), 'file': os.path.join(tmp_dir, 'test.cu'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): # If we ask for a header file, it returns the equivalent cu file assert_that( flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.h'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'cuda', '-Wall'))
def CompilationDatabase_UseFlagsFromDatabase_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -I. -I/absolute/path -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): assert_that( flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.cc'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-x', 'c++', '-I' + os.path.normpath(tmp_dir), '-I' + os.path.normpath('/absolute/path'), '-Wall'))
def CompilationDatabase_HeaderFileHeuristicNotFound_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): # If we ask for a header file, it returns the equivalent cc file (if and # only if there are flags for that file) eq_( flags.Flags().FlagsForFile(os.path.join( tmp_dir, 'not_in_the_db.h'), add_extra_clang_flags=False)[0], [])
def test_DebugInfo_FlagsWhenGlobalExtraConfAndCompilationDatabaseLoaded( self, app): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -I. -I/absolute/path -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): request_data = BuildRequest(filepath=os.path.join( tmp_dir, 'test.cc'), filetype='cpp') assert_that( app.post_json('/debug_info', request_data).json, has_entry( 'completer', has_entries({ 'name': 'C-family', 'servers': empty(), 'items': contains_exactly( has_entries({ 'key': 'compilation database path', 'value': instance_of(str) }), has_entries({ 'key': 'flags', 'value': matches_regexp( "\\['clang\\+\\+', '-x', 'c\\+\\+', .*, '-Wall', .*\\]" ) }), has_entries({ 'key': 'translation unit', 'value': os.path.join(tmp_dir, 'test.cc'), })) })))
def CompilationDatabase_HeaderFile_SameNameAsSourceFile_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): # If we ask for a header file with the same name as a source file, it # returns the flags of that cc file (and a special language flag for C++ # headers). assert_that( flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.h'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-Wall', '-x', 'c++-header'))
def CompilationDatabase_HeaderFile_DifferentNameFromSourceFile_test(): with TemporaryTestDir() as tmp_dir: compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): # Even if we ask for a header file with a different name than the source # file, it still returns the flags from the cc file (and a special # language flag for C++ headers). assert_that( flags.Flags().FlagsForFile(os.path.join( tmp_dir, 'not_in_the_db.h'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-Wall', '-x', 'c++-header'))
def CompilationDatabase_ExplicitHeaderFileEntry_test(): with TemporaryTestDir() as tmp_dir: # Have an explicit header file entry which should take priority over the # corresponding source file compile_commands = [ { 'directory': tmp_dir, 'command': 'clang++ -x c++ -I. -I/absolute/path -Wall', 'file': os.path.join(tmp_dir, 'test.cc'), }, { 'directory': tmp_dir, 'command': 'clang++ -I/absolute/path -Wall', 'file': os.path.join(tmp_dir, 'test.h'), }, ] with TemporaryClangProject(tmp_dir, compile_commands): assert_that( flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.h'), add_extra_clang_flags=False)[0], contains('clang++', '-x', 'c++', '-I' + os.path.normpath('/absolute/path'), '-Wall'))
def CompilationDatabase_FileNotInDatabase_test(): compile_commands = [] with TemporaryTestDir() as tmp_dir: with TemporaryClangProject(tmp_dir, compile_commands): eq_(flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.cc')), ([], os.path.join(tmp_dir, 'test.cc')))