def test_name_status_failure(self): def mock_run_cmd(unused_tokens): return ('A B C D\n') def mock_print(unused_str): pass run_cmd_swap = self.swap_with_checks( common, 'run_cmd', mock_run_cmd, expected_args=[ (['git', 'diff', '--name-status', 'upstream/develop'], ), ]) print_swap = self.swap_with_checks( python_utils, 'PRINT', mock_print, expected_args=[ ('Failed to parse diff --name-status line "A B C D"', ), ]) with run_cmd_swap, print_swap: diff_files, file_diffs = check_if_pr_is_low_risk.load_diff( 'develop') self.assertListEqual(diff_files, []) self.assertDictEqual(file_diffs, {})
def test_parse_diff_failure_no_diff(self): def mock_run_cmd(tokens): if '--name-status' in tokens: return ( 'M modified\n' ) if tokens[-1] == 'modified': return ( 'diff --git a/modififed b/modified\n' 'index 11af605ef2b7..89d00105ca66 100644\n' '--- a/modified\n' '+++ b/modified\n' ) def mock_print(unused_str): pass run_cmd_swap = self.swap_with_checks( common, 'run_cmd', mock_run_cmd, expected_args=[ ([ 'git', 'diff', '--name-status', 'upstream/develop'],), ([ 'git', 'diff', '-U0', 'upstream/develop', '--', 'modified'],), ]) print_swap = self.swap_with_checks( python_utils, 'PRINT', mock_print, expected_args=[ ('Failed to find end of header in "modified" diff',), ]) files_that_need_diffs_swap = self.swap( check_if_pr_is_low_risk, 'FILES_THAT_NEED_DIFFS', ('modified',), ) with run_cmd_swap, print_swap, files_that_need_diffs_swap: diff_files, file_diffs = check_if_pr_is_low_risk.load_diff( 'develop') self.assertListEqual(diff_files, []) self.assertDictEqual(file_diffs, {})
def test_parse_multifile_diff(self): def mock_run_cmd(tokens): if '--name-status' in tokens: return ('M modified\n' 'R099 old new\n' 'A added\n' 'M diff-unnecessary\n') if tokens[-1] == 'modified': return ('diff --git a/modififed b/modified\n' 'index 11af605ef2b7..89d00105ca66 100644\n' '--- a/modified\n' '+++ b/modified\n' '@@ -32,6 +32,7 @@ def hello():\n' '- print(s)\n' '+ python_utils.PRINT(s)\n') if tokens[-1] == 'old': return ('diff --git a/old b/old\n' 'index 11af605ef2b7..89d00105ca66 100644\n' '--- a/old\n' '+++ /dev/null\n' '@@ -32,6 +32,7 @@ def hello():\n' '- print(s)\n' '- python_utils.PRINT(s)\n') if tokens[-1] == 'new': return ('diff --git a/new b/new\n' 'index 11af605ef2b7..89d00105ca66 100644\n' '--- /dev/null\n' '+++ b/new\n' '@@ -32,6 +32,7 @@ def hello():\n' '+ print(s)\n' '+ python_utils.PRINT(s)\n') if tokens[-1] == 'added': return ('diff --git a/added b/added\n' 'index 11af605ef2b7..89d00105ca66 100644\n' '--- /dev/null\n' '+++ b/added\n' '@@ -32,6 +32,7 @@ def hello():\n' '+ print(s)\n' '+ python_utils.PRINT(s)\n') raise AssertionError('Unknown args to mock_run_cmd: %s' % tokens) run_cmd_swap = self.swap_with_checks( common, 'run_cmd', mock_run_cmd, expected_args=[ (['git', 'diff', '--name-status', 'upstream/develop'], ), (['git', 'diff', '-U0', 'upstream/develop', '--', 'modified'], ), (['git', 'diff', '-U0', 'upstream/develop', '--', 'old'], ), (['git', 'diff', '-U0', 'upstream/develop', '--', 'new'], ), (['git', 'diff', '-U0', 'upstream/develop', '--', 'added'], ), ]) files_that_need_diffs_swap = self.swap( check_if_pr_is_low_risk, 'FILES_THAT_NEED_DIFFS', ('modified', 'old', 'new', 'added'), ) with run_cmd_swap, files_that_need_diffs_swap: diff_files, file_diffs = check_if_pr_is_low_risk.load_diff( 'develop') expected_diff_files = [ ('modified', 'modified'), ('old', 'new'), ('added', 'added'), ('diff-unnecessary', 'diff-unnecessary'), ] expected_file_diffs = { 'modified': [ '- print(s)', '+ python_utils.PRINT(s)', ], 'old': [ '- print(s)', '- python_utils.PRINT(s)', ], 'new': [ '+ print(s)', '+ python_utils.PRINT(s)', ], 'added': [ '+ print(s)', '+ python_utils.PRINT(s)', ], } self.assertListEqual(diff_files, expected_diff_files) self.assertDictEqual(file_diffs, expected_file_diffs)