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 main 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/main'], 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 ('main', 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() + '/staging/20.1', True), 50 (get_upstream() + '/main', True), 51 ('20.1', False), 52 ('main', False), 53 ('e58a10af640ba58b6001f5c5ad750b782547da76', False), 54 ('d043d24654c851f0be57dbbf48274b5373dea42b', False), 55 ('dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False), 56 ('0000000000000000000000000000000000000000', False), 57 ('not-even-a-valid-commit-format', False), 58 ]) 59def test_validate_branch(commit: str, expected: bool) -> None: 60 if expected: 61 assert validate_branch(commit) 62 else: 63 try: 64 assert validate_branch(commit) 65 except argparse.ArgumentTypeError: 66 return 67 assert False 68 69 70@pytest.mark.parametrize( 71 'commit, expected', 72 [ 73 ('main', True), 74 ('20.1-branchpoint', True), 75 ('20.1', False), 76 (get_upstream() + '/20.1', True), 77 (get_upstream() + '/staging/20.1', True), 78 ('e58a10af640ba58b6001f5c5ad750b782547da76', True), 79 ('d043d24654c851f0be57dbbf48274b5373dea42b', True), 80 ('dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), 81 ('0000000000000000000000000000000000000000', False), 82 ('not-even-a-valid-commit-format', False), 83 ]) 84def test_is_commit_valid(commit: str, expected: bool) -> None: 85 assert is_commit_valid(commit) == expected 86 87 88@pytest.mark.parametrize( 89 'branch, commit, expected', 90 [ 91 (get_upstream() + '/20.1', '20.1-branchpoint', True), 92 (get_upstream() + '/20.1', '20.0', False), 93 (get_upstream() + '/20.1', 'main', False), 94 (get_upstream() + '/20.1', 'e58a10af640ba58b6001f5c5ad750b782547da76', True), 95 (get_upstream() + '/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True), 96 (get_upstream() + '/staging/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', True), 97 (get_upstream() + '/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', False), 98 (get_upstream() + '/main', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', True), 99 (get_upstream() + '/20.0', 'd043d24654c851f0be57dbbf48274b5373dea42b', False), 100 ]) 101def test_branch_has_commit(branch: str, commit: str, expected: bool) -> None: 102 assert branch_has_commit(branch, commit) == expected 103 104 105@pytest.mark.parametrize( 106 'branch, commit, expected', 107 [ 108 (get_upstream() + '/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'), 109 (get_upstream() + '/staging/20.1', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', 'd043d24654c851f0be57dbbf48274b5373dea42b'), 110 (get_upstream() + '/20.1', '20.1-branchpoint', ''), 111 (get_upstream() + '/20.1', '20.0', ''), 112 (get_upstream() + '/20.1', '20.2', 'abac4859618e02aea00f705b841a7c5c5007ad1a'), 113 (get_upstream() + '/20.1', 'main', ''), 114 (get_upstream() + '/20.1', 'd043d24654c851f0be57dbbf48274b5373dea42b', ''), 115 (get_upstream() + '/20.0', 'dd2bd68fa69124c86cd008b256d06f44fab8e6cd', '8cd4f57381cefe69019a3282d457d5bda3644030'), 116 ]) 117def test_branch_has_backport_of_commit(branch: str, commit: str, expected: bool) -> None: 118 assert branch_has_backport_of_commit(branch, commit) == expected 119