blob: 81058b1911ee2109042edc8878e02c6a44cd8ec8 [file] [log] [blame]
Éric Araujo017e5352011-10-09 07:11:19 +02001# -*- encoding: utf8 -*-
Tarek Ziadé942825f2009-04-11 14:55:07 +00002"""Tests for distutils.command.check."""
Benjamin Peterson95ee9c72015-01-14 23:56:35 -05003import textwrap
Tarek Ziadé942825f2009-04-11 14:55:07 +00004import unittest
Éric Araujo54274ad2011-02-03 00:12:18 +00005from test.test_support import run_unittest
Tarek Ziadé942825f2009-04-11 14:55:07 +00006
7from distutils.command.check import check, HAS_DOCUTILS
8from distutils.tests import support
9from distutils.errors import DistutilsSetupError
10
11class CheckTestCase(support.LoggingSilencer,
12 support.TempdirManager,
13 unittest.TestCase):
14
15 def _run(self, metadata=None, **options):
16 if metadata is None:
17 metadata = {}
18 pkg_info, dist = self.create_dist(**metadata)
19 cmd = check(dist)
20 cmd.initialize_options()
21 for name, value in options.items():
22 setattr(cmd, name, value)
23 cmd.ensure_finalized()
24 cmd.run()
25 return cmd
26
27 def test_check_metadata(self):
28 # let's run the command with no metadata at all
29 # by default, check is checking the metadata
30 # should have some warnings
31 cmd = self._run()
Ezio Melotti2623a372010-11-21 13:34:58 +000032 self.assertEqual(cmd._warnings, 2)
Tarek Ziadé942825f2009-04-11 14:55:07 +000033
34 # now let's add the required fields
35 # and run it again, to make sure we don't get
36 # any warning anymore
37 metadata = {'url': 'xxx', 'author': 'xxx',
38 'author_email': 'xxx',
39 'name': 'xxx', 'version': 'xxx'}
40 cmd = self._run(metadata)
Ezio Melotti2623a372010-11-21 13:34:58 +000041 self.assertEqual(cmd._warnings, 0)
Tarek Ziadé942825f2009-04-11 14:55:07 +000042
43 # now with the strict mode, we should
44 # get an error if there are missing metadata
45 self.assertRaises(DistutilsSetupError, self._run, {}, **{'strict': 1})
46
47 # and of course, no error when all metadata are present
48 cmd = self._run(metadata, strict=1)
Ezio Melotti2623a372010-11-21 13:34:58 +000049 self.assertEqual(cmd._warnings, 0)
Tarek Ziadé942825f2009-04-11 14:55:07 +000050
Éric Araujo017e5352011-10-09 07:11:19 +020051 # now a test with Unicode entries
52 metadata = {'url': u'xxx', 'author': u'\u00c9ric',
53 'author_email': u'xxx', u'name': 'xxx',
54 'version': u'xxx',
55 'description': u'Something about esszet \u00df',
56 'long_description': u'More things about esszet \u00df'}
57 cmd = self._run(metadata)
58 self.assertEqual(cmd._warnings, 0)
59
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020060 @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
Tarek Ziadé942825f2009-04-11 14:55:07 +000061 def test_check_document(self):
Tarek Ziadé942825f2009-04-11 14:55:07 +000062 pkg_info, dist = self.create_dist()
63 cmd = check(dist)
64
65 # let's see if it detects broken rest
66 broken_rest = 'title\n===\n\ntest'
67 msgs = cmd._check_rst_data(broken_rest)
Ezio Melotti2623a372010-11-21 13:34:58 +000068 self.assertEqual(len(msgs), 1)
Tarek Ziadé942825f2009-04-11 14:55:07 +000069
70 # and non-broken rest
71 rest = 'title\n=====\n\ntest'
72 msgs = cmd._check_rst_data(rest)
Ezio Melotti2623a372010-11-21 13:34:58 +000073 self.assertEqual(len(msgs), 0)
Tarek Ziadé942825f2009-04-11 14:55:07 +000074
Serhiy Storchaka57bc6da2013-12-18 16:45:37 +020075 @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
Tarek Ziadé942825f2009-04-11 14:55:07 +000076 def test_check_restructuredtext(self):
Tarek Ziadé942825f2009-04-11 14:55:07 +000077 # let's see if it detects broken rest in long_description
78 broken_rest = 'title\n===\n\ntest'
79 pkg_info, dist = self.create_dist(long_description=broken_rest)
80 cmd = check(dist)
81 cmd.check_restructuredtext()
Ezio Melotti2623a372010-11-21 13:34:58 +000082 self.assertEqual(cmd._warnings, 1)
Tarek Ziadé942825f2009-04-11 14:55:07 +000083
84 # let's see if we have an error with strict=1
Tarek Ziadé783f4932009-04-17 14:29:56 +000085 metadata = {'url': 'xxx', 'author': 'xxx',
86 'author_email': 'xxx',
87 'name': 'xxx', 'version': 'xxx',
88 'long_description': broken_rest}
89 self.assertRaises(DistutilsSetupError, self._run, metadata,
90 **{'strict': 1, 'restructuredtext': 1})
Tarek Ziadé942825f2009-04-11 14:55:07 +000091
Éric Araujo017e5352011-10-09 07:11:19 +020092 # and non-broken rest, including a non-ASCII character to test #12114
93 metadata['long_description'] = u'title\n=====\n\ntest \u00df'
Tarek Ziadé783f4932009-04-17 14:29:56 +000094 cmd = self._run(metadata, strict=1, restructuredtext=1)
Ezio Melotti2623a372010-11-21 13:34:58 +000095 self.assertEqual(cmd._warnings, 0)
Tarek Ziadé942825f2009-04-11 14:55:07 +000096
Benjamin Peterson95ee9c72015-01-14 23:56:35 -050097 @unittest.skipUnless(HAS_DOCUTILS, "won't test without docutils")
98 def test_check_restructuredtext_with_syntax_highlight(self):
99 # Don't fail if there is a `code` or `code-block` directive
100
101 example_rst_docs = []
102 example_rst_docs.append(textwrap.dedent("""\
103 Here's some code:
104
105 .. code:: python
106
107 def foo():
108 pass
109 """))
110 example_rst_docs.append(textwrap.dedent("""\
111 Here's some code:
112
113 .. code-block:: python
114
115 def foo():
116 pass
117 """))
118
119 for rest_with_code in example_rst_docs:
120 pkg_info, dist = self.create_dist(long_description=rest_with_code)
121 cmd = check(dist)
122 cmd.check_restructuredtext()
123 self.assertEqual(cmd._warnings, 0)
124 msgs = cmd._check_rst_data(rest_with_code)
125 self.assertEqual(len(msgs), 0)
126
Tarek Ziadéc2936b72009-04-11 15:14:17 +0000127 def test_check_all(self):
128
129 metadata = {'url': 'xxx', 'author': 'xxx'}
130 self.assertRaises(DistutilsSetupError, self._run,
131 {}, **{'strict': 1,
132 'restructuredtext': 1})
133
Tarek Ziadé942825f2009-04-11 14:55:07 +0000134def test_suite():
135 return unittest.makeSuite(CheckTestCase)
136
137if __name__ == "__main__":
Éric Araujo54274ad2011-02-03 00:12:18 +0000138 run_unittest(test_suite())