blob: 601b68686be26ac17c8bf6d9e39307a3431bc779 [file] [log] [blame]
Tarek Ziadéf396ecf2009-04-11 15:00:43 +00001"""Tests for distutils.command.check."""
2import unittest
Éric Araujo70ec44a2010-11-06 02:44:43 +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 Melottib3aedd42010-11-20 19:04:17 +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 Melottib3aedd42010-11-20 19:04:17 +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 Melottib3aedd42010-11-20 19:04:17 +000047 self.assertEqual(cmd._warnings, 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000048
Éric Araujo5819dcc2011-10-09 07:25:33 +020049 # now a test with non-ASCII characters
50 metadata = {'url': 'xxx', 'author': '\u00c9ric',
51 'author_email': 'xxx', 'name': 'xxx',
52 'version': 'xxx',
53 'description': 'Something about esszet \u00df',
54 'long_description': 'More things about esszet \u00df'}
55 cmd = self._run(metadata)
56 self.assertEqual(cmd._warnings, 0)
57
Serhiy Storchaka3c02ece2013-12-18 16:41:01 +020058 @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000059 def test_check_document(self):
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000060 pkg_info, dist = self.create_dist()
61 cmd = check(dist)
62
63 # let's see if it detects broken rest
64 broken_rest = 'title\n===\n\ntest'
65 msgs = cmd._check_rst_data(broken_rest)
Ezio Melottib3aedd42010-11-20 19:04:17 +000066 self.assertEqual(len(msgs), 1)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000067
68 # and non-broken rest
69 rest = 'title\n=====\n\ntest'
70 msgs = cmd._check_rst_data(rest)
Ezio Melottib3aedd42010-11-20 19:04:17 +000071 self.assertEqual(len(msgs), 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000072
Serhiy Storchaka3c02ece2013-12-18 16:41:01 +020073 @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000074 def test_check_restructuredtext(self):
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000075 # let's see if it detects broken rest in long_description
76 broken_rest = 'title\n===\n\ntest'
77 pkg_info, dist = self.create_dist(long_description=broken_rest)
78 cmd = check(dist)
79 cmd.check_restructuredtext()
Ezio Melottib3aedd42010-11-20 19:04:17 +000080 self.assertEqual(cmd._warnings, 1)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000081
82 # let's see if we have an error with strict=1
Tarek Ziadéf633ff42009-04-17 14:34:49 +000083 metadata = {'url': 'xxx', 'author': 'xxx',
84 'author_email': 'xxx',
85 'name': 'xxx', 'version': 'xxx',
86 'long_description': broken_rest}
87 self.assertRaises(DistutilsSetupError, self._run, metadata,
88 **{'strict': 1, 'restructuredtext': 1})
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000089
Éric Araujo5819dcc2011-10-09 07:25:33 +020090 # and non-broken rest, including a non-ASCII character to test #12114
91 metadata['long_description'] = 'title\n=====\n\ntest \u00df'
Tarek Ziadéf633ff42009-04-17 14:34:49 +000092 cmd = self._run(metadata, strict=1, restructuredtext=1)
Ezio Melottib3aedd42010-11-20 19:04:17 +000093 self.assertEqual(cmd._warnings, 0)
Tarek Ziadéf396ecf2009-04-11 15:00:43 +000094
Tarek Ziadéaa4398b2009-04-11 15:17:04 +000095 def test_check_all(self):
96
97 metadata = {'url': 'xxx', 'author': 'xxx'}
98 self.assertRaises(DistutilsSetupError, self._run,
99 {}, **{'strict': 1,
100 'restructuredtext': 1})
101
Tarek Ziadéf396ecf2009-04-11 15:00:43 +0000102def test_suite():
103 return unittest.makeSuite(CheckTestCase)
104
105if __name__ == "__main__":
Éric Araujo70ec44a2010-11-06 02:44:43 +0000106 run_unittest(test_suite())