|
| 1 | +import tempfile |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch |
| 4 | +from subprocess import CalledProcessError, PIPE, STDOUT |
| 5 | +import contrib.testRender as test_render |
| 6 | +from io import StringIO |
| 7 | +from shlex import split |
| 8 | + |
| 9 | + |
| 10 | +class TestTestRender(unittest.TestCase): |
| 11 | + @patch("contrib.testRender.run") |
| 12 | + def test_check_call_raises_CalledProcessError_from_subprocess_run(self, m_run): |
| 13 | + m_run.side_effect = CalledProcessError(1, "python program.js") |
| 14 | + with self.assertRaises(CalledProcessError): |
| 15 | + test_render.check_call(["python", "program.js"]) |
| 16 | + |
| 17 | + @patch("contrib.testRender.run") |
| 18 | + def test_check_call_captures_stdout_if_not_verbose(self, m_run): |
| 19 | + test_render.check_call(["python", "program.py"]) |
| 20 | + args, kwargs = m_run.call_args |
| 21 | + self.assertEqual(kwargs['stdout'], PIPE) |
| 22 | + self.assertEqual(kwargs['stderr'], STDOUT) |
| 23 | + |
| 24 | + @patch("contrib.testRender.run") |
| 25 | + def test_check_call_does_not_capture_stdout_if_verbose(self, m_run): |
| 26 | + test_render.check_call(["python", "program.py"], verbose=True) |
| 27 | + args, kwargs = m_run.call_args |
| 28 | + self.assertEqual(kwargs['stdout'], None) |
| 29 | + self.assertEqual(kwargs['stderr'], None) |
| 30 | + |
| 31 | + @patch('sys.stdout', new_callable=StringIO) |
| 32 | + @patch("contrib.testRender.run") |
| 33 | + def test_check_call_prints_exception_output_if_verbose(self, m_run, m_out): |
| 34 | + m_run.side_effect = CalledProcessError( |
| 35 | + 1, "python program.js", output="SyntaxError: invalid syntax") |
| 36 | + with self.assertRaises(CalledProcessError): |
| 37 | + test_render.check_call(["python", "program.js"], verbose=True) |
| 38 | + self.assertEqual(m_out.getvalue().strip(), "SyntaxError: invalid syntax") |
| 39 | + |
| 40 | + @patch("contrib.testRender.run") |
| 41 | + def test_check_output_captures_stdout(self, m_run): |
| 42 | + test_render.check_call(["python", "program.py"]) |
| 43 | + args, kwargs = m_run.call_args |
| 44 | + self.assertEqual(kwargs['stdout'], PIPE) |
| 45 | + |
| 46 | + @patch('contrib.testRender.check_output') |
| 47 | + def test_get_commits(self, m_check_output): |
| 48 | + gitrange = '2eca1a5fb5fa7eeb5494abb350cd535f67acfb8b..08a86a52abfabd59ac68b37dc7e5270bd7fb328a' |
| 49 | + m_check_output.return_value = ( |
| 50 | + "commit 2eca1a5fb5fa7eeb5494abb350cd535f67acfb8b\nAuthor: Andrew " |
| 51 | + "<andrew@fry.(none)>\nDate: Sun Aug 22 10:16:10 2010 -0400\n\n " |
| 52 | + " initial comit\n\n:000000 100644 0000000 c398ada A\tchunk.py\n:000000 " |
| 53 | + "100644 0000000 d5ee6ed A\tnbt.py\n:000000 100644 0000000 8fc65c9 A\ttextures.py\n:" |
| 54 | + "000000 100644 0000000 6934326 A\tworld.py\n\ncommit 08a86a52abfabd59ac68b37dc7e5270bd7fb328a" |
| 55 | + "\nAuthor: Andrew <andrew@fry.(none)>\nDate: Tue Aug 24 21:11:57 2010 -0400\n\n " |
| 56 | + "uses multiprocessing to speed up rendering. Caches chunks\n\n:1" |
| 57 | + ) |
| 58 | + |
| 59 | + result = list(test_render.get_commits(gitrange)) |
| 60 | + self.assertListEqual(result, ['2eca1a5fb5fa7eeb5494abb350cd535f67acfb8b', |
| 61 | + '08a86a52abfabd59ac68b37dc7e5270bd7fb328a']) |
| 62 | + |
| 63 | + @patch('contrib.testRender.check_output', return_value="my-feature-branch") |
| 64 | + def test_get_current_branch(self, m_check_output): |
| 65 | + self.assertEqual(test_render.get_current_branch(), "my-feature-branch") |
| 66 | + |
| 67 | + @patch('contrib.testRender.check_output', return_value="HEAD") |
| 68 | + def test_get_current_branch_returns_none_for_detached_head(self, m_check_output): |
| 69 | + self.assertIsNone(test_render.get_current_branch()) |
| 70 | + |
| 71 | + @patch('contrib.testRender.check_output', return_value="3f1f3d748e1c79843279ba18ab65a34368b95b67") |
| 72 | + def test_get_current_commit(self, m_check_output): |
| 73 | + self.assertEqual( |
| 74 | + test_render.get_current_branch(), |
| 75 | + "3f1f3d748e1c79843279ba18ab65a34368b95b67" |
| 76 | + ) |
| 77 | + |
| 78 | + @patch('contrib.testRender.get_current_branch', return_value="my-feature-branch") |
| 79 | + def test_get_current_ref_returns_branch_name_if_possible(self, m_branch): |
| 80 | + self.assertEqual(test_render.get_current_ref(), "my-feature-branch") |
| 81 | + |
| 82 | + @patch('contrib.testRender.get_current_commit', return_value="3f1f3d748e1c79843279ba18ab65a34368b95b67") |
| 83 | + @patch('contrib.testRender.get_current_branch', return_value=None) |
| 84 | + def test_get_current_ref_returns_current_commit_if_no_branch(self, m_branch, m_commit): |
| 85 | + self.assertEqual( |
| 86 | + test_render.get_current_ref(), |
| 87 | + "3f1f3d748e1c79843279ba18ab65a34368b95b67" |
| 88 | + ) |
| 89 | + |
| 90 | + @patch('contrib.testRender.check_output') |
| 91 | + def test_get_commits(self, m_check_output): |
| 92 | + m_check_output.return_value = "\n".join( |
| 93 | + [ |
| 94 | + "41ceaeab58473416bb79680ab21211764e6f1908", |
| 95 | + "a4d0daa91c25a51ca95182301e503c020900dafe", |
| 96 | + "05906c81f5778a543dfab14e77231db0a99bae24", |
| 97 | + ] |
| 98 | + ) |
| 99 | + gitrange = "41ceaeab58473416bb79680ab21211764e6f1908..05906c81f5778a543dfab14e77231db0a99bae24" |
| 100 | + result = list(test_render.get_commits(gitrange)) |
| 101 | + self.assertListEqual( |
| 102 | + result, |
| 103 | + [ |
| 104 | + "41ceaeab58473416bb79680ab21211764e6f1908", |
| 105 | + "a4d0daa91c25a51ca95182301e503c020900dafe", |
| 106 | + "05906c81f5778a543dfab14e77231db0a99bae24" |
| 107 | + ] |
| 108 | + ) |
0 commit comments