def FlagsForFile_FlagsCachedByDefault_test( *args ): flags_object = flags.Flags() results = { 'flags': [ '-x', 'c' ] } with patch( 'ycmd.completers.cpp.flags._CallExtraConfFlagsForFile', return_value = results ): flags_list = flags_object.FlagsForFile( '/foo', False ) assert_that( flags_list, contains( '-x', 'c' ) ) results[ 'flags' ] = [ '-x', 'c++' ] with patch( 'ycmd.completers.cpp.flags._CallExtraConfFlagsForFile', return_value = results ): flags_list = flags_object.FlagsForFile( '/foo', False ) assert_that( flags_list, contains( '-x', 'c' ) )
def FlagsForFile_MakeRelativePathsAbsoluteIfOptionSpecified_test(): flags_object = flags.Flags() def FlagsForFile(filename): return { 'flags': ['-x', 'c', '-I', 'header'], 'include_paths_relative_to_dir': '/working_dir/' } with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo', False) assert_that( flags_list, contains('-x', 'c', '-I', os.path.normpath('/working_dir/header')))
def FlagsForFile_AddMacIncludePaths_NoStandardSystemIncludes_test(): flags_object = flags.Flags() def Settings(**kwargs): return {'flags': ['-Wall', '-nostdinc']} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo') assert_that( flags_list, contains('-Wall', '-nostdinc', '-resource-dir=' + CLANG_RESOURCE_DIR, '-isystem', os.path.join(CLANG_RESOURCE_DIR, 'include'), '-fspell-checking'))
def FlagsForFile_FlagsCachedWhenDoCacheIsTrue_test(): flags_object = flags.Flags() def Settings(**kwargs): return {'flags': ['-x', 'c'], 'do_cache': True} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c')) def Settings(**kwargs): return {'flags': ['-x', 'c++']} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c'))
def FlagsForFile_FlagsNotCachedWhenDoCacheIsFalse_test(): flags_object = flags.Flags() def FlagsForFile(filename): return {'flags': ['-x', 'c'], 'do_cache': False} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c')) def FlagsForFile(filename): return {'flags': ['-x', 'c++']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c++'))
def CompilationDatabase_HeaderFileHeuristic_test(): with TemporaryClangTestDir() 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 assert_that( flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.h'), add_extra_clang_flags=False), contains('clang++', '-x', 'c++', '-x', 'c++', '-Wall'))
def FlagsForFile_FlagsCachedByDefault_test(): flags_object = flags.Flags() def FlagsForFile(filename): return {'flags': ['-x', 'c']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c')) def FlagsForFile(filename): return {'flags': ['-x', 'c++']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo', False) assert_that(flags_list, contains('-x', 'c'))
def FlagsForFile_AddMacIncludePaths_NoBuiltinIncludes_test(): flags_object = flags.Flags() def Settings(**kwargs): return {'flags': ['-Wall', '-nobuiltininc']} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo') assert_that( flags_list, contains('-Wall', '-nobuiltininc', '-resource-dir=' + CLANG_RESOURCE_DIR, '-isystem', '/usr/include/c++/v1', '-isystem', '/usr/local/include', '-isystem', '/usr/include', '-iframework', '/System/Library/Frameworks', '-iframework', '/Library/Frameworks', '-fspell-checking'))
def CompilationDatabase_UseFlagsFromDatabase_test(): with TemporaryClangTestDir() 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), contains('clang++', '-x', 'c++', '-x', 'c++', '-I' + os.path.normpath(tmp_dir), '-I' + os.path.normpath('/absolute/path'), '-Wall'))
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_HeaderFileHeuristicNotFound_test(): with TemporaryClangTestDir() 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), [])
def FlagsForFile_AddMacIncludePaths_NoLibCpp_test(): flags_object = flags.Flags() def Settings(**kwargs): return {'flags': ['-Wall', '-stdlib=libc++', '-stdlib=libstdc++']} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo') assert_that( flags_list, contains('-Wall', '-stdlib=libc++', '-stdlib=libstdc++', '-resource-dir=' + CLANG_RESOURCE_DIR, '-isystem', '/usr/local/include', '-isystem', os.path.join(CLANG_RESOURCE_DIR, 'include'), '-isystem', '/usr/include', '-iframework', '/System/Library/Frameworks', '-iframework', '/Library/Frameworks', '-fspell-checking'))
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_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 FlagsForFile_AddMacIncludePaths_Toolchain_CommandLine_test(): flags_object = flags.Flags() def Settings(**kwargs): return {'flags': ['-Wall']} with MockExtraConfModule(Settings): flags_list, _ = flags_object.FlagsForFile('/foo') assert_that( flags_list, contains('-Wall', '-resource-dir=' + CLANG_RESOURCE_DIR, '-isystem', '/Library/Developer/CommandLineTools/usr/include/c++/v1', '-isystem', '/usr/include/c++/v1', '-isystem', '/usr/local/include', '-isystem', os.path.join(CLANG_RESOURCE_DIR, 'include'), '-isystem', '/Library/Developer/CommandLineTools/usr/include', '-isystem', '/usr/include', '-iframework', '/System/Library/Frameworks', '-iframework', '/Library/Frameworks', '-fspell-checking'))
def CompilationDatabase_ExplicitHeaderFileEntry_test(): with TemporaryClangTestDir() 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), contains('clang++', '-x', 'c++', '-I' + os.path.normpath('/absolute/path'), '-Wall'))
def FlagsForFile_DoNotAddMacIncludePathsWithSysroot_test(): flags_object = flags.Flags() def FlagsForFile(filename): return {'flags': ['-isysroot', 'test1', '--test2=test']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo') assert_that(flags_list, not_(has_item('sentinel_value_for_testing'))) def FlagsForFile(filename): return {'flags': ['-test', '--sysroot', 'test1']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo') assert_that(flags_list, not_(has_item('sentinel_value_for_testing'))) def FlagsForFile(filename): return {'flags': ['-test', 'test1', '--sysroot=test']} with MockExtraConfModule(FlagsForFile): flags_list = flags_object.FlagsForFile('/foo') assert_that(flags_list, not_(has_item('sentinel_value_for_testing')))
def CompilationDatabase_FileNotInDatabase_test(): compile_commands = [] with TemporaryClangTestDir() as tmp_dir: with TemporaryClangProject(tmp_dir, compile_commands): eq_(flags.Flags().FlagsForFile(os.path.join(tmp_dir, 'test.cc')), [])
def CompilationDatabase_NoDatabase_test(): with TemporaryClangTestDir() as tmp_dir: assert_that( calling(flags.Flags().FlagsForFile).with_args( os.path.join(tmp_dir, 'test.cc')), raises(NoExtraConfDetected))
def FlagsForFile_OverrideTranslationUnit_test(): flags_object = flags.Flags() def Settings( **kwargs ): return { 'flags': [], 'override_filename': 'changed:' + kwargs[ 'filename' ] } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( 'changed:/foo' ) ) def Settings( **kwargs ): return { 'flags': [], 'override_filename': kwargs[ 'filename' ] } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( '/foo' ) ) def Settings( **kwargs ): return { 'flags': [], 'override_filename': None } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( '/foo' ) ) def Settings( **kwargs ): return { 'flags': [], } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( '/foo' ) ) def Settings( **kwargs ): return { 'flags': [], 'override_filename': '' } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( '/foo' ) ) def Settings( **kwargs ): return { 'flags': [], 'override_filename': '0' } with MockExtraConfModule( Settings ): flags_list, filename = flags_object.FlagsForFile( '/foo' ) assert_that( flags_list, contains() ) assert_that( filename, equal_to( '0' ) )