1import argparse 2import pytest # type: ignore 3import subprocess 4 5from .commit_in_branch import ( 6 is_commit_valid, 7 branch_has_commit, 8 branch_has_backport_of_commit, 9 canonicalize_commit, 10 validate_branch, 11) 12 13 14def get_upstream() -> str: 15 # Let's assume master is bound to the upstream remote and not a fork 16 out = subprocess.check_output(['git', 'for-each-ref', 17 '--format=%(upstream)', 18 'refs/heads/master'], 19 stderr=subprocess.DEVNULL) 20 return out.decode().strip().split('/')[2] 21 22 23@pytest.mark.parametrize( 24 'commit, expected', 25 [ 26 ('20.1-branchpoint', True), 27 ('master', True), 28 ('e58a10af640ba58b6001f5c5ad750b782547da76', True), 29 ('d043d24654c851f0be57dbbf48274b5373dea42b', True), 30 ('dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), 31 ('0000000000000000000000000000000000000000', False), 32 ('not-even-a-valid-commit-format', False), 33 ]) 34def test_canonicalize_commit(commit: str, expected: bool) -> None: 35 if expected: 36 assert canonicalize_commit(commit) 37 else: 38 try: 39 assert canonicalize_commit(commit) 40 except argparse.ArgumentTypeError: 41 return 42 assert False 43 44 45@pytest.mark.parametrize( 46 'commit, expected', 47 [ 48 (get_upstream() + '/20.1', True), 49 (get_upstream() + '/master', True), 50 ('20.1', False), 51 ('master', False), 52 ('e58a10af640ba58b6001f5c5ad750b782547da76', False), 53 ('d043d24654c851f0be57dbbf48274b5373dea42b', False), 54 ('dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False), 55 ('0000000000000000000000000000000000000000', False), 56 ('not-even-a-valid-commit-format', False), 57 ]) 58def test_validate_branch(commit: str, expected: bool) -> None: 59 if expected: 60 assert validate_branch(commit) 61 else: 62 try: 63 assert validate_branch(commit) 64 except argparse.ArgumentTypeError: 65 return 66 assert False 67 68 69@pytest.mark.parametrize( 70 'commit, expected', 71 [ 72 ('master', True), 73 ('20.1-branchpoint', True), 74 ('20.1', False), 75 (get_upstream() + '/20.1', True), 76 ('e58a10af640ba58b6001f5c5ad750b782547da76', True), 77 ('d043d24654c851f0be57dbbf48274b5373dea42b', True), 78 ('dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), 79 ('0000000000000000000000000000000000000000', False), 80 ('not-even-a-valid-commit-format', False), 81 ]) 82def test_is_commit_valid(commit: str, expected: bool) -> None: 83 assert is_commit_valid(commit) == expected 84 85 86@pytest.mark.parametrize( 87 'branch, commit, expected', 88 [ 89 ('20.1', '20.1-branchpoint', True), 90 ('20.1', '20.0', False), 91 ('20.1', 'master', False), 92 ('20.1', 'e58a10af640ba58b6001f5c5ad750b782547da76', True), 93 ('20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True), 94 ('20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False), 95 ('master', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), 96 ('20.0', 'd043d24654c851f0be57dbbf48274b5373dea42b', False), 97 ]) 98def test_branch_has_commit(branch: str, commit: str, expected: bool) -> None: 99 upstream = get_upstream() 100 assert branch_has_commit(upstream, branch, commit) == expected 101 102 103@pytest.mark.parametrize( 104 'branch, commit, expected', 105 [ 106 ('20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'), 107 ('20.1', '20.1-branchpoint', ''), 108 ('20.1', '20.0', ''), 109 ('20.1', '20.2', ''), 110 ('20.1', 'master', ''), 111 ('20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', ''), 112 ('20.0', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', ''), 113 ]) 114def test_branch_has_backport_of_commit(branch: str, commit: str, expected: bool) -> None: 115 upstream = get_upstream() 116 assert branch_has_backport_of_commit(upstream, branch, commit) == expected 117