Mike Frysinger | 9589a25 | 2023-06-23 02:56:16 -0400 | [diff] [blame] | 1 | # Copyright 2023 The ChromiumOS Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Test the gerrit module.""" |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from chromite.scripts import gerrit |
| 10 | |
| 11 | |
| 12 | def test_main_usage(): |
| 13 | """Basic tests for the main help.""" |
| 14 | # Missing subcommand is an error. |
| 15 | with pytest.raises(SystemExit) as excinfo: |
| 16 | gerrit.main([]) |
| 17 | assert excinfo.value.code != 0 |
| 18 | |
| 19 | with pytest.raises(SystemExit) as excinfo: |
| 20 | gerrit.main(["--help"]) |
| 21 | assert excinfo.value.code == 0 |
| 22 | |
| 23 | actions = gerrit._GetActions() # pylint: disable=protected-access |
| 24 | # Don't track exactly how many actions there are, just make sure we have a |
| 25 | # reasonable return value. |
| 26 | assert len(actions) > 20 |
| 27 | assert "help" in actions |
| 28 | assert "search" in actions |
| 29 | |
| 30 | # Check help for all subcommands. |
| 31 | for action in actions: |
| 32 | with pytest.raises(SystemExit) as excinfo: |
| 33 | gerrit.main(["help", action]) |
| 34 | assert excinfo.value.code == 0 |
| 35 | |
| 36 | gerrit.main(["help-all"]) |
Mike Frysinger | 32c1d9f | 2023-06-22 09:07:19 -0400 | [diff] [blame] | 37 | |
| 38 | |
| 39 | DATA_PROCESS_ADD_REMOVE_LISTS = ( |
| 40 | # No inputs means no outputs. |
| 41 | ([], set(), set()), |
| 42 | (["a"], {"a"}, set()), |
| 43 | (["~a"], set(), {"a"}), |
| 44 | (["a", "~a"], set(), {"a"}), |
| 45 | (["~a", "a"], {"a"}, set()), |
| 46 | (["a", "b", "c", "~d"], {"a", "b", "c"}, {"d"}), |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | @pytest.mark.parametrize( |
| 51 | "items, exp_add, exp_remove", DATA_PROCESS_ADD_REMOVE_LISTS |
| 52 | ) |
| 53 | def test_process_add_remove_lists(items, exp_add, exp_remove): |
| 54 | """Test process_add_remove_lists behavior.""" |
| 55 | add, remove = gerrit.process_add_remove_lists(items) |
| 56 | assert add == exp_add and remove == exp_remove |
| 57 | |
| 58 | |
| 59 | DATA_PROCESS_ADD_REMOVE_LISTS_VALIDATE = ( |
| 60 | # No inputs means no outputs. |
| 61 | ([], set(), set()), |
| 62 | (["u@d"], {"u@d"}, set()), |
| 63 | (["~u@d"], set(), {"u@d"}), |
| 64 | ) |
| 65 | |
| 66 | |
| 67 | @pytest.mark.parametrize( |
| 68 | "items, exp_add, exp_remove", DATA_PROCESS_ADD_REMOVE_LISTS_VALIDATE |
| 69 | ) |
| 70 | def test_process_add_remove_lists_validate(items, exp_add, exp_remove): |
| 71 | """Test process_add_remove_lists behavior with a validator.""" |
| 72 | add, remove = gerrit.process_add_remove_lists(items, validate=r"^.@.$") |
| 73 | assert add == exp_add and remove == exp_remove |
| 74 | |
| 75 | |
| 76 | def test_process_add_remove_lists_invalid(): |
| 77 | """Test validation errors.""" |
| 78 | with pytest.raises(SystemExit) as excinfo: |
| 79 | gerrit.process_add_remove_lists(["a"], validate=r"^.@.$") |
| 80 | assert excinfo.value.code != 0 |
| 81 | |
| 82 | # Never accept the empty string. |
| 83 | with pytest.raises(SystemExit) as excinfo: |
| 84 | gerrit.process_add_remove_lists([""]) |
| 85 | assert excinfo.value.code != 0 |