fork/mons
git clone https://git.julia.blue/fork/mons
164f254
fixes
1"""Sanity checks for click commands.""" 2import typing as t 3 4if t .TYPE_CHECKING : 5import click 6import mons .clickExt as clickExt 7from inspect import signature ,unwrap 8 9 10def unwrap_count (f ): 11depth = 0 12 13def incr (_ ): 14nonlocal depth 15depth = depth + 1 16return False 17 18return unwrap (f ,stop = incr ),depth 19 20 21def test_command_arguments (command :"click.Command" ): 22assert command .callback 23 24callback ,depth = unwrap_count (command .callback ) 25 26callback_args = signature (callback ).parameters .keys () 27callback_args_unique = set (callback_args ) 28command_params = [ 29param .name 30for param in command .params 31if param .expose_value and not isinstance (param ,clickExt .PlaceHolder ) 32if param .name 33 ] 34 35for param in command_params : 36assert ( 37param in callback_args 38 ),f"Callback for ' { command . name } ' has missing argument: ' { param } '" 39callback_args_unique .discard (param ) 40 41assert depth == len ( 42callback_args_unique 43 ),f"Callback for ' { command . name } ' has too many positional arguments: ' { callback_args_unique } " 44 45 46def test_command_global_flags (command :"click.Command" ): 47command_params = [ 48param 49for param in command .params 50if param .expose_value 51and not isinstance (param ,clickExt .PlaceHolder ) 52and any ( 53opt in clickExt .loglevel_flags 54for opt in (* param .opts ,* param .secondary_opts ) 55 ) 56 ] 57 58assert ( 59not command_params 60 ),"Log level flags are swallowed by logger. Use `logger.isEnabledFor` to check logging level." 61 62command_params = [ 63param 64for param in command .params 65if param .expose_value 66and not isinstance (param ,clickExt .PlaceHolder ) 67and any ( 68opt in ["--pause" ,"--prompt-install" ] 69for opt in (* param .opts ,* param .secondary_opts ) 70 ) 71 ] 72 73assert ( 74not command_params 75 ),"The '--pause' and '--prompt-install' flags are swallowed by the command group."