test_arguments.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. import collections
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.commands import arguments as module
  5. def test_get_subaction_parsers_with_no_subactions_returns_empty_result():
  6. assert module.get_subaction_parsers(flexmock(_subparsers=None)) == {}
  7. def test_get_subaction_parsers_with_subactions_returns_one_entry_per_subaction():
  8. foo_parser = flexmock()
  9. bar_parser = flexmock()
  10. baz_parser = flexmock()
  11. assert module.get_subaction_parsers(
  12. flexmock(
  13. _subparsers=flexmock(
  14. _group_actions=(
  15. flexmock(choices={'foo': foo_parser, 'bar': bar_parser}),
  16. flexmock(choices={'baz': baz_parser}),
  17. ),
  18. ),
  19. ),
  20. ) == {'foo': foo_parser, 'bar': bar_parser, 'baz': baz_parser}
  21. def test_get_subactions_for_actions_with_no_subactions_returns_empty_result():
  22. assert module.get_subactions_for_actions({'action': flexmock(_subparsers=None)}) == {}
  23. def test_get_subactions_for_actions_with_subactions_returns_one_entry_per_action():
  24. assert module.get_subactions_for_actions(
  25. {
  26. 'action': flexmock(
  27. _subparsers=flexmock(
  28. _group_actions=(
  29. flexmock(choices={'foo': flexmock(), 'bar': flexmock()}),
  30. flexmock(choices={'baz': flexmock()}),
  31. ),
  32. ),
  33. ),
  34. 'other': flexmock(
  35. _subparsers=flexmock(_group_actions=(flexmock(choices={'quux': flexmock()}),)),
  36. ),
  37. },
  38. ) == {'action': ('foo', 'bar', 'baz'), 'other': ('quux',)}
  39. def test_omit_values_colliding_with_action_names_drops_action_names_that_have_been_parsed_as_values():
  40. assert module.omit_values_colliding_with_action_names(
  41. ('check', '--only', 'extract', '--some-list', 'borg'),
  42. {'check': flexmock(only='extract', some_list=['borg'])},
  43. ) == ('check', '--only', '--some-list')
  44. def test_omit_values_colliding_twice_with_action_names_drops_action_names_that_have_been_parsed_as_values():
  45. assert module.omit_values_colliding_with_action_names(
  46. ('config', 'bootstrap', '--local-path', '--remote-path', 'borg'),
  47. {'bootstrap': flexmock(local_path='borg', remote_path='borg')},
  48. ) == ('config', 'bootstrap', '--local-path', '--remote-path')
  49. def test_parse_and_record_action_arguments_without_action_name_leaves_arguments_untouched():
  50. unparsed_arguments = ('--foo', '--bar')
  51. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  52. unparsed_arguments,
  53. )
  54. assert (
  55. module.parse_and_record_action_arguments(
  56. unparsed_arguments,
  57. flexmock(),
  58. flexmock(),
  59. 'action',
  60. )
  61. == unparsed_arguments
  62. )
  63. def test_parse_and_record_action_arguments_updates_parsed_arguments_and_returns_remaining():
  64. unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
  65. other_parsed_arguments = flexmock()
  66. parsed_arguments = {'other': other_parsed_arguments}
  67. action_parsed_arguments = flexmock()
  68. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  69. unparsed_arguments,
  70. )
  71. action_parser = flexmock()
  72. flexmock(action_parser).should_receive('parse_known_args').and_return(
  73. action_parsed_arguments,
  74. ('action', '--verbosity', '1'),
  75. )
  76. assert module.parse_and_record_action_arguments(
  77. unparsed_arguments,
  78. parsed_arguments,
  79. action_parser,
  80. 'action',
  81. ) == ('--verbosity', '1')
  82. assert parsed_arguments == {'other': other_parsed_arguments, 'action': action_parsed_arguments}
  83. def test_parse_and_record_action_arguments_with_alias_updates_canonical_parsed_arguments():
  84. unparsed_arguments = ('action', '--foo', '--bar', '--verbosity', '1')
  85. other_parsed_arguments = flexmock()
  86. parsed_arguments = {'other': other_parsed_arguments}
  87. action_parsed_arguments = flexmock()
  88. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  89. unparsed_arguments,
  90. )
  91. action_parser = flexmock()
  92. flexmock(action_parser).should_receive('parse_known_args').and_return(
  93. action_parsed_arguments,
  94. ('action', '--verbosity', '1'),
  95. )
  96. assert module.parse_and_record_action_arguments(
  97. unparsed_arguments,
  98. parsed_arguments,
  99. action_parser,
  100. 'action',
  101. canonical_name='doit',
  102. ) == ('--verbosity', '1')
  103. assert parsed_arguments == {'other': other_parsed_arguments, 'doit': action_parsed_arguments}
  104. def test_parse_and_record_action_arguments_with_borg_action_consumes_arguments_after_action_name():
  105. unparsed_arguments = ('--verbosity', '1', 'borg', 'list')
  106. parsed_arguments = {}
  107. borg_parsed_arguments = flexmock(options=flexmock())
  108. flexmock(module).should_receive('omit_values_colliding_with_action_names').and_return(
  109. unparsed_arguments,
  110. )
  111. borg_parser = flexmock()
  112. flexmock(borg_parser).should_receive('parse_known_args').and_return(
  113. borg_parsed_arguments,
  114. ('--verbosity', '1', 'borg', 'list'),
  115. )
  116. assert module.parse_and_record_action_arguments(
  117. unparsed_arguments,
  118. parsed_arguments,
  119. borg_parser,
  120. 'borg',
  121. ) == ('--verbosity', '1')
  122. assert parsed_arguments == {'borg': borg_parsed_arguments}
  123. assert borg_parsed_arguments.options == ('list',)
  124. @pytest.mark.parametrize(
  125. 'argument, expected',
  126. [
  127. ('--foo', True),
  128. ('foo', False),
  129. (33, False),
  130. ],
  131. )
  132. def test_argument_is_flag_only_for_string_starting_with_double_dash(argument, expected):
  133. assert module.argument_is_flag(argument) == expected
  134. @pytest.mark.parametrize(
  135. 'arguments, expected',
  136. [
  137. # Ending with a valueless flag.
  138. (
  139. ('--foo', '--bar', 33, '--baz'),
  140. (
  141. ('--foo',),
  142. ('--bar', 33),
  143. ('--baz',),
  144. ),
  145. ),
  146. # Ending with a flag and its corresponding value.
  147. (
  148. ('--foo', '--bar', 33, '--baz', '--quux', 'thing'),
  149. (('--foo',), ('--bar', 33), ('--baz',), ('--quux', 'thing')),
  150. ),
  151. # Starting with an action name.
  152. (
  153. ('check', '--foo', '--bar', 33, '--baz'),
  154. (
  155. ('check',),
  156. ('--foo',),
  157. ('--bar', 33),
  158. ('--baz',),
  159. ),
  160. ),
  161. # Action name that one could mistake for a flag value.
  162. (('--progress', 'list'), (('--progress',), ('list',))),
  163. # No arguments.
  164. ((), ()),
  165. ],
  166. )
  167. def test_group_arguments_with_values_returns_flags_with_corresponding_values(arguments, expected):
  168. flexmock(module).should_receive('argument_is_flag').with_args('--foo').and_return(True)
  169. flexmock(module).should_receive('argument_is_flag').with_args('--bar').and_return(True)
  170. flexmock(module).should_receive('argument_is_flag').with_args('--baz').and_return(True)
  171. flexmock(module).should_receive('argument_is_flag').with_args('--quux').and_return(True)
  172. flexmock(module).should_receive('argument_is_flag').with_args('--progress').and_return(True)
  173. flexmock(module).should_receive('argument_is_flag').with_args(33).and_return(False)
  174. flexmock(module).should_receive('argument_is_flag').with_args('thing').and_return(False)
  175. flexmock(module).should_receive('argument_is_flag').with_args('check').and_return(False)
  176. flexmock(module).should_receive('argument_is_flag').with_args('list').and_return(False)
  177. assert module.group_arguments_with_values(arguments) == expected
  178. @pytest.mark.parametrize(
  179. 'arguments, grouped_arguments, expected',
  180. [
  181. # An unparsable flag remaining from each parsed action.
  182. (
  183. (
  184. ('--latest', 'archive', 'prune', 'extract', 'list', '--flag'),
  185. ('--latest', 'archive', 'check', 'extract', 'list', '--flag'),
  186. ('prune', 'check', 'list', '--flag'),
  187. ('prune', 'check', 'extract', '--flag'),
  188. ),
  189. (
  190. (
  191. ('--latest',),
  192. ('archive',),
  193. ('prune',),
  194. ('extract',),
  195. ('list',),
  196. ('--flag',),
  197. ),
  198. (
  199. ('--latest',),
  200. ('archive',),
  201. ('check',),
  202. ('extract',),
  203. ('list',),
  204. ('--flag',),
  205. ),
  206. (('prune',), ('check',), ('list',), ('--flag',)),
  207. (('prune',), ('check',), ('extract',), ('--flag',)),
  208. ),
  209. ('--flag',),
  210. ),
  211. # No unparsable flags remaining.
  212. (
  213. (
  214. ('--archive', 'archive', 'prune', 'extract', 'list'),
  215. ('--archive', 'archive', 'check', 'extract', 'list'),
  216. ('prune', 'check', 'list'),
  217. ('prune', 'check', 'extract'),
  218. ),
  219. (
  220. (
  221. (
  222. '--archive',
  223. 'archive',
  224. ),
  225. ('prune',),
  226. ('extract',),
  227. ('list',),
  228. ),
  229. (
  230. (
  231. '--archive',
  232. 'archive',
  233. ),
  234. ('check',),
  235. ('extract',),
  236. ('list',),
  237. ),
  238. (('prune',), ('check',), ('list',)),
  239. (('prune',), ('check',), ('extract',)),
  240. ),
  241. (),
  242. ),
  243. # No unparsable flags remaining, but some values in common.
  244. (
  245. (
  246. ('--verbosity', '5', 'archive', 'prune', 'extract', 'list'),
  247. ('--last', '5', 'archive', 'check', 'extract', 'list'),
  248. ('prune', 'check', 'list', '--last', '5'),
  249. ('prune', 'check', '--verbosity', '5', 'extract'),
  250. ),
  251. (
  252. (('--verbosity', '5'), ('archive',), ('prune',), ('extract',), ('list',)),
  253. (
  254. (
  255. '--last',
  256. '5',
  257. ),
  258. ('archive',),
  259. ('check',),
  260. ('extract',),
  261. ('list',),
  262. ),
  263. (('prune',), ('check',), ('list',), ('--last', '5')),
  264. (
  265. ('prune',),
  266. ('check',),
  267. (
  268. '--verbosity',
  269. '5',
  270. ),
  271. ('extract',),
  272. ),
  273. ),
  274. (),
  275. ),
  276. # No flags.
  277. ((), (), ()),
  278. ],
  279. )
  280. def test_get_unparsable_arguments_returns_remaining_arguments_that_no_action_can_parse(
  281. arguments,
  282. grouped_arguments,
  283. expected,
  284. ):
  285. for action_arguments, grouped_action_arguments in zip(arguments, grouped_arguments):
  286. flexmock(module).should_receive('group_arguments_with_values').with_args(
  287. action_arguments,
  288. ).and_return(grouped_action_arguments)
  289. assert module.get_unparsable_arguments(arguments) == expected
  290. def test_parse_arguments_for_actions_consumes_action_arguments_after_action_name():
  291. action_namespace = flexmock(foo=True)
  292. remaining = flexmock()
  293. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  294. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  295. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  296. {action: action_namespace},
  297. )
  298. or remaining,
  299. )
  300. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  301. action_parsers = {'action': flexmock(), 'other': flexmock()}
  302. global_namespace = flexmock(config_paths=[])
  303. global_parser = flexmock()
  304. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  305. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  306. ('action', '--foo', 'true'),
  307. action_parsers,
  308. global_parser,
  309. )
  310. assert arguments == {'global': global_namespace, 'action': action_namespace}
  311. assert remaining_action_arguments == (remaining, ())
  312. def test_parse_arguments_for_actions_consumes_action_arguments_with_alias():
  313. action_namespace = flexmock(foo=True)
  314. remaining = flexmock()
  315. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  316. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  317. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  318. {canonical or action: action_namespace},
  319. )
  320. or remaining,
  321. )
  322. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  323. action_parsers = {
  324. 'action': flexmock(),
  325. '-a': flexmock(),
  326. 'other': flexmock(),
  327. '-o': flexmock(),
  328. }
  329. global_namespace = flexmock(config_paths=[])
  330. global_parser = flexmock()
  331. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  332. flexmock(module).ACTION_ALIASES = {'action': ['-a'], 'other': ['-o']}
  333. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  334. ('-a', '--foo', 'true'),
  335. action_parsers,
  336. global_parser,
  337. )
  338. assert arguments == {'global': global_namespace, 'action': action_namespace}
  339. assert remaining_action_arguments == (remaining, ())
  340. def test_parse_arguments_for_actions_consumes_multiple_action_arguments():
  341. action_namespace = flexmock(foo=True)
  342. other_namespace = flexmock(bar=3)
  343. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  344. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  345. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  346. {action: action_namespace if action == 'action' else other_namespace},
  347. )
  348. or (),
  349. ).and_return(('other', '--bar', '3')).and_return('action', '--foo', 'true')
  350. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  351. action_parsers = {
  352. 'action': flexmock(),
  353. 'other': flexmock(),
  354. }
  355. global_namespace = flexmock(config_paths=[])
  356. global_parser = flexmock()
  357. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  358. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  359. ('action', '--foo', 'true', 'other', '--bar', '3'),
  360. action_parsers,
  361. global_parser,
  362. )
  363. assert arguments == {
  364. 'global': global_namespace,
  365. 'action': action_namespace,
  366. 'other': other_namespace,
  367. }
  368. assert remaining_action_arguments == ((), (), ())
  369. def test_parse_arguments_for_actions_respects_command_line_action_ordering():
  370. other_namespace = flexmock()
  371. action_namespace = flexmock(foo=True)
  372. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  373. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  374. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  375. {action: other_namespace if action == 'other' else action_namespace},
  376. )
  377. or (),
  378. ).and_return(('action',)).and_return(('other', '--foo', 'true'))
  379. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  380. action_parsers = {
  381. 'action': flexmock(),
  382. 'other': flexmock(),
  383. }
  384. global_namespace = flexmock(config_paths=[])
  385. global_parser = flexmock()
  386. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  387. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  388. ('other', '--foo', 'true', 'action'),
  389. action_parsers,
  390. global_parser,
  391. )
  392. assert arguments == collections.OrderedDict(
  393. [('other', other_namespace), ('action', action_namespace), ('global', global_namespace)],
  394. )
  395. assert remaining_action_arguments == ((), (), ())
  396. def test_parse_arguments_for_actions_applies_default_action_parsers():
  397. global_namespace = flexmock(config_paths=[])
  398. namespaces = {
  399. 'global': global_namespace,
  400. 'prune': flexmock(),
  401. 'compact': flexmock(),
  402. 'create': flexmock(progress=True),
  403. 'check': flexmock(),
  404. }
  405. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  406. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  407. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  408. {action: namespaces.get(action)},
  409. )
  410. or (),
  411. ).and_return(())
  412. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  413. action_parsers = {
  414. 'prune': flexmock(),
  415. 'compact': flexmock(),
  416. 'create': flexmock(),
  417. 'check': flexmock(),
  418. 'other': flexmock(),
  419. }
  420. global_parser = flexmock()
  421. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  422. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  423. ('--progress'),
  424. action_parsers,
  425. global_parser,
  426. )
  427. assert arguments == namespaces
  428. assert remaining_action_arguments == ((), (), (), (), ())
  429. def test_parse_arguments_for_actions_consumes_global_arguments():
  430. action_namespace = flexmock()
  431. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  432. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  433. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  434. {action: action_namespace},
  435. )
  436. or ('--verbosity', 'lots'),
  437. )
  438. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  439. action_parsers = {
  440. 'action': flexmock(),
  441. 'other': flexmock(),
  442. }
  443. global_namespace = flexmock(config_paths=[])
  444. global_parser = flexmock()
  445. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  446. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  447. ('action', '--verbosity', 'lots'),
  448. action_parsers,
  449. global_parser,
  450. )
  451. assert arguments == {'global': global_namespace, 'action': action_namespace}
  452. assert remaining_action_arguments == (('--verbosity', 'lots'), ())
  453. def test_parse_arguments_for_actions_passes_through_unknown_arguments_before_action_name():
  454. action_namespace = flexmock()
  455. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  456. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  457. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  458. {action: action_namespace},
  459. )
  460. or ('--wtf', 'yes'),
  461. )
  462. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  463. action_parsers = {
  464. 'action': flexmock(),
  465. 'other': flexmock(),
  466. }
  467. global_namespace = flexmock(config_paths=[])
  468. global_parser = flexmock()
  469. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  470. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  471. ('--wtf', 'yes', 'action'),
  472. action_parsers,
  473. global_parser,
  474. )
  475. assert arguments == {'global': global_namespace, 'action': action_namespace}
  476. assert remaining_action_arguments == (('--wtf', 'yes'), ())
  477. def test_parse_arguments_for_actions_passes_through_unknown_arguments_after_action_name():
  478. action_namespace = flexmock()
  479. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  480. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  481. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  482. {action: action_namespace},
  483. )
  484. or ('--wtf', 'yes'),
  485. )
  486. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  487. action_parsers = {
  488. 'action': flexmock(),
  489. 'other': flexmock(),
  490. }
  491. global_namespace = flexmock(config_paths=[])
  492. global_parser = flexmock()
  493. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  494. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  495. ('action', '--wtf', 'yes'),
  496. action_parsers,
  497. global_parser,
  498. )
  499. assert arguments == {'global': global_namespace, 'action': action_namespace}
  500. assert remaining_action_arguments == (('--wtf', 'yes'), ())
  501. def test_parse_arguments_for_actions_with_borg_action_skips_other_action_parsers():
  502. action_namespace = flexmock(options=[])
  503. flexmock(module).should_receive('get_subaction_parsers').and_return({})
  504. flexmock(module).should_receive('parse_and_record_action_arguments').replace_with(
  505. lambda unparsed, parsed, parser, action, canonical=None: parsed.update(
  506. {action: action_namespace},
  507. )
  508. or (),
  509. ).and_return(())
  510. flexmock(module).should_receive('get_subactions_for_actions').and_return({})
  511. action_parsers = {
  512. 'borg': flexmock(),
  513. 'list': flexmock(),
  514. }
  515. global_namespace = flexmock(config_paths=[])
  516. global_parser = flexmock()
  517. global_parser.should_receive('parse_known_args').and_return((global_namespace, ()))
  518. arguments, remaining_action_arguments = module.parse_arguments_for_actions(
  519. ('borg', 'list'),
  520. action_parsers,
  521. global_parser,
  522. )
  523. assert arguments == {'global': global_namespace, 'borg': action_namespace}
  524. assert remaining_action_arguments == ((), ())
  525. def test_parse_arguments_for_actions_raises_error_when_no_action_is_specified():
  526. flexmock(module).should_receive('get_subaction_parsers').and_return({'bootstrap': [flexmock()]})
  527. flexmock(module).should_receive('parse_and_record_action_arguments').and_return(flexmock())
  528. flexmock(module).should_receive('get_subactions_for_actions').and_return(
  529. {'config': ['bootstrap']},
  530. )
  531. action_parsers = {'config': flexmock()}
  532. global_parser = flexmock()
  533. global_parser.should_receive('parse_known_args').and_return((flexmock(), ()))
  534. with pytest.raises(ValueError):
  535. module.parse_arguments_for_actions(('config',), action_parsers, global_parser)
  536. def test_make_argument_description_with_object_adds_example():
  537. buffer = flexmock()
  538. buffer.should_receive('getvalue').and_return('{foo: example}')
  539. flexmock(module.io).should_receive('StringIO').and_return(buffer)
  540. yaml = flexmock()
  541. yaml.should_receive('dump')
  542. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
  543. assert (
  544. module.make_argument_description(
  545. schema={
  546. 'description': 'Thing.',
  547. 'type': 'object',
  548. 'example': {'foo': 'example'},
  549. },
  550. flag_name='flag',
  551. )
  552. == 'Thing. Example value: "{foo: example}"'
  553. )
  554. def test_make_argument_description_without_description_and_with_object_sets_example():
  555. buffer = flexmock()
  556. buffer.should_receive('getvalue').and_return('{foo: example}')
  557. flexmock(module.io).should_receive('StringIO').and_return(buffer)
  558. yaml = flexmock()
  559. yaml.should_receive('dump')
  560. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
  561. assert (
  562. module.make_argument_description(
  563. schema={
  564. 'type': 'object',
  565. 'example': {'foo': 'example'},
  566. },
  567. flag_name='flag',
  568. )
  569. == 'Example value: "{foo: example}"'
  570. )
  571. def test_make_argument_description_with_object_skips_missing_example():
  572. flexmock(module.ruamel.yaml).should_receive('YAML').never()
  573. assert (
  574. module.make_argument_description(
  575. schema={
  576. 'description': 'Thing.',
  577. 'type': 'object',
  578. },
  579. flag_name='flag',
  580. )
  581. == 'Thing.'
  582. )
  583. def test_make_argument_description_with_array_adds_example():
  584. buffer = flexmock()
  585. buffer.should_receive('getvalue').and_return('[example]')
  586. flexmock(module.io).should_receive('StringIO').and_return(buffer)
  587. yaml = flexmock()
  588. yaml.should_receive('dump')
  589. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
  590. assert (
  591. module.make_argument_description(
  592. schema={
  593. 'description': 'Thing.',
  594. 'type': 'array',
  595. 'example': ['example'],
  596. },
  597. flag_name='flag',
  598. )
  599. == 'Thing. Example value: "[example]"'
  600. )
  601. def test_make_argument_description_without_description_and_with_array_sets_example():
  602. buffer = flexmock()
  603. buffer.should_receive('getvalue').and_return('[example]')
  604. flexmock(module.io).should_receive('StringIO').and_return(buffer)
  605. yaml = flexmock()
  606. yaml.should_receive('dump')
  607. flexmock(module.ruamel.yaml).should_receive('YAML').and_return(yaml)
  608. assert (
  609. module.make_argument_description(
  610. schema={
  611. 'type': 'array',
  612. 'example': ['example'],
  613. },
  614. flag_name='flag',
  615. )
  616. == 'Example value: "[example]"'
  617. )
  618. def test_make_argument_description_with_array_skips_missing_example():
  619. flexmock(module.ruamel.yaml).should_receive('YAML').never()
  620. assert (
  621. module.make_argument_description(
  622. schema={
  623. 'description': 'Thing.',
  624. 'type': 'array',
  625. },
  626. flag_name='flag',
  627. )
  628. == 'Thing.'
  629. )
  630. def test_make_argument_description_with_array_index_in_flag_name_adds_to_description():
  631. assert 'list element' in module.make_argument_description(
  632. schema={
  633. 'description': 'Thing.',
  634. 'type': 'something',
  635. },
  636. flag_name='flag[0]',
  637. )
  638. def test_make_argument_description_without_description_and_with_array_index_in_flag_name_sets_description():
  639. assert 'list element' in module.make_argument_description(
  640. schema={
  641. 'type': 'something',
  642. },
  643. flag_name='flag[0]',
  644. )
  645. def test_make_argument_description_escapes_percent_character():
  646. assert (
  647. module.make_argument_description(
  648. schema={
  649. 'description': '% Thing.',
  650. 'type': 'something',
  651. },
  652. flag_name='flag',
  653. )
  654. == '%% Thing.'
  655. )
  656. def test_add_array_element_arguments_without_array_index_bails():
  657. arguments_group = flexmock()
  658. arguments_group.should_receive('add_argument').never()
  659. module.add_array_element_arguments(
  660. arguments_group=arguments_group,
  661. unparsed_arguments=(),
  662. flag_name='foo',
  663. )
  664. def test_add_array_element_arguments_with_help_flag_bails():
  665. arguments_group = flexmock()
  666. arguments_group.should_receive('add_argument').never()
  667. module.add_array_element_arguments(
  668. arguments_group=arguments_group,
  669. unparsed_arguments=('--foo', '--help', '--bar'),
  670. flag_name='foo[0]',
  671. )
  672. def test_add_array_element_arguments_without_any_flags_bails():
  673. arguments_group = flexmock()
  674. arguments_group.should_receive('add_argument').never()
  675. module.add_array_element_arguments(
  676. arguments_group=arguments_group,
  677. unparsed_arguments=(),
  678. flag_name='foo[0]',
  679. )
  680. # Use this instead of a flexmock because it's not easy to check the type() of a flexmock instance.
  681. Group_action = collections.namedtuple(
  682. 'Group_action',
  683. (
  684. 'option_strings',
  685. 'choices',
  686. 'default',
  687. 'nargs',
  688. 'required',
  689. 'type',
  690. ),
  691. defaults=(
  692. flexmock(),
  693. flexmock(),
  694. flexmock(),
  695. flexmock(),
  696. flexmock(),
  697. ),
  698. )
  699. def test_add_array_element_arguments_without_array_index_flags_bails():
  700. arguments_group = flexmock(
  701. _group_actions=(
  702. Group_action(
  703. option_strings=('--foo[0].val',),
  704. ),
  705. ),
  706. _registries={'action': {'store_stuff': Group_action}},
  707. )
  708. arguments_group.should_receive('add_argument').never()
  709. module.add_array_element_arguments(
  710. arguments_group=arguments_group,
  711. unparsed_arguments=('--foo', '--bar'),
  712. flag_name='foo[0].val',
  713. )
  714. def test_add_array_element_arguments_with_non_matching_array_index_flags_bails():
  715. arguments_group = flexmock(
  716. _group_actions=(
  717. Group_action(
  718. option_strings=('--foo[0].val',),
  719. ),
  720. ),
  721. _registries={'action': {'store_stuff': Group_action}},
  722. )
  723. arguments_group.should_receive('add_argument').never()
  724. module.add_array_element_arguments(
  725. arguments_group=arguments_group,
  726. unparsed_arguments=('--foo', '--bar[25].val', 'barval'),
  727. flag_name='foo[0].val',
  728. )
  729. def test_add_array_element_arguments_with_identical_array_index_flag_bails():
  730. arguments_group = flexmock(
  731. _group_actions=(
  732. Group_action(
  733. option_strings=('--foo[0].val',),
  734. ),
  735. ),
  736. _registries={'action': {'store_stuff': Group_action}},
  737. )
  738. arguments_group.should_receive('add_argument').never()
  739. module.add_array_element_arguments(
  740. arguments_group=arguments_group,
  741. unparsed_arguments=('--foo[0].val', 'fooval', '--bar'),
  742. flag_name='foo[0].val',
  743. )
  744. def test_add_array_element_arguments_without_action_type_in_registry_bails():
  745. arguments_group = flexmock(
  746. _group_actions=(
  747. Group_action(
  748. option_strings=('--foo[0].val',),
  749. choices=flexmock(),
  750. default=flexmock(),
  751. nargs=flexmock(),
  752. required=flexmock(),
  753. type=flexmock(),
  754. ),
  755. ),
  756. _registries={'action': {'store_stuff': bool}},
  757. )
  758. arguments_group.should_receive('add_argument').never()
  759. module.add_array_element_arguments(
  760. arguments_group=arguments_group,
  761. unparsed_arguments=('--foo[25].val', 'fooval', '--bar[1].val', 'barval'),
  762. flag_name='foo[0].val',
  763. )
  764. def test_add_array_element_arguments_adds_arguments_for_array_index_flags():
  765. arguments_group = flexmock(
  766. _group_actions=(
  767. Group_action(
  768. option_strings=('--foo[0].val',),
  769. choices=flexmock(),
  770. default=flexmock(),
  771. nargs=flexmock(),
  772. required=flexmock(),
  773. type=flexmock(),
  774. ),
  775. ),
  776. _registries={'action': {'store_stuff': Group_action}},
  777. )
  778. arguments_group.should_receive('add_argument').with_args(
  779. '--foo[25].val',
  780. action='store_stuff',
  781. choices=object,
  782. default=object,
  783. dest='foo[25].val',
  784. nargs=object,
  785. required=object,
  786. type=object,
  787. ).once()
  788. module.add_array_element_arguments(
  789. arguments_group=arguments_group,
  790. unparsed_arguments=('--foo[25].val', 'fooval', '--bar[1].val', 'barval'),
  791. flag_name='foo[0].val',
  792. )
  793. def test_add_array_element_arguments_adds_arguments_for_array_index_flags_with_equals_sign():
  794. arguments_group = flexmock(
  795. _group_actions=(
  796. Group_action(
  797. option_strings=('--foo[0].val',),
  798. choices=flexmock(),
  799. default=flexmock(),
  800. nargs=flexmock(),
  801. required=flexmock(),
  802. type=flexmock(),
  803. ),
  804. ),
  805. _registries={'action': {'store_stuff': Group_action}},
  806. )
  807. arguments_group.should_receive('add_argument').with_args(
  808. '--foo[25].val',
  809. action='store_stuff',
  810. choices=object,
  811. default=object,
  812. dest='foo[25].val',
  813. nargs=object,
  814. required=object,
  815. type=object,
  816. ).once()
  817. module.add_array_element_arguments(
  818. arguments_group=arguments_group,
  819. unparsed_arguments=('--foo[25].val=fooval', '--bar[1].val=barval'),
  820. flag_name='foo[0].val',
  821. )
  822. def test_add_array_element_arguments_adds_arguments_for_array_index_flags_with_dashes():
  823. arguments_group = flexmock(
  824. _group_actions=(
  825. Group_action(
  826. option_strings=('--foo[0].val-and-stuff',),
  827. choices=flexmock(),
  828. default=flexmock(),
  829. nargs=flexmock(),
  830. required=flexmock(),
  831. type=flexmock(),
  832. ),
  833. ),
  834. _registries={'action': {'store_stuff': Group_action}},
  835. )
  836. arguments_group.should_receive('add_argument').with_args(
  837. '--foo[25].val-and-stuff',
  838. action='store_stuff',
  839. choices=object,
  840. default=object,
  841. dest='foo[25].val_and_stuff',
  842. nargs=object,
  843. required=object,
  844. type=object,
  845. ).once()
  846. module.add_array_element_arguments(
  847. arguments_group=arguments_group,
  848. unparsed_arguments=('--foo[25].val-and-stuff', 'fooval', '--bar[1].val', 'barval'),
  849. flag_name='foo[0].val-and-stuff',
  850. )
  851. def test_add_arguments_from_schema_with_non_dict_schema_bails():
  852. arguments_group = flexmock()
  853. flexmock(module).should_receive('make_argument_description').never()
  854. flexmock(module.borgmatic.config.schema).should_receive('parse_type').never()
  855. arguments_group.should_receive('add_argument').never()
  856. module.add_arguments_from_schema(
  857. arguments_group=arguments_group,
  858. schema='foo',
  859. unparsed_arguments=(),
  860. )
  861. def test_add_arguments_from_schema_with_nested_object_adds_flag_for_each_option():
  862. arguments_group = flexmock()
  863. flexmock(module).should_receive('make_argument_description').and_return('help 1').and_return(
  864. 'help 2',
  865. )
  866. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(
  867. int,
  868. ).and_return(str)
  869. arguments_group.should_receive('add_argument').with_args(
  870. '--foo.bar',
  871. type=int,
  872. metavar='BAR',
  873. help='help 1',
  874. ).once()
  875. arguments_group.should_receive('add_argument').with_args(
  876. '--foo.baz',
  877. type=str,
  878. metavar='BAZ',
  879. help='help 2',
  880. ).once()
  881. flexmock(module).should_receive('add_array_element_arguments')
  882. module.add_arguments_from_schema(
  883. arguments_group=arguments_group,
  884. schema={
  885. 'type': 'object',
  886. 'properties': {
  887. 'foo': {
  888. 'type': 'object',
  889. 'properties': {
  890. 'bar': {'type': 'integer'},
  891. 'baz': {'type': 'str'},
  892. },
  893. },
  894. },
  895. },
  896. unparsed_arguments=(),
  897. )
  898. def test_add_arguments_from_schema_uses_first_non_null_type_from_multi_type_object():
  899. arguments_group = flexmock()
  900. flexmock(module).should_receive('make_argument_description').and_return('help 1')
  901. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int)
  902. arguments_group.should_receive('add_argument').with_args(
  903. '--foo.bar',
  904. type=int,
  905. metavar='BAR',
  906. help='help 1',
  907. ).once()
  908. flexmock(module).should_receive('add_array_element_arguments')
  909. module.add_arguments_from_schema(
  910. arguments_group=arguments_group,
  911. schema={
  912. 'type': 'object',
  913. 'properties': {
  914. 'foo': {
  915. 'type': ['null', 'object', 'boolean'],
  916. 'properties': {
  917. 'bar': {'type': 'integer'},
  918. },
  919. },
  920. },
  921. },
  922. unparsed_arguments=(),
  923. )
  924. def test_add_arguments_from_schema_with_empty_multi_type_raises():
  925. arguments_group = flexmock()
  926. flexmock(module).should_receive('make_argument_description').and_return('help 1')
  927. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(int)
  928. arguments_group.should_receive('add_argument').never()
  929. flexmock(module).should_receive('add_array_element_arguments').never()
  930. with pytest.raises(ValueError):
  931. module.add_arguments_from_schema(
  932. arguments_group=arguments_group,
  933. schema={
  934. 'type': 'object',
  935. 'properties': {
  936. 'foo': {
  937. 'type': [],
  938. 'properties': {
  939. 'bar': {'type': 'integer'},
  940. },
  941. },
  942. },
  943. },
  944. unparsed_arguments=(),
  945. )
  946. def test_add_arguments_from_schema_with_propertyless_option_adds_flag():
  947. arguments_group = flexmock()
  948. flexmock(module).should_receive('make_argument_description').and_return('help')
  949. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
  950. arguments_group.should_receive('add_argument').with_args(
  951. '--foo',
  952. type=str,
  953. metavar='FOO',
  954. help='help',
  955. ).once()
  956. flexmock(module).should_receive('add_array_element_arguments')
  957. module.add_arguments_from_schema(
  958. arguments_group=arguments_group,
  959. schema={
  960. 'type': 'object',
  961. 'properties': {
  962. 'foo': {
  963. 'type': 'object',
  964. },
  965. },
  966. },
  967. unparsed_arguments=(),
  968. )
  969. def test_add_arguments_from_schema_with_array_of_scalars_adds_multiple_flags():
  970. arguments_group = flexmock()
  971. flexmock(module).should_receive('make_argument_description').and_return('help')
  972. flexmock(module.borgmatic.config.schema).should_receive('parse_type').with_args(
  973. 'integer',
  974. object=str,
  975. array=str,
  976. ).and_return(int)
  977. flexmock(module.borgmatic.config.schema).should_receive('parse_type').with_args(
  978. 'array',
  979. object=str,
  980. array=str,
  981. ).and_return(str)
  982. arguments_group.should_receive('add_argument').with_args(
  983. '--foo[0]',
  984. type=int,
  985. metavar='FOO[0]',
  986. help='help',
  987. ).once()
  988. arguments_group.should_receive('add_argument').with_args(
  989. '--foo',
  990. type=str,
  991. metavar='FOO',
  992. help='help',
  993. ).once()
  994. flexmock(module).should_receive('add_array_element_arguments')
  995. module.add_arguments_from_schema(
  996. arguments_group=arguments_group,
  997. schema={
  998. 'type': 'object',
  999. 'properties': {
  1000. 'foo': {
  1001. 'type': 'array',
  1002. 'items': {
  1003. 'type': 'integer',
  1004. },
  1005. },
  1006. },
  1007. },
  1008. unparsed_arguments=(),
  1009. )
  1010. def test_add_arguments_from_schema_with_array_of_objects_adds_multiple_flags():
  1011. arguments_group = flexmock()
  1012. flexmock(module).should_receive('make_argument_description').and_return('help 1').and_return(
  1013. 'help 2',
  1014. )
  1015. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(
  1016. int,
  1017. ).and_return(str)
  1018. arguments_group.should_receive('add_argument').with_args(
  1019. '--foo[0].bar',
  1020. type=int,
  1021. metavar='BAR',
  1022. help='help 1',
  1023. ).once()
  1024. arguments_group.should_receive('add_argument').with_args(
  1025. '--foo',
  1026. type=str,
  1027. metavar='FOO',
  1028. help='help 2',
  1029. ).once()
  1030. flexmock(module).should_receive('add_array_element_arguments')
  1031. flexmock(module).should_receive('add_array_element_arguments').with_args(
  1032. arguments_group=arguments_group,
  1033. unparsed_arguments=(),
  1034. flag_name='foo[0].bar',
  1035. ).once()
  1036. module.add_arguments_from_schema(
  1037. arguments_group=arguments_group,
  1038. schema={
  1039. 'type': 'object',
  1040. 'properties': {
  1041. 'foo': {
  1042. 'type': 'array',
  1043. 'items': {
  1044. 'type': 'object',
  1045. 'properties': {
  1046. 'bar': {
  1047. 'type': 'integer',
  1048. },
  1049. },
  1050. },
  1051. },
  1052. },
  1053. },
  1054. unparsed_arguments=(),
  1055. )
  1056. def test_add_arguments_from_schema_with_boolean_adds_two_valueless_flags():
  1057. arguments_group = flexmock()
  1058. flexmock(module).should_receive('make_argument_description').and_return('help')
  1059. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
  1060. arguments_group.should_receive('add_argument').with_args(
  1061. '--foo',
  1062. action='store_true',
  1063. default=None,
  1064. help='help',
  1065. ).once()
  1066. arguments_group.should_receive('add_argument').with_args(
  1067. '--no-foo',
  1068. dest='foo',
  1069. action='store_false',
  1070. default=None,
  1071. help=object,
  1072. ).once()
  1073. flexmock(module).should_receive('add_array_element_arguments')
  1074. module.add_arguments_from_schema(
  1075. arguments_group=arguments_group,
  1076. schema={
  1077. 'type': 'object',
  1078. 'properties': {
  1079. 'foo': {
  1080. 'type': 'boolean',
  1081. },
  1082. },
  1083. },
  1084. unparsed_arguments=(),
  1085. )
  1086. def test_add_arguments_from_schema_with_nested_boolean_adds_two_valueless_flags():
  1087. arguments_group = flexmock()
  1088. flexmock(module).should_receive('make_argument_description').and_return('help')
  1089. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
  1090. arguments_group.should_receive('add_argument').with_args(
  1091. '--foo.bar.baz-quux',
  1092. action='store_true',
  1093. default=None,
  1094. help='help',
  1095. ).once()
  1096. arguments_group.should_receive('add_argument').with_args(
  1097. '--foo.bar.no-baz-quux',
  1098. dest='foo.bar.baz_quux',
  1099. action='store_false',
  1100. default=None,
  1101. help=object,
  1102. ).once()
  1103. flexmock(module).should_receive('add_array_element_arguments')
  1104. module.add_arguments_from_schema(
  1105. arguments_group=arguments_group,
  1106. schema={
  1107. 'type': 'object',
  1108. 'properties': {
  1109. 'baz_quux': {
  1110. 'type': 'boolean',
  1111. },
  1112. },
  1113. },
  1114. unparsed_arguments=(),
  1115. names=('foo', 'bar'),
  1116. )
  1117. def test_add_arguments_from_schema_with_boolean_with_name_prefixed_with_no_adds_two_valueless_flags_and_removes_the_no_for_one():
  1118. arguments_group = flexmock()
  1119. flexmock(module).should_receive('make_argument_description').and_return('help')
  1120. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(bool)
  1121. arguments_group.should_receive('add_argument').with_args(
  1122. '--no-foo',
  1123. action='store_true',
  1124. default=None,
  1125. help='help',
  1126. ).once()
  1127. arguments_group.should_receive('add_argument').with_args(
  1128. '--foo',
  1129. dest='no_foo',
  1130. action='store_false',
  1131. default=None,
  1132. help=object,
  1133. ).once()
  1134. flexmock(module).should_receive('add_array_element_arguments')
  1135. module.add_arguments_from_schema(
  1136. arguments_group=arguments_group,
  1137. schema={
  1138. 'type': 'object',
  1139. 'properties': {
  1140. 'no_foo': {
  1141. 'type': 'boolean',
  1142. },
  1143. },
  1144. },
  1145. unparsed_arguments=(),
  1146. )
  1147. def test_add_arguments_from_schema_skips_omitted_flag_name():
  1148. arguments_group = flexmock()
  1149. flexmock(module).should_receive('make_argument_description').and_return('help')
  1150. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
  1151. arguments_group.should_receive('add_argument').with_args(
  1152. '--match-archives',
  1153. type=object,
  1154. metavar=object,
  1155. help=object,
  1156. ).never()
  1157. arguments_group.should_receive('add_argument').with_args(
  1158. '--foo',
  1159. type=str,
  1160. metavar='FOO',
  1161. help='help',
  1162. ).once()
  1163. flexmock(module).should_receive('add_array_element_arguments')
  1164. module.add_arguments_from_schema(
  1165. arguments_group=arguments_group,
  1166. schema={
  1167. 'type': 'object',
  1168. 'properties': {
  1169. 'match_archives': {
  1170. 'type': 'string',
  1171. },
  1172. 'foo': {
  1173. 'type': 'string',
  1174. },
  1175. },
  1176. },
  1177. unparsed_arguments=(),
  1178. )
  1179. def test_add_arguments_from_schema_rewrites_option_name_to_flag_name():
  1180. arguments_group = flexmock()
  1181. flexmock(module).should_receive('make_argument_description').and_return('help')
  1182. flexmock(module.borgmatic.config.schema).should_receive('parse_type').and_return(str)
  1183. arguments_group.should_receive('add_argument').with_args(
  1184. '--foo-and-stuff',
  1185. type=str,
  1186. metavar='FOO_AND_STUFF',
  1187. help='help',
  1188. ).once()
  1189. flexmock(module).should_receive('add_array_element_arguments')
  1190. module.add_arguments_from_schema(
  1191. arguments_group=arguments_group,
  1192. schema={
  1193. 'type': 'object',
  1194. 'properties': {
  1195. 'foo_and_stuff': {
  1196. 'type': 'string',
  1197. },
  1198. },
  1199. },
  1200. unparsed_arguments=(),
  1201. )