From 7a2b0e6061e24e769c2540c8662a4ee18243a0b8 Mon Sep 17 00:00:00 2001 From: Ben Steadman Date: Wed, 20 Mar 2019 19:38:15 +0000 Subject: [PATCH] contributors.py Python3 refactor --- test/test_all.py | 9 ++- test/test_contributors.py | 160 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 test/test_contributors.py diff --git a/test/test_all.py b/test/test_all.py index 2d6b110..bc0d8bd 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -2,7 +2,10 @@ import unittest # For convenience -import sys,os,logging +import sys +import os +import logging + sys.path.insert(0, os.getcwd()) sys.path.insert(0, os.path.join(os.getcwd(), os.pardir)) @@ -12,15 +15,19 @@ from test_rendertileset import RendertileSetTest from test_settings import SettingsTest from test_tileset import TilesetTest from test_cache import TestLRU +from test_contributors import TestContributors # DISABLE THIS BLOCK TO GET LOG OUTPUT FROM TILESET FOR DEBUGGING if 0: root = logging.getLogger() + class NullHandler(logging.Handler): def handle(self, record): pass + def emit(self, record): pass + def createLock(self): self.lock = None root.addHandler(NullHandler()) diff --git a/test/test_contributors.py b/test/test_contributors.py new file mode 100644 index 0000000..1a1c057 --- /dev/null +++ b/test/test_contributors.py @@ -0,0 +1,160 @@ +import unittest +from io import StringIO, BytesIO +from textwrap import dedent +from unittest.mock import patch + +import contrib.contributors as contrib + + +class TestContributors(unittest.TestCase): + def setUp(self): + self.contrib_file_lines = dedent("""\ + ============ + Contributors + ============ + + This file contains a list of every person who has contributed code to + Overviewer. + + --------------- + Original Author + --------------- + + * Andrew Brown + + ------------------------- + Long-term Contributions + ------------------------- + + These contributors have made many changes, over a fairly long time span, or + for many different parts of the code. + + * Alejandro Aguilera + + ------------------------ + Short-term Contributions + ------------------------ + + These contributors have made specific changes for a particular bug fix or + feature. + + * 3decibels <3db@3decibels.net>""").split("\n") + + def test_format_contributor_single_name(self): + contributor = {"name": "John", "email": ""} + self.assertEqual( + contrib.format_contributor(contributor), + " * John " + ) + + def test_format_contributor_multiple_names(self): + contributor = {"name": "John Smith", "email": ""} + self.assertEqual( + contrib.format_contributor(contributor), + " * John Smith " + ) + + def test_get_old_contributors(self): + expected = [{"name": "Andrew Brown", "email": ""}, + {"name": "Alejandro Aguilera", "email": ""}, + {"name": "3decibels", "email": "<3db@3decibels.net>"}] + + self.assertListEqual(contrib.get_old_contributors(self.contrib_file_lines), expected) + + @patch('subprocess.run') + def test_get_contributors(self, mock_run): + mock_run.return_value.stdout = dedent("""\ + 1 3decibels <3db@3decibels.net> + 585 Aaron Griffith + 1 Aaron1011 + """).encode() + expected = [{"count": 1, "name": "3decibels", "email": "<3db@3decibels.net>"}, + {"count": 585, "name": "Aaron Griffith", "email": ""}, + {"count": 1, "name": "Aaron1011", "email": ""}] + self.assertListEqual(contrib.get_contributors(), expected) + + def test_get_new_contributors_new_contributors_alphabetical_order(self): + contributors = [{"count": 1, "name": "3decibels", "email": "<3db@3decibels.net>"}, + {"count": 585, "name": "Aaron Griffith", "email": ""}, + {"count": 1, "name": "Aaron1011", "email": ""}] + + old_contributors = [{"name": "Andrew Brown", "email": ""}, + {"name": "Alejandro Aguilera", "email": ""}, + {"name": "3decibels", "email": "<3db@3decibels.net>"}] + + new_contributors, new_alias, new_email = contrib.get_new_contributors( + contributors, old_contributors) + + self.assertListEqual(new_contributors, [{"count": 1, "name": "Aaron1011", "email": ""}, { + "count": 585, "name": "Aaron Griffith", "email": ""}]) + + def test_get_new_contributors_new_alias(self): + contributors = [{"count": 1, "name": "new_name", "email": "<3db@3decibels.net>"}, + {"count": 585, "name": "Aaron Griffith", "email": ""}, + {"count": 1, "name": "Aaron1011", "email": ""}] + + old_contributors = [{"name": "Andrew Brown", "email": ""}, + {"name": "Alejandro Aguilera", "email": ""}, + {"name": "3decibels", "email": "<3db@3decibels.net>"}] + + new_contributors, new_alias, new_email = contrib.get_new_contributors( + contributors, old_contributors) + self.assertListEqual( + new_alias, [({"count": 1, "name": "new_name", "email": "<3db@3decibels.net>"}, "3decibels")]) + + def test_get_new_contributors_new_email(self): + contributors = [{"count": 1, "name": "3decibels", "email": "<3db@3decibels.com>"}, + {"count": 585, "name": "Aaron Griffith", "email": ""}, + {"count": 1, "name": "Aaron1011", "email": ""}] + + old_contributors = [{"name": "Andrew Brown", "email": ""}, + {"name": "Alejandro Aguilera", "email": ""}, + {"name": "3decibels", "email": "<3db@3decibels.net>"}] + + new_contributors, new_alias, new_email = contrib.get_new_contributors( + contributors, old_contributors) + self.assertListEqual( + new_email, [({"count": 1, "name": "3decibels", "email": "<3db@3decibels.com>"}, "<3db@3decibels.net>")]) + + def test_merge_short_term_contributors(self): + new_contributors = [{"count": 1, "name": "Aaron1011", "email": ""}, { + "count": 585, "name": "Aaron Griffith", "email": ""}] + expected = ['============', + 'Contributors', + '============', + '', + 'This file contains a list of every person who has contributed code to', + 'Overviewer.', + '', + '---------------', + 'Original Author', + '---------------', + '', + ' * Andrew Brown ', + '', + '-------------------------', + 'Long-term Contributions', + '-------------------------', + '', + 'These contributors have made many changes, over a fairly long time span, or', + 'for many different parts of the code.', + '', + ' * Alejandro Aguilera ', + '', + '------------------------', + 'Short-term Contributions', + '------------------------', + '', + 'These contributors have made specific changes for a particular bug fix or', + 'feature.', + '', + ' * 3decibels <3db@3decibels.net>', + ' * Aaron1011 \n', + ' * Aaron Griffith \n'] + + self.assertListEqual(contrib.merge_short_term_contributors( + self.contrib_file_lines, new_contributors), expected) + + +if __name__ == "__main__": + unittest.main()