From c52bd200dbb8c3e4e5258e8e5cb7c80bebeef3cc Mon Sep 17 00:00:00 2001 From: JP White Date: Sat, 13 Mar 2021 23:12:42 -0500 Subject: [PATCH] More tests --- Makefile | 2 +- tests/test_changes.py | 12 +++---- tests/test_config.py | 12 +++---- tests/test_filtering.py | 36 ++++++++++++++++++++ tests/test_format.py | 75 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 tests/test_filtering.py create mode 100644 tests/test_format.py diff --git a/Makefile b/Makefile index 752e510..3c1f6b9 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ lint: ## check style with flake8 flake8 gitinspector tests --count --ignore=E203,E722,W503,E401,C901 --exit-zero --max-complexity=10 --max-line-length=127 --statistics --builtins="_" format: ## auto format all the code with black - black gitinspector --line-length 127 + black ./gitinspector --line-length 127 test: ## run tests quickly with the default Python pytest diff --git a/tests/test_changes.py b/tests/test_changes.py index 593fa4d..eec102e 100644 --- a/tests/test_changes.py +++ b/tests/test_changes.py @@ -65,13 +65,13 @@ class TestFileDiff(unittest.TestCase): self.assertEqual(actual, expected) def test_is_not_valid_extension(self): - result = changes.FileDiff.is_valid_extension(FAKE_FILE_NAME) - self.assertFalse(result) + return_value = changes.FileDiff.is_valid_extension(FAKE_FILE_NAME) + self.assertFalse(return_value) def test_is_valid_extension(self): test_file_name = 'Arbitrary.cpp' - result = changes.FileDiff.is_valid_extension(test_file_name) - self.assertTrue(result) + return_value = changes.FileDiff.is_valid_extension(test_file_name) + self.assertTrue(return_value) class TestCommitClass(unittest.TestCase): @@ -104,8 +104,8 @@ class TestCommitClass(unittest.TestCase): self.assertEqual(expected_email, actual_email) def test_is_commit_line(self): - result = changes.Commit.is_commit_line(FAKE_COMMIT_STRING) - self.assertTrue(result) + return_value = changes.Commit.is_commit_line(FAKE_COMMIT_STRING) + self.assertTrue(return_value) def test_add_filediff(self): commit = changes.Commit(FAKE_COMMIT_STRING) diff --git a/tests/test_config.py b/tests/test_config.py index 07cc7df..a8d330c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -14,16 +14,16 @@ class TestConfig(unittest.TestCase): self.assertEqual(expected_global_only, test_config.global_only) def test_read_git_config_unknown_variable(self): - expected_result = '' + expected_return_value = '' test_config = config.GitConfig('arbitrary', '.') - actual_result = test_config.__read_git_config__('unknown') - self.assertEqual(expected_result, actual_result) + actual_return_value = test_config.__read_git_config__('unknown') + self.assertEqual(expected_return_value, actual_return_value) def test_read_git_config_string_unknown(self): - expected_result = (False, None) + expected_return_value = (False, None) test_config = config.GitConfig('arbitrary', '.') - actual_result = test_config.__read_git_config_string__('unknown') - self.assertEqual(expected_result, actual_result) + actual_return_value = test_config.__read_git_config_string__('unknown') + self.assertEqual(expected_return_value, actual_return_value) def test_read(self): class Dummy(): diff --git a/tests/test_filtering.py b/tests/test_filtering.py new file mode 100644 index 0000000..327dbd3 --- /dev/null +++ b/tests/test_filtering.py @@ -0,0 +1,36 @@ +import unittest +from gitinspector import filtering + +TEST_STRING = 'arbitrary' + + +class TestFiltering(unittest.TestCase): + + def test_InvalidRegExpError(self): + with self.assertRaises(filtering.InvalidRegExpError): + raise filtering.InvalidRegExpError(TEST_STRING) + + def test_get(self): + expected = filtering.__filters__ + actual = filtering.get() + self.assertEqual(expected, actual) + + def test_add(self): + filtering.add(TEST_STRING) + expected = [{TEST_STRING}, set()] + actual = filtering.get()['file'] + self.assertEqual(expected, actual) + + def test_get_filered(self): + filtering.add(TEST_STRING) + expected = set() + actual = filtering.get_filered() + self.assertEqual(expected, actual) + + def test_has_filtered(self): + self.assertFalse(filtering.has_filtered()) + + def test_set_filtered(self): + test_commit_sha = '53d81bcd2612dbc47e73c71ee43baae83c1ec252' + return_value = filtering.set_filtered(test_commit_sha) + self.assertFalse(return_value) diff --git a/tests/test_format.py b/tests/test_format.py new file mode 100644 index 0000000..4a230d6 --- /dev/null +++ b/tests/test_format.py @@ -0,0 +1,75 @@ +import os +import sys +import json +import unittest +from hashlib import sha256 +from gitinspector import format +from io import StringIO +from contextlib import contextmanager + +TEST_STRING = 'arbitrary' + + +class DummyRepo: + name = TEST_STRING + + +@contextmanager +def print_capture(*args, **kwds): + temp_out = StringIO() # Create the in-memory "file" + try: + sys.stdout = temp_out # Replace default stdout (terminal) with our stream + yield temp_out + finally: + sys.stdout = sys.__stdout__ # Restore default stdout + + +class TestFormat(unittest.TestCase): + + def test_InvalidFormatError(self): + with self.assertRaises(format.InvalidFormatError): + raise format.InvalidFormatError(TEST_STRING) + + def test_select(self): + test_format = 'json' + return_value = format.select(test_format) + self.assertTrue(return_value) + + def test_get_selected(self): + test_format = 'json' + format.select(test_format) + expected = test_format + actual = format.get_selected() + self.assertEqual(expected, actual) + + def test_is_interactive_format(self): + test_format = 'json' + format.select(test_format) + return_value = format.is_interactive_format() + self.assertFalse(return_value) + + def test__output_html_template__(self): + test_template_path = os.path.join('html', 'html.header') + return_value = format.__output_html_template__(test_template_path) + return_value_hash = sha256(return_value.encode('utf-8')).hexdigest() + expected_hash = '6b113dca32e7947e21ad9ad910c4995e62672ca4c0bc34577e33d2e328da7b3a' + self.assertEqual(expected_hash, return_value_hash) + + def test__get_zip_file_content__(self): + return_value = format.__get_zip_file_content__('LICENSE.txt') + return_value_hash = sha256(return_value.encode('utf-8')).hexdigest() + expected_hash = '52cb566b16d84314b92b91361ed072eaaf166e8d3dfa3d0fd3577613925f205c' + self.assertEqual(expected_hash, return_value_hash) + + def test_json_output_header_and_footer(self): + test_format = 'json' + format.select(test_format) + repos = [DummyRepo()] + with print_capture() as output: + format.output_header(repos) + format.output_footer() + output_text = output.getvalue()[:-2].replace('\n', '').replace('\t', '')[:-2] + "}}" + output_json = json.loads(output_text) + self.assertIn('report_date', output_json['gitinspector']) + self.assertEqual(output_json['gitinspector']['repository'], 'arbitrary') + self.assertEqual(output_json['gitinspector']['version'], '0.5.0dev')