blob: 229ae25c6da262aeaa60115e152865453636d1fb [file] [log] [blame]
Tarek Ziadéf396ecf2009-04-11 15:00:43 +00001"""Tests for distutils.command.check."""
2import unittest
Éric Araujob344dd02011-02-02 21:38:37 +00003from test.support import run_unittest
Tarek Ziadéf396ecf2009-04-11 15:00:43 +00004
5from distutils.command.check import check, HAS_DOCUTILS
6from distutils.tests import support
7from distutils.errors import DistutilsSetupError
8
9class CheckTestCase(support.LoggingSilencer,
10 support.TempdirManager,
11 unittest.TestCase):
12
13 def _run(self, metadata=None, **options):
14 if metadata is None:
15 metadata = {}
16 pkg_info, dist = self.create_dist(**metadata)
17 cmd = check(dist)
18 cmd.initialize_options()
19 for name, value in options.items():
20 setattr(cmd, name, value)
21 cmd.ensure_finalized()
22 cmd.run()
23 return cmd
24
25 def test_check_metadata(self):
26 # let's run the command with no metadata at all
27 # by default, check is checking the metadata
28 # should have some warnings
29 cmd = self._run()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000030 self.assertEqual(cmd._warnings, 2)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000031
32 # now let's add the required fields
33 # and run it again, to make sure we don't get
34 # any warning anymore
35 metadata = {'url': 'xxx', 'author': 'xxx',
36 'author_email': 'xxx',
37 'name': 'xxx', 'version': 'xxx'}
38 cmd = self._run(metadata)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000039 self.assertEqual(cmd._warnings, 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000040
41 # now with the strict mode, we should
42 # get an error if there are missing metadata
43 self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1})
44
45 # and of course, no error when all metadata are present
46 cmd = self._run(metadata, strict=1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000047 self.assertEqual(cmd._warnings, 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000048
49 def test_check_document(self):
50 if not HAS_DOCUTILS: # won't test without docutils
51 return
52 pkg_info, dist = self.create_dist()
53 cmd = check(dist)
54
55 # let's see if it detects broken rest
56 broken_rest = 'title\n===\n\ntest'
57 msgs = cmd._check_rst_data(broken_rest)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000058 self.assertEqual(len(msgs), 1)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000059
60 # and non-broken rest
61 rest = 'title\n=====\n\ntest'
62 msgs = cmd._check_rst_data(rest)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000063 self.assertEqual(len(msgs), 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000064
65 def test_check_restructuredtext(self):
66 if not HAS_DOCUTILS: # won't test without docutils
67 return
68 # let's see if it detects broken rest in long_description
69 broken_rest = 'title\n===\n\ntest'
70 pkg_info, dist = self.create_dist(long_description=broken_rest)
71 cmd = check(dist)
72 cmd.check_restructuredtext()
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000073 self.assertEqual(cmd._warnings, 1)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000074
75 # let's see if we have an error with strict=1
Tarek Ziadéf633ff42009-04-17 14:34:49 +000076 metadata = {'url': 'xxx', 'author': 'xxx',
77 'author_email': 'xxx',
78 'name': 'xxx', 'version': 'xxx',
79 'long_description': broken_rest}
80 self.assertRaises(DistutilsSetupError, self._run, metadata,
81 **{'strict': 1, 'restructuredtext': 1})
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000082
83 # and non-broken rest
Tarek Ziadéf633ff42009-04-17 14:34:49 +000084 metadata['long_description'] = 'title\n=====\n\ntest'
85 cmd = self._run(metadata, strict=1, restructuredtext=1)
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000086 self.assertEqual(cmd._warnings, 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000087
Tarek Ziadéaa4398b2009-04-11 15:17:04 +000088 def test_check_all(self):
89
90 metadata = {'url': 'xxx', 'author': 'xxx'}
91 self.assertRaises(DistutilsSetupError, self._run,
92 {}, **{'strict': 1,
93 'restructuredtext': 1})
94
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000095def test_suite():
96 return unittest.makeSuite(CheckTestCase)
97
98if __name__ == "__main__":
Éric Araujob344dd02011-02-02 21:38:37 +000099 run_unittest(test_suite())