test_list.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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.repo_list).should_receive('make_repo_list_command').and_return(
  394. ('borg', 'list', 'repo')
  395. )
  396. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  397. ('borg', 'list', 'repo'),
  398. extra_environment=None,
  399. borg_local_path='borg',
  400. borg_exit_codes=None,
  401. ).and_return('archive1\narchive2').once()
  402. flexmock(module).should_receive('make_list_command').and_return(
  403. ('borg', 'list', 'repo::archive1')
  404. ).and_return(('borg', 'list', 'repo::archive2'))
  405. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  406. flexmock(module.environment).should_receive('make_environment')
  407. flexmock(module).should_receive('execute_command').with_args(
  408. ('borg', 'list', 'repo::archive1') + glob_paths,
  409. output_log_level=module.borgmatic.logger.ANSWER,
  410. borg_local_path='borg',
  411. borg_exit_codes=None,
  412. extra_environment=None,
  413. ).once()
  414. flexmock(module).should_receive('execute_command').with_args(
  415. ('borg', 'list', 'repo::archive2') + glob_paths,
  416. output_log_level=module.borgmatic.logger.ANSWER,
  417. borg_local_path='borg',
  418. borg_exit_codes=None,
  419. extra_environment=None,
  420. ).once()
  421. module.list_archive(
  422. repository_path='repo',
  423. config={},
  424. local_borg_version='1.2.3',
  425. list_arguments=list_arguments,
  426. global_arguments=flexmock(log_json=False),
  427. )
  428. def test_list_archive_calls_borg_with_archive():
  429. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  430. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  431. flexmock(module.logger).answer = lambda message: None
  432. list_arguments = argparse.Namespace(
  433. archive='archive',
  434. paths=None,
  435. json=False,
  436. find_paths=None,
  437. prefix=None,
  438. match_archives=None,
  439. sort_by=None,
  440. first=None,
  441. last=None,
  442. )
  443. global_arguments = flexmock(log_json=False)
  444. flexmock(module.feature).should_receive('available').and_return(False)
  445. flexmock(module).should_receive('make_list_command').with_args(
  446. repository_path='repo',
  447. config={},
  448. local_borg_version='1.2.3',
  449. list_arguments=list_arguments,
  450. global_arguments=global_arguments,
  451. local_path='borg',
  452. remote_path=None,
  453. ).and_return(('borg', 'list', 'repo::archive'))
  454. flexmock(module).should_receive('make_find_paths').and_return(())
  455. flexmock(module.environment).should_receive('make_environment')
  456. flexmock(module).should_receive('execute_command').with_args(
  457. ('borg', 'list', 'repo::archive'),
  458. output_log_level=module.borgmatic.logger.ANSWER,
  459. borg_local_path='borg',
  460. borg_exit_codes=None,
  461. extra_environment=None,
  462. ).once()
  463. module.list_archive(
  464. repository_path='repo',
  465. config={},
  466. local_borg_version='1.2.3',
  467. list_arguments=list_arguments,
  468. global_arguments=global_arguments,
  469. )
  470. def test_list_archive_without_archive_delegates_to_list_repository():
  471. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  472. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  473. flexmock(module.logger).answer = lambda message: None
  474. list_arguments = argparse.Namespace(
  475. archive=None,
  476. short=None,
  477. format=None,
  478. json=None,
  479. prefix=None,
  480. match_archives=None,
  481. sort_by=None,
  482. first=None,
  483. last=None,
  484. find_paths=None,
  485. )
  486. flexmock(module.feature).should_receive('available').and_return(False)
  487. flexmock(module.repo_list).should_receive('list_repository')
  488. flexmock(module.environment).should_receive('make_environment').never()
  489. flexmock(module).should_receive('execute_command').never()
  490. module.list_archive(
  491. repository_path='repo',
  492. config={},
  493. local_borg_version='1.2.3',
  494. list_arguments=list_arguments,
  495. global_arguments=flexmock(log_json=False),
  496. )
  497. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  498. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  499. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  500. flexmock(module.logger).answer = lambda message: None
  501. list_arguments = argparse.Namespace(
  502. archive=None,
  503. short=None,
  504. format=None,
  505. json=None,
  506. prefix=None,
  507. match_archives=None,
  508. sort_by=None,
  509. first=None,
  510. last=None,
  511. find_paths=None,
  512. )
  513. flexmock(module.feature).should_receive('available').and_return(True)
  514. flexmock(module.repo_list).should_receive('list_repository')
  515. flexmock(module.environment).should_receive('make_environment').never()
  516. flexmock(module).should_receive('execute_command').never()
  517. module.list_archive(
  518. repository_path='repo',
  519. config={},
  520. local_borg_version='1.2.3',
  521. list_arguments=list_arguments,
  522. global_arguments=flexmock(log_json=False),
  523. )
  524. @pytest.mark.parametrize(
  525. 'archive_filter_flag',
  526. (
  527. 'prefix',
  528. 'match_archives',
  529. 'sort_by',
  530. 'first',
  531. 'last',
  532. ),
  533. )
  534. def test_list_archive_with_archive_ignores_archive_filter_flag(
  535. archive_filter_flag,
  536. ):
  537. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  538. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  539. flexmock(module.logger).answer = lambda message: None
  540. global_arguments = flexmock(log_json=False)
  541. default_filter_flags = {
  542. 'prefix': None,
  543. 'match_archives': None,
  544. 'sort_by': None,
  545. 'first': None,
  546. 'last': None,
  547. }
  548. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  549. flexmock(module.feature).should_receive('available').with_args(
  550. module.feature.Feature.REPO_LIST, '1.2.3'
  551. ).and_return(False)
  552. flexmock(module).should_receive('make_list_command').with_args(
  553. repository_path='repo',
  554. config={},
  555. local_borg_version='1.2.3',
  556. list_arguments=argparse.Namespace(
  557. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  558. ),
  559. global_arguments=global_arguments,
  560. local_path='borg',
  561. remote_path=None,
  562. ).and_return(('borg', 'list', 'repo::archive'))
  563. flexmock(module).should_receive('make_find_paths').and_return(())
  564. flexmock(module.environment).should_receive('make_environment')
  565. flexmock(module).should_receive('execute_command').with_args(
  566. ('borg', 'list', 'repo::archive'),
  567. output_log_level=module.borgmatic.logger.ANSWER,
  568. borg_local_path='borg',
  569. borg_exit_codes=None,
  570. extra_environment=None,
  571. ).once()
  572. module.list_archive(
  573. repository_path='repo',
  574. config={},
  575. local_borg_version='1.2.3',
  576. list_arguments=argparse.Namespace(
  577. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  578. ),
  579. global_arguments=global_arguments,
  580. )
  581. @pytest.mark.parametrize(
  582. 'archive_filter_flag',
  583. (
  584. 'prefix',
  585. 'match_archives',
  586. 'sort_by',
  587. 'first',
  588. 'last',
  589. ),
  590. )
  591. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_repo_list(
  592. archive_filter_flag,
  593. ):
  594. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  595. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  596. flexmock(module.logger).answer = lambda message: None
  597. default_filter_flags = {
  598. 'prefix': None,
  599. 'match_archives': None,
  600. 'sort_by': None,
  601. 'first': None,
  602. 'last': None,
  603. }
  604. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  605. glob_paths = ('**/*foo.txt*/**',)
  606. global_arguments = flexmock(log_json=False)
  607. flexmock(module.feature).should_receive('available').and_return(True)
  608. flexmock(module.repo_list).should_receive('make_repo_list_command').with_args(
  609. repository_path='repo',
  610. config={},
  611. local_borg_version='1.2.3',
  612. repo_list_arguments=argparse.Namespace(
  613. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  614. ),
  615. global_arguments=global_arguments,
  616. local_path='borg',
  617. remote_path=None,
  618. ).and_return(('borg', 'repo-list', '--repo', 'repo'))
  619. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  620. ('borg', 'repo-list', '--repo', 'repo'),
  621. extra_environment=None,
  622. borg_local_path='borg',
  623. borg_exit_codes=None,
  624. ).and_return('archive1\narchive2').once()
  625. flexmock(module).should_receive('make_list_command').with_args(
  626. repository_path='repo',
  627. config={},
  628. local_borg_version='1.2.3',
  629. list_arguments=argparse.Namespace(
  630. repository='repo',
  631. archive='archive1',
  632. paths=None,
  633. short=True,
  634. format=None,
  635. json=None,
  636. find_paths=['foo.txt'],
  637. **default_filter_flags,
  638. ),
  639. global_arguments=global_arguments,
  640. local_path='borg',
  641. remote_path=None,
  642. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  643. flexmock(module).should_receive('make_list_command').with_args(
  644. repository_path='repo',
  645. config={},
  646. local_borg_version='1.2.3',
  647. list_arguments=argparse.Namespace(
  648. repository='repo',
  649. archive='archive2',
  650. paths=None,
  651. short=True,
  652. format=None,
  653. json=None,
  654. find_paths=['foo.txt'],
  655. **default_filter_flags,
  656. ),
  657. global_arguments=global_arguments,
  658. local_path='borg',
  659. remote_path=None,
  660. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  661. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  662. flexmock(module.environment).should_receive('make_environment')
  663. flexmock(module).should_receive('execute_command').with_args(
  664. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  665. output_log_level=module.borgmatic.logger.ANSWER,
  666. borg_local_path='borg',
  667. borg_exit_codes=None,
  668. extra_environment=None,
  669. ).once()
  670. flexmock(module).should_receive('execute_command').with_args(
  671. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  672. output_log_level=module.borgmatic.logger.ANSWER,
  673. borg_local_path='borg',
  674. borg_exit_codes=None,
  675. extra_environment=None,
  676. ).once()
  677. module.list_archive(
  678. repository_path='repo',
  679. config={},
  680. local_borg_version='1.2.3',
  681. list_arguments=argparse.Namespace(
  682. repository='repo',
  683. archive=None,
  684. paths=None,
  685. short=True,
  686. format=None,
  687. json=None,
  688. find_paths=['foo.txt'],
  689. **altered_filter_flags,
  690. ),
  691. global_arguments=global_arguments,
  692. )