]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/distutils/tests/test_dist.py
3 """Tests for distutils.dist."""
11 from distutils
.dist
import Distribution
, fix_help_options
, DistributionMetadata
12 from distutils
.cmd
import Command
14 from test
.test_support
import TESTFN
, captured_stdout
, run_unittest
15 from distutils
.tests
import support
17 class test_dist(Command
):
18 """Sample distutils extension command."""
21 ("sample-option=", "S", "help text"),
24 def initialize_options(self
):
25 self
.sample_option
= None
28 class TestDistribution(Distribution
):
29 """Distribution subclasses that avoids the default search for
32 The ._config_files attribute must be set before
33 .parse_config_files() is called.
36 def find_config_files(self
):
37 return self
._config
_files
40 class DistributionTestCase(support
.TempdirManager
,
41 support
.LoggingSilencer
,
46 super(DistributionTestCase
, self
).setUp()
47 self
.argv
= sys
.argv
, sys
.argv
[:]
51 sys
.argv
= self
.argv
[0]
52 sys
.argv
[:] = self
.argv
[1]
53 super(DistributionTestCase
, self
).tearDown()
55 def create_distribution(self
, configfiles
=()):
56 d
= TestDistribution()
57 d
._config
_files
= configfiles
58 d
.parse_config_files()
59 d
.parse_command_line()
62 def test_debug_mode(self
):
63 with
open(TESTFN
, "w") as f
:
65 f
.write("command_packages = foo.bar, splat")
68 sys
.argv
.append("build")
70 with
captured_stdout() as stdout
:
71 self
.create_distribution(files
)
73 self
.assertEqual(stdout
.read(), '')
74 distutils
.dist
.DEBUG
= True
76 with
captured_stdout() as stdout
:
77 self
.create_distribution(files
)
79 self
.assertEqual(stdout
.read(), '')
81 distutils
.dist
.DEBUG
= False
83 def test_command_packages_unspecified(self
):
84 sys
.argv
.append("build")
85 d
= self
.create_distribution()
86 self
.assertEqual(d
.get_command_packages(), ["distutils.command"])
88 def test_command_packages_cmdline(self
):
89 from distutils
.tests
.test_dist
import test_dist
90 sys
.argv
.extend(["--command-packages",
91 "foo.bar,distutils.tests",
95 d
= self
.create_distribution()
96 # let's actually try to load our test command:
97 self
.assertEqual(d
.get_command_packages(),
98 ["distutils.command", "foo.bar", "distutils.tests"])
99 cmd
= d
.get_command_obj("test_dist")
100 self
.assertTrue(isinstance(cmd
, test_dist
))
101 self
.assertEqual(cmd
.sample_option
, "sometext")
103 def test_command_packages_configfile(self
):
104 sys
.argv
.append("build")
105 self
.addCleanup(os
.unlink
, TESTFN
)
106 f
= open(TESTFN
, "w")
108 print >>f
, "[global]"
109 print >>f
, "command_packages = foo.bar, splat"
113 d
= self
.create_distribution([TESTFN
])
114 self
.assertEqual(d
.get_command_packages(),
115 ["distutils.command", "foo.bar", "splat"])
117 # ensure command line overrides config:
118 sys
.argv
[1:] = ["--command-packages", "spork", "build"]
119 d
= self
.create_distribution([TESTFN
])
120 self
.assertEqual(d
.get_command_packages(),
121 ["distutils.command", "spork"])
123 # Setting --command-packages to '' should cause the default to
124 # be used even if a config file specified something else:
125 sys
.argv
[1:] = ["--command-packages", "", "build"]
126 d
= self
.create_distribution([TESTFN
])
127 self
.assertEqual(d
.get_command_packages(), ["distutils.command"])
129 def test_write_pkg_file(self
):
130 # Check DistributionMetadata handling of Unicode fields
131 tmp_dir
= self
.mkdtemp()
132 my_file
= os
.path
.join(tmp_dir
, 'f')
135 dist
= klass(attrs
={'author': u
'Mister Café',
136 'name': 'my.package',
137 'maintainer': u
'Café Junior',
138 'description': u
'Café torréfié',
139 'long_description': u
'Héhéhé'})
142 # let's make sure the file can be written
143 # with Unicode fields. they are encoded with
145 dist
.metadata
.write_pkg_file(open(my_file
, 'w'))
147 # regular ascii is of course always usable
148 dist
= klass(attrs
={'author': 'Mister Cafe',
149 'name': 'my.package',
150 'maintainer': 'Cafe Junior',
151 'description': 'Cafe torrefie',
152 'long_description': 'Hehehe'})
154 my_file2
= os
.path
.join(tmp_dir
, 'f2')
155 dist
.metadata
.write_pkg_file(open(my_file
, 'w'))
157 def test_empty_options(self
):
158 # an empty options dictionary should not stay in the
167 old_warn
= warnings
.warn
168 warnings
.warn
= _warn
170 dist
= klass(attrs
={'author': 'xxx',
176 warnings
.warn
= old_warn
178 self
.assertEqual(len(warns
), 0)
180 def test_finalize_options(self
):
182 attrs
= {'keywords': 'one,two',
183 'platforms': 'one,two'}
185 dist
= Distribution(attrs
=attrs
)
186 dist
.finalize_options()
188 # finalize_option splits platforms and keywords
189 self
.assertEqual(dist
.metadata
.platforms
, ['one', 'two'])
190 self
.assertEqual(dist
.metadata
.keywords
, ['one', 'two'])
192 def test_get_command_packages(self
):
193 dist
= Distribution()
194 self
.assertEqual(dist
.command_packages
, None)
195 cmds
= dist
.get_command_packages()
196 self
.assertEqual(cmds
, ['distutils.command'])
197 self
.assertEqual(dist
.command_packages
,
198 ['distutils.command'])
200 dist
.command_packages
= 'one,two'
201 cmds
= dist
.get_command_packages()
202 self
.assertEqual(cmds
, ['distutils.command', 'one', 'two'])
205 def test_announce(self
):
206 # make sure the level is known
207 dist
= Distribution()
209 kwargs
= {'level': 'ok2'}
210 self
.assertRaises(ValueError, dist
.announce
, args
, kwargs
)
212 def test_find_config_files_disable(self
):
213 # Ticket #1180: Allow user to disable their home config file.
214 temp_home
= self
.mkdtemp()
215 if os
.name
== 'posix':
216 user_filename
= os
.path
.join(temp_home
, ".pydistutils.cfg")
218 user_filename
= os
.path
.join(temp_home
, "pydistutils.cfg")
220 with
open(user_filename
, 'w') as f
:
221 f
.write('[distutils]\n')
226 old_expander
= os
.path
.expanduser
227 os
.path
.expanduser
= _expander
229 d
= distutils
.dist
.Distribution()
230 all_files
= d
.find_config_files()
232 d
= distutils
.dist
.Distribution(attrs
={'script_args':
234 files
= d
.find_config_files()
236 os
.path
.expanduser
= old_expander
238 # make sure --no-user-cfg disables the user cfg file
239 self
.assertEqual(len(all_files
)-1, len(files
))
242 class MetadataTestCase(support
.TempdirManager
, support
.EnvironGuard
,
246 super(MetadataTestCase
, self
).setUp()
247 self
.argv
= sys
.argv
, sys
.argv
[:]
250 sys
.argv
= self
.argv
[0]
251 sys
.argv
[:] = self
.argv
[1]
252 super(MetadataTestCase
, self
).tearDown()
254 def test_simple_metadata(self
):
255 attrs
= {"name": "package",
257 dist
= Distribution(attrs
)
258 meta
= self
.format_metadata(dist
)
259 self
.assertTrue("Metadata-Version: 1.0" in meta
)
260 self
.assertTrue("provides:" not in meta
.lower())
261 self
.assertTrue("requires:" not in meta
.lower())
262 self
.assertTrue("obsoletes:" not in meta
.lower())
264 def test_provides(self
):
265 attrs
= {"name": "package",
267 "provides": ["package", "package.sub"]}
268 dist
= Distribution(attrs
)
269 self
.assertEqual(dist
.metadata
.get_provides(),
270 ["package", "package.sub"])
271 self
.assertEqual(dist
.get_provides(),
272 ["package", "package.sub"])
273 meta
= self
.format_metadata(dist
)
274 self
.assertTrue("Metadata-Version: 1.1" in meta
)
275 self
.assertTrue("requires:" not in meta
.lower())
276 self
.assertTrue("obsoletes:" not in meta
.lower())
278 def test_provides_illegal(self
):
279 self
.assertRaises(ValueError, Distribution
,
282 "provides": ["my.pkg (splat)"]})
284 def test_requires(self
):
285 attrs
= {"name": "package",
287 "requires": ["other", "another (==1.0)"]}
288 dist
= Distribution(attrs
)
289 self
.assertEqual(dist
.metadata
.get_requires(),
290 ["other", "another (==1.0)"])
291 self
.assertEqual(dist
.get_requires(),
292 ["other", "another (==1.0)"])
293 meta
= self
.format_metadata(dist
)
294 self
.assertTrue("Metadata-Version: 1.1" in meta
)
295 self
.assertTrue("provides:" not in meta
.lower())
296 self
.assertTrue("Requires: other" in meta
)
297 self
.assertTrue("Requires: another (==1.0)" in meta
)
298 self
.assertTrue("obsoletes:" not in meta
.lower())
300 def test_requires_illegal(self
):
301 self
.assertRaises(ValueError, Distribution
,
304 "requires": ["my.pkg (splat)"]})
306 def test_obsoletes(self
):
307 attrs
= {"name": "package",
309 "obsoletes": ["other", "another (<1.0)"]}
310 dist
= Distribution(attrs
)
311 self
.assertEqual(dist
.metadata
.get_obsoletes(),
312 ["other", "another (<1.0)"])
313 self
.assertEqual(dist
.get_obsoletes(),
314 ["other", "another (<1.0)"])
315 meta
= self
.format_metadata(dist
)
316 self
.assertTrue("Metadata-Version: 1.1" in meta
)
317 self
.assertTrue("provides:" not in meta
.lower())
318 self
.assertTrue("requires:" not in meta
.lower())
319 self
.assertTrue("Obsoletes: other" in meta
)
320 self
.assertTrue("Obsoletes: another (<1.0)" in meta
)
322 def test_obsoletes_illegal(self
):
323 self
.assertRaises(ValueError, Distribution
,
326 "obsoletes": ["my.pkg (splat)"]})
328 def format_metadata(self
, dist
):
329 sio
= StringIO
.StringIO()
330 dist
.metadata
.write_pkg_file(sio
)
331 return sio
.getvalue()
333 def test_custom_pydistutils(self
):
335 # make sure pydistutils.cfg is found
336 if os
.name
== 'posix':
337 user_filename
= ".pydistutils.cfg"
339 user_filename
= "pydistutils.cfg"
341 temp_dir
= self
.mkdtemp()
342 user_filename
= os
.path
.join(temp_dir
, user_filename
)
343 f
= open(user_filename
, 'w')
350 dist
= Distribution()
353 if sys
.platform
in ('linux', 'darwin'):
354 os
.environ
['HOME'] = temp_dir
355 files
= dist
.find_config_files()
356 self
.assertTrue(user_filename
in files
)
359 if sys
.platform
== 'win32':
360 # home drive should be found
361 os
.environ
['HOME'] = temp_dir
362 files
= dist
.find_config_files()
363 self
.assertTrue(user_filename
in files
,
364 '%r not found in %r' % (user_filename
, files
))
366 os
.remove(user_filename
)
368 def test_fix_help_options(self
):
369 help_tuples
= [('a', 'b', 'c', 'd'), (1, 2, 3, 4)]
370 fancy_options
= fix_help_options(help_tuples
)
371 self
.assertEqual(fancy_options
[0], ('a', 'b', 'c'))
372 self
.assertEqual(fancy_options
[1], (1, 2, 3))
374 def test_show_help(self
):
375 # smoke test, just makes sure some help is displayed
376 dist
= Distribution()
379 dist
.script_name
= 'setup.py'
380 with
captured_stdout() as s
:
381 dist
.parse_command_line()
383 output
= [line
for line
in s
.getvalue().split('\n')
384 if line
.strip() != '']
385 self
.assertTrue(len(output
) > 0)
387 def test_long_description(self
):
388 long_desc
= textwrap
.dedent("""\
393 attrs
= {"name": "package",
395 "long_description": long_desc
}
397 dist
= distutils
.dist
.Distribution(attrs
)
398 meta
= self
.format_metadata(dist
)
399 meta
= meta
.replace('\n' + 8 * ' ', '\n')
400 self
.assertTrue(long_desc
in meta
)
402 def test_read_metadata(self
):
403 attrs
= {"name": "package",
405 "long_description": "desc",
406 "description": "xxx",
407 "download_url": "http://example.com",
408 "keywords": ['one', 'two'],
411 dist
= Distribution(attrs
)
412 metadata
= dist
.metadata
414 # write it then reloads it
415 PKG_INFO
= StringIO
.StringIO()
416 metadata
.write_pkg_file(PKG_INFO
)
418 metadata
.read_pkg_file(PKG_INFO
)
420 self
.assertEqual(metadata
.name
, "package")
421 self
.assertEqual(metadata
.version
, "1.0")
422 self
.assertEqual(metadata
.description
, "xxx")
423 self
.assertEqual(metadata
.download_url
, 'http://example.com')
424 self
.assertEqual(metadata
.keywords
, ['one', 'two'])
425 self
.assertEqual(metadata
.platforms
, ['UNKNOWN'])
426 self
.assertEqual(metadata
.obsoletes
, None)
427 self
.assertEqual(metadata
.requires
, ['foo'])
430 suite
= unittest
.TestSuite()
431 suite
.addTest(unittest
.makeSuite(DistributionTestCase
))
432 suite
.addTest(unittest
.makeSuite(MetadataTestCase
))
435 if __name__
== "__main__":
436 run_unittest(test_suite())