88from distutils .command .build_ext import build_ext
99from distutils import sysconfig
1010from distutils .tests .support import TempdirManager
11+ from distutils .tests .support import LoggingSilencer
12+ from distutils .extension import Extension
13+ from distutils .errors import UnknownFileError
1114
1215import unittest
1316from test import support
@@ -20,7 +23,9 @@ def _get_source_filename():
2023 srcdir = sysconfig .get_config_var ('srcdir' )
2124 return os .path .join (srcdir , 'Modules' , 'xxmodule.c' )
2225
23- class BuildExtTestCase (TempdirManager , unittest .TestCase ):
26+ class BuildExtTestCase (TempdirManager ,
27+ LoggingSilencer ,
28+ unittest .TestCase ):
2429 def setUp (self ):
2530 # Create a simple test environment
2631 # Note that we're making changes to sys.path
@@ -141,6 +146,22 @@ def test_user_site(self):
141146 self .assert_ (lib in cmd .library_dirs )
142147 self .assert_ (incl in cmd .include_dirs )
143148
149+ def test_optional_extension (self ):
150+
151+ # this extension will fail, but let's ignore this failure
152+ # with the optional argument.
153+ modules = [Extension ('foo' , ['xxx' ], optional = False )]
154+ dist = Distribution ({'name' : 'xx' , 'ext_modules' : modules })
155+ cmd = build_ext (dist )
156+ cmd .ensure_finalized ()
157+ self .assertRaises (UnknownFileError , cmd .run ) # should raise an error
158+
159+ modules = [Extension ('foo' , ['xxx' ], optional = True )]
160+ dist = Distribution ({'name' : 'xx' , 'ext_modules' : modules })
161+ cmd = build_ext (dist )
162+ cmd .ensure_finalized ()
163+ cmd .run () # should pass
164+
144165def test_suite ():
145166 src = _get_source_filename ()
146167 if not os .path .exists (src ):
0 commit comments