test_list.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. import argparse
  2. import logging
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.borg import list as module
  6. from ..test_verbosity import insert_logging_mock
  7. def test_make_list_command_includes_log_info():
  8. insert_logging_mock(logging.INFO)
  9. flexmock(module.flags).should_receive('make_flags').and_return(())
  10. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  11. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  12. command = module.make_list_command(
  13. repository_path='repo',
  14. config={},
  15. local_borg_version='1.2.3',
  16. list_arguments=flexmock(archive=None, paths=None, json=False),
  17. global_arguments=flexmock(log_json=False),
  18. )
  19. assert command == ('borg', 'list', '--info', 'repo')
  20. def test_make_list_command_includes_json_but_not_info():
  21. insert_logging_mock(logging.INFO)
  22. flexmock(module.flags).should_receive('make_flags').and_return(())
  23. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  24. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  25. command = module.make_list_command(
  26. repository_path='repo',
  27. config={},
  28. local_borg_version='1.2.3',
  29. list_arguments=flexmock(archive=None, paths=None, json=True),
  30. global_arguments=flexmock(log_json=False),
  31. )
  32. assert command == ('borg', 'list', '--json', 'repo')
  33. def test_make_list_command_includes_log_debug():
  34. insert_logging_mock(logging.DEBUG)
  35. flexmock(module.flags).should_receive('make_flags').and_return(())
  36. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  37. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  38. command = module.make_list_command(
  39. repository_path='repo',
  40. config={},
  41. local_borg_version='1.2.3',
  42. list_arguments=flexmock(archive=None, paths=None, json=False),
  43. global_arguments=flexmock(log_json=False),
  44. )
  45. assert command == ('borg', 'list', '--debug', '--show-rc', 'repo')
  46. def test_make_list_command_includes_json_but_not_debug():
  47. insert_logging_mock(logging.DEBUG)
  48. flexmock(module.flags).should_receive('make_flags').and_return(())
  49. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  50. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  51. command = module.make_list_command(
  52. repository_path='repo',
  53. config={},
  54. local_borg_version='1.2.3',
  55. list_arguments=flexmock(archive=None, paths=None, json=True),
  56. global_arguments=flexmock(log_json=False),
  57. )
  58. assert command == ('borg', 'list', '--json', 'repo')
  59. def test_make_list_command_includes_json():
  60. flexmock(module.flags).should_receive('make_flags').and_return(())
  61. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  62. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  63. command = module.make_list_command(
  64. repository_path='repo',
  65. config={},
  66. local_borg_version='1.2.3',
  67. list_arguments=flexmock(archive=None, paths=None, json=True),
  68. global_arguments=flexmock(log_json=False),
  69. )
  70. assert command == ('borg', 'list', '--json', 'repo')
  71. def test_make_list_command_includes_log_json():
  72. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(('--log-json',))
  73. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  74. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  75. command = module.make_list_command(
  76. repository_path='repo',
  77. config={},
  78. local_borg_version='1.2.3',
  79. list_arguments=flexmock(archive=None, paths=None, json=False),
  80. global_arguments=flexmock(log_json=True),
  81. )
  82. assert command == ('borg', 'list', '--log-json', 'repo')
  83. def test_make_list_command_includes_lock_wait():
  84. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  85. ('--lock-wait', '5')
  86. )
  87. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  88. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  89. command = module.make_list_command(
  90. repository_path='repo',
  91. config={'lock_wait': 5},
  92. local_borg_version='1.2.3',
  93. list_arguments=flexmock(archive=None, paths=None, json=False),
  94. global_arguments=flexmock(log_json=False),
  95. )
  96. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  97. def test_make_list_command_includes_archive():
  98. flexmock(module.flags).should_receive('make_flags').and_return(())
  99. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  100. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  101. ('repo::archive',)
  102. )
  103. command = module.make_list_command(
  104. repository_path='repo',
  105. config={},
  106. local_borg_version='1.2.3',
  107. list_arguments=flexmock(archive='archive', paths=None, json=False),
  108. global_arguments=flexmock(log_json=False),
  109. )
  110. assert command == ('borg', 'list', 'repo::archive')
  111. def test_make_list_command_includes_archive_and_path():
  112. flexmock(module.flags).should_receive('make_flags').and_return(())
  113. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  114. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  115. ('repo::archive',)
  116. )
  117. command = module.make_list_command(
  118. repository_path='repo',
  119. config={},
  120. local_borg_version='1.2.3',
  121. list_arguments=flexmock(archive='archive', paths=['var/lib'], json=False),
  122. global_arguments=flexmock(log_json=False),
  123. )
  124. assert command == ('borg', 'list', 'repo::archive', 'var/lib')
  125. def test_make_list_command_includes_local_path():
  126. flexmock(module.flags).should_receive('make_flags').and_return(())
  127. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  128. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  129. command = module.make_list_command(
  130. repository_path='repo',
  131. config={},
  132. local_borg_version='1.2.3',
  133. list_arguments=flexmock(archive=None, paths=None, json=False),
  134. global_arguments=flexmock(log_json=False),
  135. local_path='borg2',
  136. )
  137. assert command == ('borg2', 'list', 'repo')
  138. def test_make_list_command_includes_remote_path():
  139. flexmock(module.flags).should_receive('make_flags').and_return(())
  140. flexmock(module.flags).should_receive('make_flags').with_args(
  141. 'remote-path', 'borg2'
  142. ).and_return(('--remote-path', 'borg2'))
  143. flexmock(module.flags).should_receive('make_flags').with_args('log-json', True).and_return(
  144. ('--log-json')
  145. )
  146. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  147. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  148. command = module.make_list_command(
  149. repository_path='repo',
  150. config={},
  151. local_borg_version='1.2.3',
  152. list_arguments=flexmock(archive=None, paths=None, json=False),
  153. global_arguments=flexmock(log_json=False),
  154. remote_path='borg2',
  155. )
  156. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  157. def test_make_list_command_includes_short():
  158. flexmock(module.flags).should_receive('make_flags').and_return(())
  159. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  160. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  161. command = module.make_list_command(
  162. repository_path='repo',
  163. config={},
  164. local_borg_version='1.2.3',
  165. list_arguments=flexmock(archive=None, paths=None, json=False, short=True),
  166. global_arguments=flexmock(log_json=False),
  167. )
  168. assert command == ('borg', 'list', '--short', 'repo')
  169. @pytest.mark.parametrize(
  170. 'argument_name',
  171. (
  172. 'prefix',
  173. 'match_archives',
  174. 'sort_by',
  175. 'first',
  176. 'last',
  177. 'exclude',
  178. 'exclude_from',
  179. 'pattern',
  180. 'patterns_from',
  181. ),
  182. )
  183. def test_make_list_command_includes_additional_flags(argument_name):
  184. flexmock(module.flags).should_receive('make_flags').and_return(())
  185. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  186. (f"--{argument_name.replace('_', '-')}", 'value')
  187. )
  188. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  189. command = module.make_list_command(
  190. repository_path='repo',
  191. config={},
  192. local_borg_version='1.2.3',
  193. list_arguments=flexmock(
  194. archive=None,
  195. paths=None,
  196. json=False,
  197. find_paths=None,
  198. format=None,
  199. **{argument_name: 'value'},
  200. ),
  201. global_arguments=flexmock(log_json=False),
  202. )
  203. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  204. def test_make_find_paths_considers_none_as_empty_paths():
  205. assert module.make_find_paths(None) == ()
  206. def test_make_find_paths_passes_through_patterns():
  207. find_paths = (
  208. 'fm:*',
  209. 'sh:**/*.txt',
  210. 're:^.*$',
  211. 'pp:root/somedir',
  212. 'pf:root/foo.txt',
  213. 'R /',
  214. 'r /',
  215. 'p /',
  216. 'P /',
  217. '+ /',
  218. '- /',
  219. '! /',
  220. )
  221. assert module.make_find_paths(find_paths) == find_paths
  222. def test_make_find_paths_adds_globs_to_path_fragments():
  223. assert module.make_find_paths(('foo.txt',)) == ('sh:**/*foo.txt*/**',)
  224. def test_capture_archive_listing_does_not_raise():
  225. flexmock(module.environment).should_receive('make_environment')
  226. flexmock(module).should_receive('execute_command_and_capture_output').and_return('')
  227. flexmock(module).should_receive('make_list_command')
  228. module.capture_archive_listing(
  229. repository_path='repo',
  230. archive='archive',
  231. config={},
  232. local_borg_version=flexmock(),
  233. global_arguments=flexmock(log_json=False),
  234. )
  235. def test_list_archive_calls_borg_with_flags():
  236. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  237. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  238. flexmock(module.logger).answer = lambda message: None
  239. list_arguments = argparse.Namespace(
  240. archive='archive',
  241. paths=None,
  242. json=False,
  243. find_paths=None,
  244. prefix=None,
  245. match_archives=None,
  246. sort_by=None,
  247. first=None,
  248. last=None,
  249. )
  250. global_arguments = flexmock(log_json=False)
  251. flexmock(module.feature).should_receive('available').and_return(False)
  252. flexmock(module).should_receive('make_list_command').with_args(
  253. repository_path='repo',
  254. config={},
  255. local_borg_version='1.2.3',
  256. list_arguments=list_arguments,
  257. global_arguments=global_arguments,
  258. local_path='borg',
  259. remote_path=None,
  260. ).and_return(('borg', 'list', 'repo::archive'))
  261. flexmock(module).should_receive('make_find_paths').and_return(())
  262. flexmock(module.environment).should_receive('make_environment')
  263. flexmock(module).should_receive('execute_command').with_args(
  264. ('borg', 'list', 'repo::archive'),
  265. output_log_level=module.borgmatic.logger.ANSWER,
  266. borg_local_path='borg',
  267. borg_exit_codes=None,
  268. extra_environment=None,
  269. ).once()
  270. module.list_archive(
  271. repository_path='repo',
  272. config={},
  273. local_borg_version='1.2.3',
  274. list_arguments=list_arguments,
  275. global_arguments=global_arguments,
  276. )
  277. def test_list_archive_with_archive_and_json_errors():
  278. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  279. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  280. flexmock(module.logger).answer = lambda message: None
  281. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  282. flexmock(module.feature).should_receive('available').and_return(False)
  283. with pytest.raises(ValueError):
  284. module.list_archive(
  285. repository_path='repo',
  286. config={},
  287. local_borg_version='1.2.3',
  288. list_arguments=list_arguments,
  289. global_arguments=flexmock(log_json=False),
  290. )
  291. def test_list_archive_calls_borg_with_local_path():
  292. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  293. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  294. flexmock(module.logger).answer = lambda message: None
  295. list_arguments = argparse.Namespace(
  296. archive='archive',
  297. paths=None,
  298. json=False,
  299. find_paths=None,
  300. prefix=None,
  301. match_archives=None,
  302. sort_by=None,
  303. first=None,
  304. last=None,
  305. )
  306. global_arguments = flexmock(log_json=False)
  307. flexmock(module.feature).should_receive('available').and_return(False)
  308. flexmock(module).should_receive('make_list_command').with_args(
  309. repository_path='repo',
  310. config={},
  311. local_borg_version='1.2.3',
  312. list_arguments=list_arguments,
  313. global_arguments=global_arguments,
  314. local_path='borg2',
  315. remote_path=None,
  316. ).and_return(('borg2', 'list', 'repo::archive'))
  317. flexmock(module).should_receive('make_find_paths').and_return(())
  318. flexmock(module.environment).should_receive('make_environment')
  319. flexmock(module).should_receive('execute_command').with_args(
  320. ('borg2', 'list', 'repo::archive'),
  321. output_log_level=module.borgmatic.logger.ANSWER,
  322. borg_local_path='borg2',
  323. borg_exit_codes=None,
  324. extra_environment=None,
  325. ).once()
  326. module.list_archive(
  327. repository_path='repo',
  328. config={},
  329. local_borg_version='1.2.3',
  330. list_arguments=list_arguments,
  331. global_arguments=global_arguments,
  332. local_path='borg2',
  333. )
  334. def test_list_archive_calls_borg_using_exit_codes():
  335. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  336. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  337. flexmock(module.logger).answer = lambda message: None
  338. list_arguments = argparse.Namespace(
  339. archive='archive',
  340. paths=None,
  341. json=False,
  342. find_paths=None,
  343. prefix=None,
  344. match_archives=None,
  345. sort_by=None,
  346. first=None,
  347. last=None,
  348. )
  349. global_arguments = flexmock(log_json=False)
  350. flexmock(module.feature).should_receive('available').and_return(False)
  351. borg_exit_codes = flexmock()
  352. flexmock(module).should_receive('make_list_command').with_args(
  353. repository_path='repo',
  354. config={'borg_exit_codes': borg_exit_codes},
  355. local_borg_version='1.2.3',
  356. list_arguments=list_arguments,
  357. global_arguments=global_arguments,
  358. local_path='borg',
  359. remote_path=None,
  360. ).and_return(('borg', 'list', 'repo::archive'))
  361. flexmock(module).should_receive('make_find_paths').and_return(())
  362. flexmock(module.environment).should_receive('make_environment')
  363. flexmock(module).should_receive('execute_command').with_args(
  364. ('borg', 'list', 'repo::archive'),
  365. output_log_level=module.borgmatic.logger.ANSWER,
  366. borg_local_path='borg',
  367. borg_exit_codes=borg_exit_codes,
  368. extra_environment=None,
  369. ).once()
  370. module.list_archive(
  371. repository_path='repo',
  372. config={'borg_exit_codes': borg_exit_codes},
  373. local_borg_version='1.2.3',
  374. list_arguments=list_arguments,
  375. global_arguments=global_arguments,
  376. )
  377. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  378. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  379. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  380. flexmock(module.logger).answer = lambda message: None
  381. glob_paths = ('**/*foo.txt*/**',)
  382. list_arguments = argparse.Namespace(
  383. archive=None,
  384. json=False,
  385. find_paths=['foo.txt'],
  386. prefix=None,
  387. match_archives=None,
  388. sort_by=None,
  389. first=None,
  390. last=None,
  391. )
  392. flexmock(module.feature).should_receive('available').and_return(False)
  393. flexmock(module.rlist).should_receive('make_rlist_command').and_return(('borg', 'list', 'repo'))
  394. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  395. ('borg', 'list', 'repo'),
  396. extra_environment=None,
  397. borg_local_path='borg',
  398. borg_exit_codes=None,
  399. ).and_return('archive1\narchive2').once()
  400. flexmock(module).should_receive('make_list_command').and_return(
  401. ('borg', 'list', 'repo::archive1')
  402. ).and_return(('borg', 'list', 'repo::archive2'))
  403. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  404. flexmock(module.environment).should_receive('make_environment')
  405. flexmock(module).should_receive('execute_command').with_args(
  406. ('borg', 'list', 'repo::archive1') + glob_paths,
  407. output_log_level=module.borgmatic.logger.ANSWER,
  408. borg_local_path='borg',
  409. borg_exit_codes=None,
  410. extra_environment=None,
  411. ).once()
  412. flexmock(module).should_receive('execute_command').with_args(
  413. ('borg', 'list', 'repo::archive2') + glob_paths,
  414. output_log_level=module.borgmatic.logger.ANSWER,
  415. borg_local_path='borg',
  416. borg_exit_codes=None,
  417. extra_environment=None,
  418. ).once()
  419. module.list_archive(
  420. repository_path='repo',
  421. config={},
  422. local_borg_version='1.2.3',
  423. list_arguments=list_arguments,
  424. global_arguments=flexmock(log_json=False),
  425. )
  426. def test_list_archive_calls_borg_with_archive():
  427. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  428. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  429. flexmock(module.logger).answer = lambda message: None
  430. list_arguments = argparse.Namespace(
  431. archive='archive',
  432. paths=None,
  433. json=False,
  434. find_paths=None,
  435. prefix=None,
  436. match_archives=None,
  437. sort_by=None,
  438. first=None,
  439. last=None,
  440. )
  441. global_arguments = flexmock(log_json=False)
  442. flexmock(module.feature).should_receive('available').and_return(False)
  443. flexmock(module).should_receive('make_list_command').with_args(
  444. repository_path='repo',
  445. config={},
  446. local_borg_version='1.2.3',
  447. list_arguments=list_arguments,
  448. global_arguments=global_arguments,
  449. local_path='borg',
  450. remote_path=None,
  451. ).and_return(('borg', 'list', 'repo::archive'))
  452. flexmock(module).should_receive('make_find_paths').and_return(())
  453. flexmock(module.environment).should_receive('make_environment')
  454. flexmock(module).should_receive('execute_command').with_args(
  455. ('borg', 'list', 'repo::archive'),
  456. output_log_level=module.borgmatic.logger.ANSWER,
  457. borg_local_path='borg',
  458. borg_exit_codes=None,
  459. extra_environment=None,
  460. ).once()
  461. module.list_archive(
  462. repository_path='repo',
  463. config={},
  464. local_borg_version='1.2.3',
  465. list_arguments=list_arguments,
  466. global_arguments=global_arguments,
  467. )
  468. def test_list_archive_without_archive_delegates_to_list_repository():
  469. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  470. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  471. flexmock(module.logger).answer = lambda message: None
  472. list_arguments = argparse.Namespace(
  473. archive=None,
  474. short=None,
  475. format=None,
  476. json=None,
  477. prefix=None,
  478. match_archives=None,
  479. sort_by=None,
  480. first=None,
  481. last=None,
  482. find_paths=None,
  483. )
  484. flexmock(module.feature).should_receive('available').and_return(False)
  485. flexmock(module.rlist).should_receive('list_repository')
  486. flexmock(module.environment).should_receive('make_environment').never()
  487. flexmock(module).should_receive('execute_command').never()
  488. module.list_archive(
  489. repository_path='repo',
  490. config={},
  491. local_borg_version='1.2.3',
  492. list_arguments=list_arguments,
  493. global_arguments=flexmock(log_json=False),
  494. )
  495. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  496. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  497. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  498. flexmock(module.logger).answer = lambda message: None
  499. list_arguments = argparse.Namespace(
  500. archive=None,
  501. short=None,
  502. format=None,
  503. json=None,
  504. prefix=None,
  505. match_archives=None,
  506. sort_by=None,
  507. first=None,
  508. last=None,
  509. find_paths=None,
  510. )
  511. flexmock(module.feature).should_receive('available').and_return(True)
  512. flexmock(module.rlist).should_receive('list_repository')
  513. flexmock(module.environment).should_receive('make_environment').never()
  514. flexmock(module).should_receive('execute_command').never()
  515. module.list_archive(
  516. repository_path='repo',
  517. config={},
  518. local_borg_version='1.2.3',
  519. list_arguments=list_arguments,
  520. global_arguments=flexmock(log_json=False),
  521. )
  522. @pytest.mark.parametrize(
  523. 'archive_filter_flag',
  524. (
  525. 'prefix',
  526. 'match_archives',
  527. 'sort_by',
  528. 'first',
  529. 'last',
  530. ),
  531. )
  532. def test_list_archive_with_archive_ignores_archive_filter_flag(
  533. archive_filter_flag,
  534. ):
  535. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  536. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  537. flexmock(module.logger).answer = lambda message: None
  538. global_arguments = flexmock(log_json=False)
  539. default_filter_flags = {
  540. 'prefix': None,
  541. 'match_archives': None,
  542. 'sort_by': None,
  543. 'first': None,
  544. 'last': None,
  545. }
  546. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  547. flexmock(module.feature).should_receive('available').with_args(
  548. module.feature.Feature.RLIST, '1.2.3'
  549. ).and_return(False)
  550. flexmock(module).should_receive('make_list_command').with_args(
  551. repository_path='repo',
  552. config={},
  553. local_borg_version='1.2.3',
  554. list_arguments=argparse.Namespace(
  555. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  556. ),
  557. global_arguments=global_arguments,
  558. local_path='borg',
  559. remote_path=None,
  560. ).and_return(('borg', 'list', 'repo::archive'))
  561. flexmock(module).should_receive('make_find_paths').and_return(())
  562. flexmock(module.environment).should_receive('make_environment')
  563. flexmock(module).should_receive('execute_command').with_args(
  564. ('borg', 'list', 'repo::archive'),
  565. output_log_level=module.borgmatic.logger.ANSWER,
  566. borg_local_path='borg',
  567. borg_exit_codes=None,
  568. extra_environment=None,
  569. ).once()
  570. module.list_archive(
  571. repository_path='repo',
  572. config={},
  573. local_borg_version='1.2.3',
  574. list_arguments=argparse.Namespace(
  575. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  576. ),
  577. global_arguments=global_arguments,
  578. )
  579. @pytest.mark.parametrize(
  580. 'archive_filter_flag',
  581. (
  582. 'prefix',
  583. 'match_archives',
  584. 'sort_by',
  585. 'first',
  586. 'last',
  587. ),
  588. )
  589. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_rlist(
  590. archive_filter_flag,
  591. ):
  592. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  593. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  594. flexmock(module.logger).answer = lambda message: None
  595. default_filter_flags = {
  596. 'prefix': None,
  597. 'match_archives': None,
  598. 'sort_by': None,
  599. 'first': None,
  600. 'last': None,
  601. }
  602. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  603. glob_paths = ('**/*foo.txt*/**',)
  604. global_arguments = flexmock(log_json=False)
  605. flexmock(module.feature).should_receive('available').and_return(True)
  606. flexmock(module.rlist).should_receive('make_rlist_command').with_args(
  607. repository_path='repo',
  608. config={},
  609. local_borg_version='1.2.3',
  610. rlist_arguments=argparse.Namespace(
  611. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  612. ),
  613. global_arguments=global_arguments,
  614. local_path='borg',
  615. remote_path=None,
  616. ).and_return(('borg', 'rlist', '--repo', 'repo'))
  617. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  618. ('borg', 'rlist', '--repo', 'repo'),
  619. extra_environment=None,
  620. borg_local_path='borg',
  621. borg_exit_codes=None,
  622. ).and_return('archive1\narchive2').once()
  623. flexmock(module).should_receive('make_list_command').with_args(
  624. repository_path='repo',
  625. config={},
  626. local_borg_version='1.2.3',
  627. list_arguments=argparse.Namespace(
  628. repository='repo',
  629. archive='archive1',
  630. paths=None,
  631. short=True,
  632. format=None,
  633. json=None,
  634. find_paths=['foo.txt'],
  635. **default_filter_flags,
  636. ),
  637. global_arguments=global_arguments,
  638. local_path='borg',
  639. remote_path=None,
  640. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  641. flexmock(module).should_receive('make_list_command').with_args(
  642. repository_path='repo',
  643. config={},
  644. local_borg_version='1.2.3',
  645. list_arguments=argparse.Namespace(
  646. repository='repo',
  647. archive='archive2',
  648. paths=None,
  649. short=True,
  650. format=None,
  651. json=None,
  652. find_paths=['foo.txt'],
  653. **default_filter_flags,
  654. ),
  655. global_arguments=global_arguments,
  656. local_path='borg',
  657. remote_path=None,
  658. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  659. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  660. flexmock(module.environment).should_receive('make_environment')
  661. flexmock(module).should_receive('execute_command').with_args(
  662. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  663. output_log_level=module.borgmatic.logger.ANSWER,
  664. borg_local_path='borg',
  665. borg_exit_codes=None,
  666. extra_environment=None,
  667. ).once()
  668. flexmock(module).should_receive('execute_command').with_args(
  669. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  670. output_log_level=module.borgmatic.logger.ANSWER,
  671. borg_local_path='borg',
  672. borg_exit_codes=None,
  673. extra_environment=None,
  674. ).once()
  675. module.list_archive(
  676. repository_path='repo',
  677. config={},
  678. local_borg_version='1.2.3',
  679. list_arguments=argparse.Namespace(
  680. repository='repo',
  681. archive=None,
  682. paths=None,
  683. short=True,
  684. format=None,
  685. json=None,
  686. find_paths=['foo.txt'],
  687. **altered_filter_flags,
  688. ),
  689. global_arguments=global_arguments,
  690. )