test_list.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. storage_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. ).and_return('archive1\narchive2').once()
  353. flexmock(module).should_receive('make_list_command').and_return(
  354. ('borg', 'list', 'repo::archive1')
  355. ).and_return(('borg', 'list', 'repo::archive2'))
  356. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  357. flexmock(module.environment).should_receive('make_environment')
  358. flexmock(module).should_receive('execute_command').with_args(
  359. ('borg', 'list', 'repo::archive1') + glob_paths,
  360. output_log_level=module.borgmatic.logger.ANSWER,
  361. borg_local_path='borg',
  362. extra_environment=None,
  363. ).once()
  364. flexmock(module).should_receive('execute_command').with_args(
  365. ('borg', 'list', 'repo::archive2') + glob_paths,
  366. output_log_level=module.borgmatic.logger.ANSWER,
  367. borg_local_path='borg',
  368. extra_environment=None,
  369. ).once()
  370. module.list_archive(
  371. repository_path='repo',
  372. storage_config={},
  373. local_borg_version='1.2.3',
  374. list_arguments=list_arguments,
  375. global_arguments=flexmock(log_json=False),
  376. )
  377. def test_list_archive_calls_borg_with_archive():
  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. list_arguments = argparse.Namespace(
  382. archive='archive',
  383. paths=None,
  384. json=False,
  385. find_paths=None,
  386. prefix=None,
  387. match_archives=None,
  388. sort_by=None,
  389. first=None,
  390. last=None,
  391. )
  392. global_arguments = flexmock(log_json=False)
  393. flexmock(module.feature).should_receive('available').and_return(False)
  394. flexmock(module).should_receive('make_list_command').with_args(
  395. repository_path='repo',
  396. storage_config={},
  397. local_borg_version='1.2.3',
  398. list_arguments=list_arguments,
  399. global_arguments=global_arguments,
  400. local_path='borg',
  401. remote_path=None,
  402. ).and_return(('borg', 'list', 'repo::archive'))
  403. flexmock(module).should_receive('make_find_paths').and_return(())
  404. flexmock(module.environment).should_receive('make_environment')
  405. flexmock(module).should_receive('execute_command').with_args(
  406. ('borg', 'list', 'repo::archive'),
  407. output_log_level=module.borgmatic.logger.ANSWER,
  408. borg_local_path='borg',
  409. extra_environment=None,
  410. ).once()
  411. module.list_archive(
  412. repository_path='repo',
  413. storage_config={},
  414. local_borg_version='1.2.3',
  415. list_arguments=list_arguments,
  416. global_arguments=global_arguments,
  417. )
  418. def test_list_archive_without_archive_delegates_to_list_repository():
  419. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  420. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  421. flexmock(module.logger).answer = lambda message: None
  422. list_arguments = argparse.Namespace(
  423. archive=None,
  424. short=None,
  425. format=None,
  426. json=None,
  427. prefix=None,
  428. match_archives=None,
  429. sort_by=None,
  430. first=None,
  431. last=None,
  432. find_paths=None,
  433. )
  434. flexmock(module.feature).should_receive('available').and_return(False)
  435. flexmock(module.rlist).should_receive('list_repository')
  436. flexmock(module.environment).should_receive('make_environment').never()
  437. flexmock(module).should_receive('execute_command').never()
  438. module.list_archive(
  439. repository_path='repo',
  440. storage_config={},
  441. local_borg_version='1.2.3',
  442. list_arguments=list_arguments,
  443. global_arguments=flexmock(log_json=False),
  444. )
  445. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  446. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  447. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  448. flexmock(module.logger).answer = lambda message: None
  449. list_arguments = argparse.Namespace(
  450. archive=None,
  451. short=None,
  452. format=None,
  453. json=None,
  454. prefix=None,
  455. match_archives=None,
  456. sort_by=None,
  457. first=None,
  458. last=None,
  459. find_paths=None,
  460. )
  461. flexmock(module.feature).should_receive('available').and_return(True)
  462. flexmock(module.rlist).should_receive('list_repository')
  463. flexmock(module.environment).should_receive('make_environment').never()
  464. flexmock(module).should_receive('execute_command').never()
  465. module.list_archive(
  466. repository_path='repo',
  467. storage_config={},
  468. local_borg_version='1.2.3',
  469. list_arguments=list_arguments,
  470. global_arguments=flexmock(log_json=False),
  471. )
  472. @pytest.mark.parametrize(
  473. 'archive_filter_flag',
  474. (
  475. 'prefix',
  476. 'match_archives',
  477. 'sort_by',
  478. 'first',
  479. 'last',
  480. ),
  481. )
  482. def test_list_archive_with_archive_ignores_archive_filter_flag(
  483. archive_filter_flag,
  484. ):
  485. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  486. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  487. flexmock(module.logger).answer = lambda message: None
  488. global_arguments = flexmock(log_json=False)
  489. default_filter_flags = {
  490. 'prefix': None,
  491. 'match_archives': None,
  492. 'sort_by': None,
  493. 'first': None,
  494. 'last': None,
  495. }
  496. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  497. flexmock(module.feature).should_receive('available').with_args(
  498. module.feature.Feature.RLIST, '1.2.3'
  499. ).and_return(False)
  500. flexmock(module).should_receive('make_list_command').with_args(
  501. repository_path='repo',
  502. storage_config={},
  503. local_borg_version='1.2.3',
  504. list_arguments=argparse.Namespace(
  505. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  506. ),
  507. global_arguments=global_arguments,
  508. local_path='borg',
  509. remote_path=None,
  510. ).and_return(('borg', 'list', 'repo::archive'))
  511. flexmock(module).should_receive('make_find_paths').and_return(())
  512. flexmock(module.environment).should_receive('make_environment')
  513. flexmock(module).should_receive('execute_command').with_args(
  514. ('borg', 'list', 'repo::archive'),
  515. output_log_level=module.borgmatic.logger.ANSWER,
  516. borg_local_path='borg',
  517. extra_environment=None,
  518. ).once()
  519. module.list_archive(
  520. repository_path='repo',
  521. storage_config={},
  522. local_borg_version='1.2.3',
  523. list_arguments=argparse.Namespace(
  524. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  525. ),
  526. global_arguments=global_arguments,
  527. )
  528. @pytest.mark.parametrize(
  529. 'archive_filter_flag',
  530. (
  531. 'prefix',
  532. 'match_archives',
  533. 'sort_by',
  534. 'first',
  535. 'last',
  536. ),
  537. )
  538. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_rlist(
  539. archive_filter_flag,
  540. ):
  541. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  542. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  543. flexmock(module.logger).answer = lambda message: None
  544. default_filter_flags = {
  545. 'prefix': None,
  546. 'match_archives': None,
  547. 'sort_by': None,
  548. 'first': None,
  549. 'last': None,
  550. }
  551. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  552. glob_paths = ('**/*foo.txt*/**',)
  553. global_arguments = flexmock(log_json=False)
  554. flexmock(module.feature).should_receive('available').and_return(True)
  555. flexmock(module.rlist).should_receive('make_rlist_command').with_args(
  556. repository_path='repo',
  557. storage_config={},
  558. local_borg_version='1.2.3',
  559. rlist_arguments=argparse.Namespace(
  560. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  561. ),
  562. global_arguments=global_arguments,
  563. local_path='borg',
  564. remote_path=None,
  565. ).and_return(('borg', 'rlist', '--repo', 'repo'))
  566. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  567. ('borg', 'rlist', '--repo', 'repo'),
  568. extra_environment=None,
  569. ).and_return('archive1\narchive2').once()
  570. flexmock(module).should_receive('make_list_command').with_args(
  571. repository_path='repo',
  572. storage_config={},
  573. local_borg_version='1.2.3',
  574. list_arguments=argparse.Namespace(
  575. repository='repo',
  576. archive='archive1',
  577. paths=None,
  578. short=True,
  579. format=None,
  580. json=None,
  581. find_paths=['foo.txt'],
  582. **default_filter_flags,
  583. ),
  584. global_arguments=global_arguments,
  585. local_path='borg',
  586. remote_path=None,
  587. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  588. flexmock(module).should_receive('make_list_command').with_args(
  589. repository_path='repo',
  590. storage_config={},
  591. local_borg_version='1.2.3',
  592. list_arguments=argparse.Namespace(
  593. repository='repo',
  594. archive='archive2',
  595. paths=None,
  596. short=True,
  597. format=None,
  598. json=None,
  599. find_paths=['foo.txt'],
  600. **default_filter_flags,
  601. ),
  602. global_arguments=global_arguments,
  603. local_path='borg',
  604. remote_path=None,
  605. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  606. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  607. flexmock(module.environment).should_receive('make_environment')
  608. flexmock(module).should_receive('execute_command').with_args(
  609. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  610. output_log_level=module.borgmatic.logger.ANSWER,
  611. borg_local_path='borg',
  612. extra_environment=None,
  613. ).once()
  614. flexmock(module).should_receive('execute_command').with_args(
  615. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  616. output_log_level=module.borgmatic.logger.ANSWER,
  617. borg_local_path='borg',
  618. extra_environment=None,
  619. ).once()
  620. module.list_archive(
  621. repository_path='repo',
  622. storage_config={},
  623. local_borg_version='1.2.3',
  624. list_arguments=argparse.Namespace(
  625. repository='repo',
  626. archive=None,
  627. paths=None,
  628. short=True,
  629. format=None,
  630. json=None,
  631. find_paths=['foo.txt'],
  632. **altered_filter_flags,
  633. ),
  634. global_arguments=global_arguments,
  635. )