Tests: fix bl_keymap_completeness.py test for keymap_hierarchy.py

Restore the keymap completeness test which has been effectively
disabled since [0].

Address various issues that broke `bl_keymap_completeness.py`.

- Ensure the keymap is loaded in background mode.
- Identify key-maps by name, space & region types.
- Add special handling for tool fallback keymaps.

Ref !162440

[0]: d7d9320f09
This commit is contained in:
Campbell Barton 2026-08-11 06:34:02 +00:00
parent 3eb7bf0f32
commit 849c01e6b8

View file

@ -2,73 +2,125 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
# simple script to test 'bl_keymap_utils.keymap_hierarchy' contains correct values.
# ./blender.bin -b -X -P tests/python/bl_keymap_completeness.py
#
"""
Simple script to test ``bl_keymap_utils.keymap_hierarchy`` contains correct values.
"""
__all__ = (
"main",
)
# Needed for 'bl_keymap_utils.keymap_hierarchy' which inspects tools.
import sys
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, "scripts", "startup"))
del sys, os
from bl_keymap_utils import keymap_hierarchy
# Suffix for the key-map used while the tool acts as the fallback tool, see `_fallback_id`.
KEYMAP_SUFFIX_FALLBACK = " (fallback)"
# Key-maps to skip when checking blender's key-maps can all be reached from the hierarchy.
# TODO: investigate each of these, in most cases the key-map data should probably be removed.
ALLOW_MISSING_FROM_HIERARCHY = {
# The "Spin Duplicates" tool was merged into "Spin", see: !117880.
"3D View Tool: Edit Mesh, Spin Duplicates",
# The mask editor shares the UV editors cursor tool, which uses "Image Editor Tool: Uv, Cursor".
"Image Editor Tool: Mask, Cursor",
# This tool is gizmo only, it intentionally has no default action.
"Image Editor Tool: Mask, Transform",
# The mask primitive tools are disabled, see the TODO in `space_toolsystem_toolbar.py`.
"Image Editor Tool: Mask, Box",
"Image Editor Tool: Mask, Circle",
}
def keymaps_missing(maps_py, maps_bl):
return [
km_id for km_id in sorted(maps_py - maps_bl)
if not km_id[0].endswith(KEYMAP_SUFFIX_FALLBACK)
]
def keymaps_missing_fallback(maps_py, maps_bl):
# `keymap_ui_hierarchy` yields a fallback for every tool keymap although only the select
# tools have them. So the fallback may not exist, the keymap it falls back from must.
return [
km_id for km_id in sorted(maps_py - maps_bl)
if km_id[0].endswith(KEYMAP_SUFFIX_FALLBACK)
if (km_id[0].removesuffix(KEYMAP_SUFFIX_FALLBACK), *km_id[1:]) not in maps_bl
]
def check_maps():
maps = {}
import bpy
# A key-map is identified by its name, space-type & region-type, all three must match
# for the key-map to be found, see `rna_keymap_ui.draw_entry`.
maps_py = set()
def fill_maps(seq):
for km_name, km_space_type, km_region_type, km_sub in seq:
maps[km_name] = (km_space_type, km_region_type)
maps_py.add((km_name, km_space_type, km_region_type))
fill_maps(km_sub)
fill_maps(keymap_hierarchy.generate())
import bpy
keyconf = bpy.context.window_manager.keyconfigs.active
maps_bl = set(keyconf.keymaps.keys())
maps_py = set(maps.keys())
maps_bl = {(km.name, km.space_type, km.region_type) for km in keyconf.keymaps}
err = False
# Check keyconfig contains only maps that exist in blender
test = maps_py - maps_bl
# Check the hierarchy only references keymaps that exist in blender.
test = keymaps_missing(maps_py, maps_bl)
test_fallback = keymaps_missing_fallback(maps_py, maps_bl)
if test:
print("Keymaps that are in 'bl_keymap_utils.keymap_hierarchy' but not blender")
for km_id in test:
if callable(km_id):
# Keymap functions of tools are not in blender anyway...
continue
print("\t%s" % km_id)
# TODO #65963, broken keymap hierarchy tests disabled until fixed.
# err = True
print(" ('{:s}', '{:s}', '{:s}', []),".format(*km_id))
err = True
if test_fallback:
print("Fallback keymaps in 'bl_keymap_utils.keymap_hierarchy' with no keymap to fall back from")
for km_id in test_fallback:
print(" ('{:s}', '{:s}', '{:s}', []),".format(*km_id))
err = True
# Check blender's keymaps can all be reached from the hierarchy.
test = sorted(maps_bl - maps_py)
if ALLOW_MISSING_FROM_HIERARCHY:
test = [km_id for km_id in test if km_id[0] not in ALLOW_MISSING_FROM_HIERARCHY]
test = maps_bl - maps_py
if test:
print("Keymaps that are in blender but not in 'bl_keymap_utils.keymap_hierarchy'")
for km_id in test:
km = keyconf.keymaps[km_id]
print(" ('%s', '%s', '%s', [])," % (km_id, km.space_type, km.region_type))
# TODO #65963, broken keymap hierarchy tests disabled until fixed.
# err = True
# Check space/region's are OK
print("Comparing keymap space/region types...")
for km_id, km in keyconf.keymaps.items():
km_py = maps.get(km_id)
if km_py is not None:
km_space_type, km_region_type = km_py
if km_space_type != km.space_type or km_region_type != km.region_type:
print(" Error:")
print(" expected -- ('%s', '%s', '%s', [])," % (km_id, km.space_type, km.region_type))
print(" got -- ('%s', '%s', '%s', [])," % (km_id, km_space_type, km_region_type))
print("done!")
print(" ('{:s}', '{:s}', '{:s}', []),".format(*km_id))
# Listing the name with the wrong types is a common cause, report it as it's easy to miss.
km_args_other = sorted(km_id_other[1:] for km_id_other in maps_py if km_id_other[0] == km_id[0])
if km_args_other:
km_args_text = ", ".join(["('{:s}', '{:s}')".format(*args) for args in km_args_other])
print(" listed with: {:s}".format(km_args_text))
err = True
return err
def main():
import bpy
# Not loaded in background mode.
if bpy.app.background:
bpy.utils.keyconfig_init()
err = check_maps()
import bpy
if err and bpy.app.background:
# alert CTest we failed
import sys