test_info_dict.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import pytest
  2. import click.types
  3. # Common (obj, expect) pairs used to construct multiple tests.
  4. STRING_PARAM_TYPE = (click.STRING, {"param_type": "String", "name": "text"})
  5. INT_PARAM_TYPE = (click.INT, {"param_type": "Int", "name": "integer"})
  6. BOOL_PARAM_TYPE = (click.BOOL, {"param_type": "Bool", "name": "boolean"})
  7. HELP_OPTION = (
  8. None,
  9. {
  10. "name": "help",
  11. "param_type_name": "option",
  12. "opts": ["--help"],
  13. "secondary_opts": [],
  14. "type": BOOL_PARAM_TYPE[1],
  15. "required": False,
  16. "nargs": 1,
  17. "multiple": False,
  18. "default": False,
  19. "envvar": None,
  20. "help": "Show this message and exit.",
  21. "prompt": None,
  22. "is_flag": True,
  23. "flag_value": True,
  24. "count": False,
  25. "hidden": False,
  26. },
  27. )
  28. NAME_ARGUMENT = (
  29. click.Argument(["name"]),
  30. {
  31. "name": "name",
  32. "param_type_name": "argument",
  33. "opts": ["name"],
  34. "secondary_opts": [],
  35. "type": STRING_PARAM_TYPE[1],
  36. "required": True,
  37. "nargs": 1,
  38. "multiple": False,
  39. "default": None,
  40. "envvar": None,
  41. },
  42. )
  43. NUMBER_OPTION = (
  44. click.Option(["-c", "--count", "number"], default=1),
  45. {
  46. "name": "number",
  47. "param_type_name": "option",
  48. "opts": ["-c", "--count"],
  49. "secondary_opts": [],
  50. "type": INT_PARAM_TYPE[1],
  51. "required": False,
  52. "nargs": 1,
  53. "multiple": False,
  54. "default": 1,
  55. "envvar": None,
  56. "help": None,
  57. "prompt": None,
  58. "is_flag": False,
  59. "flag_value": False,
  60. "count": False,
  61. "hidden": False,
  62. },
  63. )
  64. HELLO_COMMAND = (
  65. click.Command("hello", params=[NUMBER_OPTION[0]]),
  66. {
  67. "name": "hello",
  68. "params": [NUMBER_OPTION[1], HELP_OPTION[1]],
  69. "help": None,
  70. "epilog": None,
  71. "short_help": None,
  72. "hidden": False,
  73. "deprecated": False,
  74. },
  75. )
  76. HELLO_GROUP = (
  77. click.Group("cli", [HELLO_COMMAND[0]]),
  78. {
  79. "name": "cli",
  80. "params": [HELP_OPTION[1]],
  81. "help": None,
  82. "epilog": None,
  83. "short_help": None,
  84. "hidden": False,
  85. "deprecated": False,
  86. "commands": {"hello": HELLO_COMMAND[1]},
  87. "chain": False,
  88. },
  89. )
  90. @pytest.mark.parametrize(
  91. ("obj", "expect"),
  92. [
  93. pytest.param(
  94. click.types.FuncParamType(range),
  95. {"param_type": "Func", "name": "range", "func": range},
  96. id="Func ParamType",
  97. ),
  98. pytest.param(
  99. click.UNPROCESSED,
  100. {"param_type": "Unprocessed", "name": "text"},
  101. id="UNPROCESSED ParamType",
  102. ),
  103. pytest.param(*STRING_PARAM_TYPE, id="STRING ParamType"),
  104. pytest.param(
  105. click.Choice(["a", "b"]),
  106. {
  107. "param_type": "Choice",
  108. "name": "choice",
  109. "choices": ["a", "b"],
  110. "case_sensitive": True,
  111. },
  112. id="Choice ParamType",
  113. ),
  114. pytest.param(
  115. click.DateTime(["%Y-%m-%d"]),
  116. {"param_type": "DateTime", "name": "datetime", "formats": ["%Y-%m-%d"]},
  117. id="DateTime ParamType",
  118. ),
  119. pytest.param(*INT_PARAM_TYPE, id="INT ParamType"),
  120. pytest.param(
  121. click.IntRange(0, 10, clamp=True),
  122. {
  123. "param_type": "IntRange",
  124. "name": "integer range",
  125. "min": 0,
  126. "max": 10,
  127. "min_open": False,
  128. "max_open": False,
  129. "clamp": True,
  130. },
  131. id="IntRange ParamType",
  132. ),
  133. pytest.param(
  134. click.FLOAT, {"param_type": "Float", "name": "float"}, id="FLOAT ParamType"
  135. ),
  136. pytest.param(
  137. click.FloatRange(-0.5, 0.5),
  138. {
  139. "param_type": "FloatRange",
  140. "name": "float range",
  141. "min": -0.5,
  142. "max": 0.5,
  143. "min_open": False,
  144. "max_open": False,
  145. "clamp": False,
  146. },
  147. id="FloatRange ParamType",
  148. ),
  149. pytest.param(*BOOL_PARAM_TYPE, id="Bool ParamType"),
  150. pytest.param(
  151. click.UUID, {"param_type": "UUID", "name": "uuid"}, id="UUID ParamType"
  152. ),
  153. pytest.param(
  154. click.File(),
  155. {"param_type": "File", "name": "filename", "mode": "r", "encoding": None},
  156. id="File ParamType",
  157. ),
  158. pytest.param(
  159. click.Path(),
  160. {
  161. "param_type": "Path",
  162. "name": "path",
  163. "exists": False,
  164. "file_okay": True,
  165. "dir_okay": True,
  166. "writable": False,
  167. "readable": True,
  168. "allow_dash": False,
  169. },
  170. id="Path ParamType",
  171. ),
  172. pytest.param(
  173. click.Tuple((click.STRING, click.INT)),
  174. {
  175. "param_type": "Tuple",
  176. "name": "<text integer>",
  177. "types": [STRING_PARAM_TYPE[1], INT_PARAM_TYPE[1]],
  178. },
  179. id="Tuple ParamType",
  180. ),
  181. pytest.param(*NUMBER_OPTION, id="Option"),
  182. pytest.param(
  183. click.Option(["--cache/--no-cache", "-c/-u"]),
  184. {
  185. "name": "cache",
  186. "param_type_name": "option",
  187. "opts": ["--cache", "-c"],
  188. "secondary_opts": ["--no-cache", "-u"],
  189. "type": BOOL_PARAM_TYPE[1],
  190. "required": False,
  191. "nargs": 1,
  192. "multiple": False,
  193. "default": False,
  194. "envvar": None,
  195. "help": None,
  196. "prompt": None,
  197. "is_flag": True,
  198. "flag_value": True,
  199. "count": False,
  200. "hidden": False,
  201. },
  202. id="Flag Option",
  203. ),
  204. pytest.param(*NAME_ARGUMENT, id="Argument"),
  205. ],
  206. )
  207. def test_parameter(obj, expect):
  208. out = obj.to_info_dict()
  209. assert out == expect
  210. @pytest.mark.parametrize(
  211. ("obj", "expect"),
  212. [
  213. pytest.param(*HELLO_COMMAND, id="Command"),
  214. pytest.param(*HELLO_GROUP, id="Group"),
  215. pytest.param(
  216. click.Group(
  217. "base",
  218. [click.Command("test", params=[NAME_ARGUMENT[0]]), HELLO_GROUP[0]],
  219. ),
  220. {
  221. "name": "base",
  222. "params": [HELP_OPTION[1]],
  223. "help": None,
  224. "epilog": None,
  225. "short_help": None,
  226. "hidden": False,
  227. "deprecated": False,
  228. "commands": {
  229. "cli": HELLO_GROUP[1],
  230. "test": {
  231. "name": "test",
  232. "params": [NAME_ARGUMENT[1], HELP_OPTION[1]],
  233. "help": None,
  234. "epilog": None,
  235. "short_help": None,
  236. "hidden": False,
  237. "deprecated": False,
  238. },
  239. },
  240. "chain": False,
  241. },
  242. id="Nested Group",
  243. ),
  244. ],
  245. )
  246. def test_command(obj, expect):
  247. ctx = click.Context(obj)
  248. out = obj.to_info_dict(ctx)
  249. assert out == expect
  250. def test_context():
  251. ctx = click.Context(HELLO_COMMAND[0])
  252. out = ctx.to_info_dict()
  253. assert out == {
  254. "command": HELLO_COMMAND[1],
  255. "info_name": None,
  256. "allow_extra_args": False,
  257. "allow_interspersed_args": True,
  258. "ignore_unknown_options": False,
  259. "auto_envvar_prefix": None,
  260. }
  261. def test_paramtype_no_name():
  262. class TestType(click.ParamType):
  263. pass
  264. assert TestType().to_info_dict()["name"] == "TestType"