def test_file(self): class MockCreator(object): def __init__(self, msbuild_output=False): self.msbuild_output = msbuild_output src = SourceFile(Path('foo'), 'c++') self.assertEqual(textify(src), r'$(SolutionDir)\foo') src.creator = MockCreator() self.assertEqual(textify(src), r'$(IntDir)\foo') src.creator = MockCreator(True) self.assertEqual(textify(src), r'$(OutDir)\foo')
def test_filter(self): def my_filter(path): if path.directory: return find.FindResult.not_now elif path.ext() == '.cpp': return find.FindResult.include else: return find.FindResult.exclude expected = [SourceFile(srcpath('file.cpp'), 'c++')] pre = [ Directory(srcpath('./')), Directory(srcpath('dir/')), Directory(srcpath('dir2/')) ] post = [Directory(srcpath('dir/sub/'))] self.assertFound(self.find('**', type='*', filter=my_filter), expected, pre=pre, post=post) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_no_cache(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find('**', cache=False), expected) self.assertFindDirs(set())
def test_flat(self): expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), SourceFile(srcpath('file.cpp'), 'c++') ] with mock_context(): self.assertFound(self.find(flat=True), expected)
def test_type_file(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] with mock_context(): self.assertFound(self.find(type='f'), expected)
def test_submodule_path_object(self): with self.context.push_path(Path('dir/build.bfg', Root.srcdir)): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find(srcpath('**')), expected) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') }) expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), Directory(srcpath('dir2')), Directory(srcpath('dir/sub')) ] self.assertFound(self.find(srcpath('**/')), expected) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_multiple_patterns(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find(['*.cpp', 'dir/*.txt']), expected) self.assertFindDirs({srcpath('./'), srcpath('dir/')})
def test_name(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] with mock.patch('warnings.warn') as m: self.assertFound(self.find(name='file*'), expected) self.assertEqual(m.call_count, 1)
def test_glob_with_filters(self): def my_filter(path): return (find.FindResult.exclude_recursive if path.directory else find.FindResult.include) expected = [SourceFile(srcpath('file.cpp'), 'c++')] self.assertFound(self.find('**/file*', filter=my_filter), expected) self.assertFindDirs({srcpath('./')})
def test_output_file(self): fmt = self.env.target_platform.object_format out = MsvcPrecompiledHeader(Path('hdr.pch'), Path('src.obj'), 'hdr.h', fmt, 'c++') self.assertEqual( self.compiler.output_file( 'hdr.h', AttrDict(pch_source=SourceFile(Path('src.c'), 'c'))), [out, out.object_file])
def test_invalid_run_arguments(self): bad_file = SourceFile(self.Path('file', Root.srcdir), lang=self.lang) with self.assertRaises(TypeError): self.runner.run_arguments(bad_file) with mock.patch('bfg9000.shell.which', return_value=['command']), \ mock.patch('bfg9000.shell.execute', default_mock_execute), \ self.assertRaises(TypeError): self.env.run_arguments(bad_file)
def test_combine_filters(self): def my_filter(path, type): return (find.FindResult.include if type == 'f' else find.FindResult.exclude) expected = [SourceFile(srcpath('file.cpp'), 'c++')] with mock_context(): self.assertFound(self.find(name='*.cpp', filter=my_filter), expected)
def test_dist(self): expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] with mock_context(): self.assertFound(self.find(dist=False), expected, [self.bfgfile])
def test_cache(self): expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] with mock_context(): self.assertFound(self.find(cache=False), expected) self.assertEqual(self.build['find_dirs'], set())
def test_submodule_path_object(self): with self.context.push_path(Path('dir/build.bfg', Root.srcdir)), \ mock_context(): # noqa expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find(srcpath('.')), expected)
def test_run_arguments(self): src_file = SourceFile(self.Path('file', Root.srcdir), lang=self.lang) self.assertEqual(self.tool.run_arguments(src_file), [self.tool, src_file]) with mock.patch('bfg9000.shell.which', return_value=['command']): args = self.env.run_arguments(src_file) self.assertEqual(len(args), 2) self.assertEqual(type(args[0]), self.tool_type) self.assertEqual(args[1], src_file)
def test_run_arguments(self): src_file = SourceFile(Path('file', Root.srcdir), lang=self.lang) self.assertEqual(self.tool.run_arguments(src_file), [self.tool, src_file]) with mock.patch('bfg9000.shell.which', mock_which): args = self.env.run_arguments(src_file) self.assertEqual(len(args), 2) self.assertEqual(type(args[0]), self.ToolType) self.assertEqual(args[1], src_file)
def test_type_file(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find('**', type='f'), expected) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_resources(self): proj = VcxProject(FakeEnv(), 'project', output_file=Path('output'), files=[{ 'name': SourceFile(Path('res.rc'), 'rc'), 'options': {} }]) project = self.xpath(self._write_get_tree(proj), '/x:Project')[0] self.assertXPath(project, './x:ItemGroup/x:ResourceCompile/@Include', ['$(SolutionDir)res.rc'])
def test_default(self): expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), Directory(srcpath('dir2')), SourceFile(srcpath('file.cpp'), 'c++'), Directory(srcpath('dir/sub')), File(srcpath('dir/file2.txt')) ] with mock.patch('warnings.warn') as m: self.assertFound(self.find(), expected) self.assertEqual(m.call_count, 1)
def test_multiple_extra(self): expected = [SourceFile(srcpath('file.cpp'), 'c++')] extra = [Directory(srcpath('dir/sub')), File(srcpath('dir/file2.txt'))] self.assertFound(self.find('**/*.cpp', extra=['*.txt', 'su?/']), expected, post=extra) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_extra(self): expected = [SourceFile(srcpath('file.cpp'), 'c++')] extra = [File(srcpath('dir/file2.txt'))] self.assertFound(self.find('**/*.cpp', extra='*.txt'), expected, post=extra) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_no_dist(self): expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('dir/file2.txt')) ] self.assertFoundResult(self.find('**', dist=False), expected) self.assertEqual(list(self.build.sources()), [self.bfgfile]) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_duplicate_basename(self): proj = VcxProject(FakeEnv(), 'project', output_file=Path('output'), files=[ { 'name': SourceFile(Path('a/src.cpp'), 'c++'), 'options': {} }, { 'name': SourceFile(Path('b/src.cpp'), 'c++'), 'options': {} }, ]) tree = self._write_get_tree(proj) self.assertXPath( tree, 'x:ItemGroup/x:ClCompile/@Include', [r'$(SolutionDir)a\src.cpp', r'$(SolutionDir)b\src.cpp']) self.assertXPath(tree, 'x:ItemGroup/x:ClCompile/x:ObjectFileName/text()', [r'$(IntDir)a\src.obj', r'$(IntDir)b\src.obj'])
def test_custom_default_exclude(self): self.context['project'](find_exclude=['*.txt']) expected = [ SourceFile(srcpath('file.cpp'), 'c++'), File(srcpath('file.cpp~')) ] self.assertFound(self.find('**/file*'), expected) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })
def test_duplicate_basename(self): proj = VcxProject(FakeEnv(), 'project', output_file=Path('output'), files=[ { 'name': SourceFile(Path('a/src.cpp'), 'c++'), 'options': {} }, { 'name': SourceFile(Path('b/src.cpp'), 'c++'), 'options': {} }, ]) out = BytesIO() proj.write(out) tree = etree.fromstring(out.getvalue()) self.assertXPath( tree, 'x:ItemGroup/x:ClCompile/@Include', [r'$(SolutionDir)\a\src.cpp', r'$(SolutionDir)\b\src.cpp']) self.assertXPath(tree, 'x:ItemGroup/x:ClCompile/x:ObjectFileName/text()', [r'$(IntDir)\a\src.obj', r'$(IntDir)\b\src.obj'])
def test_write(self): proj = VcxProject(FakeEnv(), 'project', output_file=Path('output'), files=[{ 'name': SourceFile(Path('src.cpp'), 'c++'), 'options': {} }]) project = self.xpath(self._write_get_tree(proj), '/x:Project')[0] self.assertXPath(project, './x:PropertyGroup/x:TargetPath/text()', ['$(OutDir)output']) self.assertXPath(project, './x:ItemGroup/x:ClCompile/@Include', ['$(SolutionDir)src.cpp'])
def test_filter(self): def my_filter(path, type): if path.basename() == 'dir': return find.FindResult.not_now elif path.ext() == '.cpp': return find.FindResult.include else: return find.FindResult.exclude expected = [SourceFile(srcpath('file.cpp'), 'c++')] with mock_context(): self.assertFound( self.find(filter=my_filter), expected, [self.bfgfile, Directory(srcpath('dir'))] + expected)
def test_write(self): proj = VcxProject(FakeEnv(), 'project', output_file=Path('output'), files=[{ 'name': SourceFile(Path('src.cpp'), 'c++'), 'options': {} }]) out = BytesIO() proj.write(out) tree = etree.fromstring(out.getvalue()) project = self.xpath(tree, '/x:Project')[0] self.assertXPath(project, './x:PropertyGroup/x:TargetPath/text()', [r'$(OutDir)\output']) self.assertXPath(project, './x:ItemGroup/x:ClCompile/@Include', [r'$(SolutionDir)\src.cpp'])
def test_type_all(self): expected = [ Directory(srcpath('.')), Directory(srcpath('dir')), Directory(srcpath('dir2')), SourceFile(srcpath('file.cpp'), 'c++'), Directory(srcpath('dir/sub')), File(srcpath('dir/file2.txt')) ] self.assertFound(self.find('**', type='*'), expected) self.assertFindDirs({ srcpath('./'), srcpath('dir/'), srcpath('dir2/'), srcpath('dir/sub/') })