예제 #1
0
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' ) )
예제 #2
0
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')))
예제 #3
0
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'))
예제 #4
0
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'))
예제 #5
0
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++'))
예제 #6
0
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'))
예제 #7
0
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'))
예제 #8
0
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'))
예제 #9
0
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'))
예제 #10
0
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'))
예제 #11
0
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), [])
예제 #12
0
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'))
예제 #13
0
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' ) )
예제 #14
0
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'))
예제 #15
0
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'))
예제 #16
0
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'))
예제 #17
0
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'))
예제 #18
0
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')))
예제 #19
0
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')),
                [])
예제 #20
0
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))
예제 #21
0
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' ) )