test_list.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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=flexmock(),
  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. extra_environment=None,
  268. ).once()
  269. module.list_archive(
  270. repository_path='repo',
  271. config={},
  272. local_borg_version='1.2.3',
  273. list_arguments=list_arguments,
  274. global_arguments=global_arguments,
  275. )
  276. def test_list_archive_with_archive_and_json_errors():
  277. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  278. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  279. flexmock(module.logger).answer = lambda message: None
  280. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  281. flexmock(module.feature).should_receive('available').and_return(False)
  282. with pytest.raises(ValueError):
  283. module.list_archive(
  284. repository_path='repo',
  285. config={},
  286. local_borg_version='1.2.3',
  287. list_arguments=list_arguments,
  288. global_arguments=flexmock(log_json=False),
  289. )
  290. def test_list_archive_calls_borg_with_local_path():
  291. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  292. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  293. flexmock(module.logger).answer = lambda message: None
  294. list_arguments = argparse.Namespace(
  295. archive='archive',
  296. paths=None,
  297. json=False,
  298. find_paths=None,
  299. prefix=None,
  300. match_archives=None,
  301. sort_by=None,
  302. first=None,
  303. last=None,
  304. )
  305. global_arguments = flexmock(log_json=False)
  306. flexmock(module.feature).should_receive('available').and_return(False)
  307. flexmock(module).should_receive('make_list_command').with_args(
  308. repository_path='repo',
  309. config={},
  310. local_borg_version='1.2.3',
  311. list_arguments=list_arguments,
  312. global_arguments=global_arguments,
  313. local_path='borg2',
  314. remote_path=None,
  315. ).and_return(('borg2', 'list', 'repo::archive'))
  316. flexmock(module).should_receive('make_find_paths').and_return(())
  317. flexmock(module.environment).should_receive('make_environment')
  318. flexmock(module).should_receive('execute_command').with_args(
  319. ('borg2', 'list', 'repo::archive'),
  320. output_log_level=module.borgmatic.logger.ANSWER,
  321. borg_local_path='borg2',
  322. extra_environment=None,
  323. ).once()
  324. module.list_archive(
  325. repository_path='repo',
  326. config={},
  327. local_borg_version='1.2.3',
  328. list_arguments=list_arguments,
  329. global_arguments=global_arguments,
  330. local_path='borg2',
  331. )
  332. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  333. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  334. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  335. flexmock(module.logger).answer = lambda message: None
  336. glob_paths = ('**/*foo.txt*/**',)
  337. list_arguments = argparse.Namespace(
  338. archive=None,
  339. json=False,
  340. find_paths=['foo.txt'],
  341. prefix=None,
  342. match_archives=None,
  343. sort_by=None,
  344. first=None,
  345. last=None,
  346. )
  347. flexmock(module.feature).should_receive('available').and_return(False)
  348. flexmock(module.rlist).should_receive('make_rlist_command').and_return(('borg', 'list', 'repo'))
  349. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  350. ('borg', 'list', 'repo'),
  351. extra_environment=None,
  352. borg_local_path='borg',
  353. ).and_return('archive1\narchive2').once()
  354. flexmock(module).should_receive('make_list_command').and_return(
  355. ('borg', 'list', 'repo::archive1')
  356. ).and_return(('borg', 'list', 'repo::archive2'))
  357. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  358. flexmock(module.environment).should_receive('make_environment')
  359. flexmock(module).should_receive('execute_command').with_args(
  360. ('borg', 'list', 'repo::archive1') + glob_paths,
  361. output_log_level=module.borgmatic.logger.ANSWER,
  362. borg_local_path='borg',
  363. extra_environment=None,
  364. ).once()
  365. flexmock(module).should_receive('execute_command').with_args(
  366. ('borg', 'list', 'repo::archive2') + glob_paths,
  367. output_log_level=module.borgmatic.logger.ANSWER,
  368. borg_local_path='borg',
  369. extra_environment=None,
  370. ).once()
  371. module.list_archive(
  372. repository_path='repo',
  373. config={},
  374. local_borg_version='1.2.3',
  375. list_arguments=list_arguments,
  376. global_arguments=flexmock(log_json=False),
  377. )
  378. def test_list_archive_calls_borg_with_archive():
  379. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  380. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  381. flexmock(module.logger).answer = lambda message: None
  382. list_arguments = argparse.Namespace(
  383. archive='archive',
  384. paths=None,
  385. json=False,
  386. find_paths=None,
  387. prefix=None,
  388. match_archives=None,
  389. sort_by=None,
  390. first=None,
  391. last=None,
  392. )
  393. global_arguments = flexmock(log_json=False)
  394. flexmock(module.feature).should_receive('available').and_return(False)
  395. flexmock(module).should_receive('make_list_command').with_args(
  396. repository_path='repo',
  397. config={},
  398. local_borg_version='1.2.3',
  399. list_arguments=list_arguments,
  400. global_arguments=global_arguments,
  401. local_path='borg',
  402. remote_path=None,
  403. ).and_return(('borg', 'list', 'repo::archive'))
  404. flexmock(module).should_receive('make_find_paths').and_return(())
  405. flexmock(module.environment).should_receive('make_environment')
  406. flexmock(module).should_receive('execute_command').with_args(
  407. ('borg', 'list', 'repo::archive'),
  408. output_log_level=module.borgmatic.logger.ANSWER,
  409. borg_local_path='borg',
  410. extra_environment=None,
  411. ).once()
  412. module.list_archive(
  413. repository_path='repo',
  414. config={},
  415. local_borg_version='1.2.3',
  416. list_arguments=list_arguments,
  417. global_arguments=global_arguments,
  418. )
  419. def test_list_archive_without_archive_delegates_to_list_repository():
  420. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  421. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  422. flexmock(module.logger).answer = lambda message: None
  423. list_arguments = argparse.Namespace(
  424. archive=None,
  425. short=None,
  426. format=None,
  427. json=None,
  428. prefix=None,
  429. match_archives=None,
  430. sort_by=None,
  431. first=None,
  432. last=None,
  433. find_paths=None,
  434. )
  435. flexmock(module.feature).should_receive('available').and_return(False)
  436. flexmock(module.rlist).should_receive('list_repository')
  437. flexmock(module.environment).should_receive('make_environment').never()
  438. flexmock(module).should_receive('execute_command').never()
  439. module.list_archive(
  440. repository_path='repo',
  441. config={},
  442. local_borg_version='1.2.3',
  443. list_arguments=list_arguments,
  444. global_arguments=flexmock(log_json=False),
  445. )
  446. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  447. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  448. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  449. flexmock(module.logger).answer = lambda message: None
  450. list_arguments = argparse.Namespace(
  451. archive=None,
  452. short=None,
  453. format=None,
  454. json=None,
  455. prefix=None,
  456. match_archives=None,
  457. sort_by=None,
  458. first=None,
  459. last=None,
  460. find_paths=None,
  461. )
  462. flexmock(module.feature).should_receive('available').and_return(True)
  463. flexmock(module.rlist).should_receive('list_repository')
  464. flexmock(module.environment).should_receive('make_environment').never()
  465. flexmock(module).should_receive('execute_command').never()
  466. module.list_archive(
  467. repository_path='repo',
  468. config={},
  469. local_borg_version='1.2.3',
  470. list_arguments=list_arguments,
  471. global_arguments=flexmock(log_json=False),
  472. )
  473. @pytest.mark.parametrize(
  474. 'archive_filter_flag',
  475. (
  476. 'prefix',
  477. 'match_archives',
  478. 'sort_by',
  479. 'first',
  480. 'last',
  481. ),
  482. )
  483. def test_list_archive_with_archive_ignores_archive_filter_flag(
  484. archive_filter_flag,
  485. ):
  486. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  487. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  488. flexmock(module.logger).answer = lambda message: None
  489. global_arguments = flexmock(log_json=False)
  490. default_filter_flags = {
  491. 'prefix': None,
  492. 'match_archives': None,
  493. 'sort_by': None,
  494. 'first': None,
  495. 'last': None,
  496. }
  497. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  498. flexmock(module.feature).should_receive('available').with_args(
  499. module.feature.Feature.RLIST, '1.2.3'
  500. ).and_return(False)
  501. flexmock(module).should_receive('make_list_command').with_args(
  502. repository_path='repo',
  503. config={},
  504. local_borg_version='1.2.3',
  505. list_arguments=argparse.Namespace(
  506. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  507. ),
  508. global_arguments=global_arguments,
  509. local_path='borg',
  510. remote_path=None,
  511. ).and_return(('borg', 'list', 'repo::archive'))
  512. flexmock(module).should_receive('make_find_paths').and_return(())
  513. flexmock(module.environment).should_receive('make_environment')
  514. flexmock(module).should_receive('execute_command').with_args(
  515. ('borg', 'list', 'repo::archive'),
  516. output_log_level=module.borgmatic.logger.ANSWER,
  517. borg_local_path='borg',
  518. extra_environment=None,
  519. ).once()
  520. module.list_archive(
  521. repository_path='repo',
  522. config={},
  523. local_borg_version='1.2.3',
  524. list_arguments=argparse.Namespace(
  525. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  526. ),
  527. global_arguments=global_arguments,
  528. )
  529. @pytest.mark.parametrize(
  530. 'archive_filter_flag',
  531. (
  532. 'prefix',
  533. 'match_archives',
  534. 'sort_by',
  535. 'first',
  536. 'last',
  537. ),
  538. )
  539. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_rlist(
  540. archive_filter_flag,
  541. ):
  542. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  543. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  544. flexmock(module.logger).answer = lambda message: None
  545. default_filter_flags = {
  546. 'prefix': None,
  547. 'match_archives': None,
  548. 'sort_by': None,
  549. 'first': None,
  550. 'last': None,
  551. }
  552. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  553. glob_paths = ('**/*foo.txt*/**',)
  554. global_arguments = flexmock(log_json=False)
  555. flexmock(module.feature).should_receive('available').and_return(True)
  556. flexmock(module.rlist).should_receive('make_rlist_command').with_args(
  557. repository_path='repo',
  558. config={},
  559. local_borg_version='1.2.3',
  560. rlist_arguments=argparse.Namespace(
  561. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  562. ),
  563. global_arguments=global_arguments,
  564. local_path='borg',
  565. remote_path=None,
  566. ).and_return(('borg', 'rlist', '--repo', 'repo'))
  567. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  568. ('borg', 'rlist', '--repo', 'repo'),
  569. extra_environment=None,
  570. borg_local_path='borg',
  571. ).and_return('archive1\narchive2').once()
  572. flexmock(module).should_receive('make_list_command').with_args(
  573. repository_path='repo',
  574. config={},
  575. local_borg_version='1.2.3',
  576. list_arguments=argparse.Namespace(
  577. repository='repo',
  578. archive='archive1',
  579. paths=None,
  580. short=True,
  581. format=None,
  582. json=None,
  583. find_paths=['foo.txt'],
  584. **default_filter_flags,
  585. ),
  586. global_arguments=global_arguments,
  587. local_path='borg',
  588. remote_path=None,
  589. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  590. flexmock(module).should_receive('make_list_command').with_args(
  591. repository_path='repo',
  592. config={},
  593. local_borg_version='1.2.3',
  594. list_arguments=argparse.Namespace(
  595. repository='repo',
  596. archive='archive2',
  597. paths=None,
  598. short=True,
  599. format=None,
  600. json=None,
  601. find_paths=['foo.txt'],
  602. **default_filter_flags,
  603. ),
  604. global_arguments=global_arguments,
  605. local_path='borg',
  606. remote_path=None,
  607. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  608. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  609. flexmock(module.environment).should_receive('make_environment')
  610. flexmock(module).should_receive('execute_command').with_args(
  611. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  612. output_log_level=module.borgmatic.logger.ANSWER,
  613. borg_local_path='borg',
  614. extra_environment=None,
  615. ).once()
  616. flexmock(module).should_receive('execute_command').with_args(
  617. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  618. output_log_level=module.borgmatic.logger.ANSWER,
  619. borg_local_path='borg',
  620. extra_environment=None,
  621. ).once()
  622. module.list_archive(
  623. repository_path='repo',
  624. config={},
  625. local_borg_version='1.2.3',
  626. list_arguments=argparse.Namespace(
  627. repository='repo',
  628. archive=None,
  629. paths=None,
  630. short=True,
  631. format=None,
  632. json=None,
  633. find_paths=['foo.txt'],
  634. **altered_filter_flags,
  635. ),
  636. global_arguments=global_arguments,
  637. )