codekingpro/portable-devtools
114k
1"""Tests for distutils.command.build_clib."""2 3import os4from distutils.command.build_clib import build_clib5from distutils.errors import DistutilsSetupError6from distutils.tests import missing_compiler_executable, support7 8import pytest9 10 11class TestBuildCLib(support.TempdirManager):12 def test_check_library_dist(self):13 pkg_dir, dist = self.create_dist()14 cmd = build_clib(dist)15 16 # 'libraries' option must be a list17 with pytest.raises(DistutilsSetupError):18 cmd.check_library_list('foo')19 20 # each element of 'libraries' must a 2-tuple21 with pytest.raises(DistutilsSetupError):22 cmd.check_library_list(['foo1', 'foo2'])23 24 # first element of each tuple in 'libraries'25 # must be a string (the library name)26 with pytest.raises(DistutilsSetupError):27 cmd.check_library_list([(1, 'foo1'), ('name', 'foo2')])28 29 # library name may not contain directory separators30 with pytest.raises(DistutilsSetupError):31 cmd.check_library_list(32 [('name', 'foo1'), ('another/name', 'foo2')],33 )34 35 # second element of each tuple must be a dictionary (build info)36 with pytest.raises(DistutilsSetupError):37 cmd.check_library_list(38 [('name', {}), ('another', 'foo2')],39 )40 41 # those work42 libs = [('name', {}), ('name', {'ok': 'good'})]43 cmd.check_library_list(libs)44 45 def test_get_source_files(self):46 pkg_dir, dist = self.create_dist()47 cmd = build_clib(dist)48 49 # "in 'libraries' option 'sources' must be present and must be50 # a list of source filenames51 cmd.libraries = [('name', {})]52 with pytest.raises(DistutilsSetupError):53 cmd.get_source_files()54 55 cmd.libraries = [('name', {'sources': 1})]56 with pytest.raises(DistutilsSetupError):57 cmd.get_source_files()58 59 cmd.libraries = [('name', {'sources': ['a', 'b']})]60 assert cmd.get_source_files() == ['a', 'b']61 62 cmd.libraries = [('name', {'sources': ('a', 'b')})]63 assert cmd.get_source_files() == ['a', 'b']64 65 cmd.libraries = [66 ('name', {'sources': ('a', 'b')}),67 ('name2', {'sources': ['c', 'd']}),68 ]69 assert cmd.get_source_files() == ['a', 'b', 'c', 'd']70 71 def test_build_libraries(self):72 pkg_dir, dist = self.create_dist()73 cmd = build_clib(dist)74 75 class FakeCompiler:76 def compile(*args, **kw):77 pass78 79 create_static_lib = compile80 81 cmd.compiler = FakeCompiler()82 83 # build_libraries is also doing a bit of typo checking84 lib = [('name', {'sources': 'notvalid'})]85 with pytest.raises(DistutilsSetupError):86 cmd.build_libraries(lib)87 88 lib = [('name', {'sources': list()})]89 cmd.build_libraries(lib)90 91 lib = [('name', {'sources': tuple()})]92 cmd.build_libraries(lib)93 94 def test_finalize_options(self):95 pkg_dir, dist = self.create_dist()96 cmd = build_clib(dist)97 98 cmd.include_dirs = 'one-dir'99 cmd.finalize_options()100 assert cmd.include_dirs == ['one-dir']101 102 cmd.include_dirs = None103 cmd.finalize_options()104 assert cmd.include_dirs == []105 106 cmd.distribution.libraries = 'WONTWORK'107 with pytest.raises(DistutilsSetupError):108 cmd.finalize_options()109 110 @pytest.mark.skipif('platform.system() == "Windows"')111 def test_run(self):112 pkg_dir, dist = self.create_dist()113 cmd = build_clib(dist)114 115 foo_c = os.path.join(pkg_dir, 'foo.c')116 self.write_file(foo_c, 'int main(void) { return 1;}\n')117 cmd.libraries = [('foo', {'sources': [foo_c]})]118 119 build_temp = os.path.join(pkg_dir, 'build')120 os.mkdir(build_temp)121 cmd.build_temp = build_temp122 cmd.build_clib = build_temp123 124 # Before we run the command, we want to make sure125 # all commands are present on the system.126 ccmd = missing_compiler_executable()127 if ccmd is not None:128 self.skipTest(f'The {ccmd!r} command is not found')129 130 # this should work131 cmd.run()132 133 # let's check the result134 assert 'libfoo.a' in os.listdir(build_temp)135 