test_doc.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. '''
  3. tests.unit.doc_test
  4. ~~~~~~~~~~~~~~~~~~~~
  5. '''
  6. # Import Python libs
  7. from __future__ import absolute_import
  8. import os
  9. import re
  10. import logging
  11. # Import Salt Testing libs
  12. from tests.support.unit import TestCase
  13. from tests.support.runtests import RUNTIME_VARS
  14. # Import Salt libs
  15. import salt.modules.cmdmod
  16. import salt.utils.platform
  17. log = logging.getLogger(__name__)
  18. class DocTestCase(TestCase):
  19. '''
  20. Unit test case for testing doc files and strings.
  21. '''
  22. def test_check_for_doc_inline_markup(self):
  23. '''
  24. We should not be using the ``:doc:`` inline markup option when
  25. cross-referencing locations. Use ``:ref:`` or ``:mod:`` instead.
  26. This test checks for reference to ``:doc:`` usage.
  27. See Issue #12788 for more information.
  28. https://github.com/saltstack/salt/issues/12788
  29. '''
  30. salt_dir = RUNTIME_VARS.CODE_DIR
  31. if salt.utils.platform.is_windows():
  32. if salt.utils.path.which('bash'):
  33. # Use grep from git-bash when it exists.
  34. cmd = 'bash -c \'grep -r :doc: ./salt/'
  35. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd, cwd=salt_dir).split(os.linesep)
  36. else:
  37. # No grep in Windows, use findstr
  38. # findstr in windows doesn't prepend 'Binary` to binary files, so
  39. # use the '/P' switch to skip files with unprintable characters
  40. cmd = 'findstr /C:":doc:" /S /P {0}\\*'.format(salt_dir)
  41. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  42. else:
  43. salt_dir += '/'
  44. cmd = 'grep -r :doc: ' + salt_dir
  45. grep_call = salt.modules.cmdmod.run_stdout(cmd=cmd).split(os.linesep)
  46. test_ret = {}
  47. for line in grep_call:
  48. # Skip any .pyc files that may be present
  49. if line.startswith('Binary'):
  50. continue
  51. # Only split on colons not followed by a '\' as is the case with
  52. # Windows Drives
  53. regex = re.compile(r':(?!\\)')
  54. try:
  55. key, val = regex.split(line, 1)
  56. except ValueError:
  57. log.error("Could not split line: %s", line)
  58. continue
  59. # Don't test man pages, this file, the tox or nox virtualenv files,
  60. # the page that documents to not use ":doc:", the doc/conf.py file
  61. # or the artifacts directory on nox CI test runs
  62. if 'man' in key \
  63. or '.tox{}'.format(os.sep) in key \
  64. or '.nox{}'.format(os.sep) in key \
  65. or 'artifacts{}'.format(os.sep) in key \
  66. or key.endswith('test_doc.py') \
  67. or key.endswith(os.sep.join(['doc', 'conf.py'])) \
  68. or key.endswith(os.sep.join(['conventions', 'documentation.rst'])) \
  69. or key.endswith(os.sep.join(['doc', 'topics', 'releases', '2016.11.2.rst'])) \
  70. or key.endswith(os.sep.join(['doc', 'topics', 'releases', '2016.11.3.rst'])) \
  71. or key.endswith(os.sep.join(['doc', 'topics', 'releases', '2016.3.5.rst'])):
  72. continue
  73. # Set up test return dict
  74. if test_ret.get(key) is None:
  75. test_ret[key] = [val.strip()]
  76. else:
  77. test_ret[key].append(val.strip())
  78. # Allow test results to show files with :doc: ref, rather than truncating
  79. self.maxDiff = None
  80. # test_ret should be empty, otherwise there are :doc: references present
  81. self.assertEqual(test_ret, {})