blob: 6b18d712c98311125c0f127dc2b5beec8f927f1f [file] [log] [blame]
Mike Frysinger9589a252023-06-23 02:56:16 -04001# 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
7import pytest
8
9from chromite.scripts import gerrit
10
11
12def 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 Frysinger32c1d9f2023-06-22 09:07:19 -040037
38
39DATA_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"}),
Mike Frysingerd74a8bc2023-06-22 09:42:55 -040047 (["-a", "a"], {"a"}, set()),
Mike Frysinger32c1d9f2023-06-22 09:07:19 -040048)
49
50
51@pytest.mark.parametrize(
52 "items, exp_add, exp_remove", DATA_PROCESS_ADD_REMOVE_LISTS
53)
54def test_process_add_remove_lists(items, exp_add, exp_remove):
55 """Test process_add_remove_lists behavior."""
56 add, remove = gerrit.process_add_remove_lists(items)
57 assert add == exp_add and remove == exp_remove
58
59
60DATA_PROCESS_ADD_REMOVE_LISTS_VALIDATE = (
61 # No inputs means no outputs.
62 ([], set(), set()),
63 (["u@d"], {"u@d"}, set()),
64 (["~u@d"], set(), {"u@d"}),
65)
66
67
68@pytest.mark.parametrize(
69 "items, exp_add, exp_remove", DATA_PROCESS_ADD_REMOVE_LISTS_VALIDATE
70)
71def test_process_add_remove_lists_validate(items, exp_add, exp_remove):
72 """Test process_add_remove_lists behavior with a validator."""
73 add, remove = gerrit.process_add_remove_lists(items, validate=r"^.@.$")
74 assert add == exp_add and remove == exp_remove
75
76
77def test_process_add_remove_lists_invalid():
78 """Test validation errors."""
79 with pytest.raises(SystemExit) as excinfo:
80 gerrit.process_add_remove_lists(["a"], validate=r"^.@.$")
81 assert excinfo.value.code != 0
82
83 # Never accept the empty string.
84 with pytest.raises(SystemExit) as excinfo:
85 gerrit.process_add_remove_lists([""])
86 assert excinfo.value.code != 0