Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittest_checker_similar.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (c) 2010, 2012, 2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2012 Ry4an Brase <ry4an-hg@ry4an.org>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  5. # Copyright (c) 2016-2017 Claudiu Popa <pcmanticore@gmail.com>
  6. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  7. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  8. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  9. import sys
  10. from os.path import join, dirname, abspath
  11. import six
  12. import pytest
  13. from pylint.checkers import similar
  14. SIMILAR1 = join(dirname(abspath(__file__)), 'input', 'similar1')
  15. SIMILAR2 = join(dirname(abspath(__file__)), 'input', 'similar2')
  16. def test_ignore_comments():
  17. sys.stdout = six.StringIO()
  18. with pytest.raises(SystemExit) as ex:
  19. similar.Run(['--ignore-comments', SIMILAR1, SIMILAR2])
  20. assert ex.value.code == 0
  21. output = sys.stdout.getvalue()
  22. sys.stdout = sys.__stdout__
  23. assert output.strip() == ("""
  24. 10 similar lines in 2 files
  25. ==%s:0
  26. ==%s:0
  27. import one
  28. from two import two
  29. three
  30. four
  31. five
  32. six
  33. seven
  34. eight
  35. nine
  36. ''' ten
  37. TOTAL lines=44 duplicates=10 percent=22.73
  38. """ % (SIMILAR1, SIMILAR2)).strip()
  39. def test_ignore_docsrings():
  40. sys.stdout = six.StringIO()
  41. with pytest.raises(SystemExit) as ex:
  42. similar.Run(['--ignore-docstrings', SIMILAR1, SIMILAR2])
  43. assert ex.value.code == 0
  44. output = sys.stdout.getvalue()
  45. sys.stdout = sys.__stdout__
  46. assert output.strip() == ("""
  47. 8 similar lines in 2 files
  48. ==%s:6
  49. ==%s:6
  50. seven
  51. eight
  52. nine
  53. ''' ten
  54. ELEVEN
  55. twelve '''
  56. thirteen
  57. fourteen
  58. 5 similar lines in 2 files
  59. ==%s:0
  60. ==%s:0
  61. import one
  62. from two import two
  63. three
  64. four
  65. five
  66. TOTAL lines=44 duplicates=13 percent=29.55
  67. """ % ((SIMILAR1, SIMILAR2) * 2)).strip()
  68. def test_ignore_imports():
  69. sys.stdout = six.StringIO()
  70. with pytest.raises(SystemExit) as ex:
  71. similar.Run(['--ignore-imports', SIMILAR1, SIMILAR2])
  72. assert ex.value.code == 0
  73. output = sys.stdout.getvalue()
  74. sys.stdout = sys.__stdout__
  75. assert output.strip() == """
  76. TOTAL lines=44 duplicates=0 percent=0.00
  77. """.strip()
  78. def test_ignore_nothing():
  79. sys.stdout = six.StringIO()
  80. with pytest.raises(SystemExit) as ex:
  81. similar.Run([SIMILAR1, SIMILAR2])
  82. assert ex.value.code == 0
  83. output = sys.stdout.getvalue()
  84. sys.stdout = sys.__stdout__
  85. assert output.strip() == ("""
  86. 5 similar lines in 2 files
  87. ==%s:0
  88. ==%s:0
  89. import one
  90. from two import two
  91. three
  92. four
  93. five
  94. TOTAL lines=44 duplicates=5 percent=11.36
  95. """ % (SIMILAR1, SIMILAR2)).strip()
  96. def test_help():
  97. sys.stdout = six.StringIO()
  98. try:
  99. similar.Run(['--help'])
  100. except SystemExit as ex:
  101. assert ex.code == 0
  102. else:
  103. pytest.fail('not system exit')
  104. finally:
  105. sys.stdout = sys.__stdout__
  106. def test_no_args():
  107. sys.stdout = six.StringIO()
  108. try:
  109. similar.Run([])
  110. except SystemExit as ex:
  111. assert ex.code == 1
  112. else:
  113. pytest.fail('not system exit')
  114. finally:
  115. sys.stdout = sys.__stdout__