-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
If you have two shortcuts that point to the same key combo, you'll get "QAction::event: Ambiguous shortcut overload: <sequence>" in the logs and neither action will be taken. Add a test that scans the whole codebase to verify the argument to setShortcut() is globally unique. To make the test simple to write, standardize on a single way of writing shortcuts, by adding Qt constants. A semgrep rule tries to guide people into writing shortcuts in the same style, so the test can match them.
- Loading branch information
Showing
4 changed files
with
34 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import re | ||
from collections import defaultdict | ||
from pathlib import Path | ||
|
||
|
||
def test_shortcuts_are_unique(mocker): | ||
""" | ||
Look through the codebase for setShortcut() calls, | ||
verify the argument passed is unique | ||
The `enforce-setshortcut-style` semgrep rule enforces a consistent style | ||
""" | ||
root = Path(__file__).parent.parent.parent / "securedrop_client" | ||
shortcuts = defaultdict(int) | ||
for path in root.glob("**/*.py"): | ||
for match_ in re.findall(r"setShortcut\((.*)\)", path.read_text()): | ||
shortcuts[match_] += 1 | ||
|
||
# Verify that the parsing actually found stuff | ||
assert "Qt.CTRL + Qt.Key_Q" in shortcuts | ||
# Look for duplicates | ||
for shortcut, instances in shortcuts.items(): | ||
if instances > 1: | ||
raise AssertionError(f"Shortcut {shortcut} is used {instances} times") |