test_list.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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, format=None, json=False),
  17. global_arguments=flexmock(),
  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, format=None, json=True),
  30. global_arguments=flexmock(),
  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, format=None, json=False),
  43. global_arguments=flexmock(),
  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, format=None, json=True),
  56. global_arguments=flexmock(),
  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, format=None, json=True),
  68. global_arguments=flexmock(),
  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(()).and_return(
  73. ('--log-json',),
  74. ).and_return(())
  75. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  76. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  77. command = module.make_list_command(
  78. repository_path='repo',
  79. config={'log_json': True},
  80. local_borg_version='1.2.3',
  81. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  82. global_arguments=flexmock(),
  83. )
  84. assert command == ('borg', 'list', '--log-json', 'repo')
  85. def test_make_list_command_includes_lock_wait():
  86. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  87. ('--lock-wait', '5'),
  88. ).and_return(())
  89. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  90. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  91. command = module.make_list_command(
  92. repository_path='repo',
  93. config={'lock_wait': 5},
  94. local_borg_version='1.2.3',
  95. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  96. global_arguments=flexmock(),
  97. )
  98. assert command == ('borg', 'list', '--lock-wait', '5', 'repo')
  99. def test_make_list_command_includes_format():
  100. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(()).and_return(
  101. ()
  102. ).and_return(('--format', 'stuff'))
  103. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  104. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  105. command = module.make_list_command(
  106. repository_path='repo',
  107. config={},
  108. local_borg_version='1.2.3',
  109. list_arguments=flexmock(archive=None, paths=None, format='stuff', json=False),
  110. global_arguments=flexmock(),
  111. )
  112. assert command == ('borg', 'list', '--format', 'stuff', 'repo')
  113. def test_make_list_command_includes_extra_borg_options():
  114. flexmock(module.flags).should_receive('make_flags').and_return(())
  115. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  116. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  117. command = module.make_list_command(
  118. repository_path='repo',
  119. config={'extra_borg_options': {'list': '--extra "value with space"'}},
  120. local_borg_version='1.2.3',
  121. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  122. global_arguments=flexmock(),
  123. )
  124. assert command == ('borg', 'list', '--extra', 'value with space', 'repo')
  125. def test_make_list_command_includes_archive():
  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_archive_flags').and_return(
  129. ('repo::archive',),
  130. )
  131. command = module.make_list_command(
  132. repository_path='repo',
  133. config={},
  134. local_borg_version='1.2.3',
  135. list_arguments=flexmock(archive='archive', paths=None, format=None, json=False),
  136. global_arguments=flexmock(),
  137. )
  138. assert command == ('borg', 'list', 'repo::archive')
  139. def test_make_list_command_includes_archive_and_path():
  140. flexmock(module.flags).should_receive('make_flags').and_return(())
  141. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  142. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  143. ('repo::archive',),
  144. )
  145. command = module.make_list_command(
  146. repository_path='repo',
  147. config={},
  148. local_borg_version='1.2.3',
  149. list_arguments=flexmock(archive='archive', paths=['var/lib'], format=None, json=False),
  150. global_arguments=flexmock(),
  151. )
  152. assert command == ('borg', 'list', 'repo::archive', 'var/lib')
  153. def test_make_list_command_includes_local_path():
  154. flexmock(module.flags).should_receive('make_flags').and_return(())
  155. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  156. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  157. command = module.make_list_command(
  158. repository_path='repo',
  159. config={},
  160. local_borg_version='1.2.3',
  161. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  162. global_arguments=flexmock(),
  163. local_path='borg2',
  164. )
  165. assert command == ('borg2', 'list', 'repo')
  166. def test_make_list_command_includes_remote_path():
  167. flexmock(module.flags).should_receive('make_flags').and_return(())
  168. flexmock(module.flags).should_receive('make_flags').with_args(
  169. 'remote-path',
  170. 'borg2',
  171. ).and_return(('--remote-path', 'borg2'))
  172. flexmock(module.flags).should_receive('make_flags').with_args('log-json', True).and_return(
  173. ('--log-json'),
  174. )
  175. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  176. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  177. command = module.make_list_command(
  178. repository_path='repo',
  179. config={},
  180. local_borg_version='1.2.3',
  181. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  182. global_arguments=flexmock(),
  183. remote_path='borg2',
  184. )
  185. assert command == ('borg', 'list', '--remote-path', 'borg2', 'repo')
  186. def test_make_list_command_includes_umask():
  187. flexmock(module.flags).should_receive('make_flags').replace_with(
  188. lambda name, value: (f'--{name}', value) if value else (),
  189. )
  190. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  191. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  192. command = module.make_list_command(
  193. repository_path='repo',
  194. config={'umask': '077'},
  195. local_borg_version='1.2.3',
  196. list_arguments=flexmock(archive=None, paths=None, format=None, json=False),
  197. global_arguments=flexmock(),
  198. )
  199. assert command == ('borg', 'list', '--umask', '077', 'repo')
  200. def test_make_list_command_includes_short():
  201. flexmock(module.flags).should_receive('make_flags').and_return(())
  202. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--short',))
  203. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  204. command = module.make_list_command(
  205. repository_path='repo',
  206. config={},
  207. local_borg_version='1.2.3',
  208. list_arguments=flexmock(archive=None, paths=None, format=None, json=False, short=True),
  209. global_arguments=flexmock(),
  210. )
  211. assert command == ('borg', 'list', '--short', 'repo')
  212. @pytest.mark.parametrize(
  213. 'argument_name',
  214. (
  215. 'prefix',
  216. 'match_archives',
  217. 'sort_by',
  218. 'first',
  219. 'last',
  220. 'exclude',
  221. 'exclude_from',
  222. 'pattern',
  223. 'patterns_from',
  224. ),
  225. )
  226. def test_make_list_command_includes_additional_flags(argument_name):
  227. flexmock(module.flags).should_receive('make_flags').and_return(())
  228. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  229. (f"--{argument_name.replace('_', '-')}", 'value'),
  230. )
  231. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  232. command = module.make_list_command(
  233. repository_path='repo',
  234. config={},
  235. local_borg_version='1.2.3',
  236. list_arguments=flexmock(
  237. archive=None,
  238. paths=None,
  239. format=None,
  240. json=False,
  241. find_paths=None,
  242. **{argument_name: 'value'},
  243. ),
  244. global_arguments=flexmock(),
  245. )
  246. assert command == ('borg', 'list', '--' + argument_name.replace('_', '-'), 'value', 'repo')
  247. def test_make_find_paths_considers_none_as_empty_paths():
  248. assert module.make_find_paths(None) == ()
  249. def test_make_find_paths_passes_through_patterns():
  250. find_paths = (
  251. 'fm:*',
  252. 'sh:**/*.txt',
  253. 're:^.*$',
  254. 'pp:root/somedir',
  255. 'pf:root/foo.txt',
  256. 'R /',
  257. 'r /',
  258. 'p /',
  259. 'P /',
  260. '+ /',
  261. '- /',
  262. '! /',
  263. )
  264. assert module.make_find_paths(find_paths) == find_paths
  265. def test_make_find_paths_adds_globs_to_path_fragments():
  266. assert module.make_find_paths(('foo.txt',)) == ('sh:**/*foo.txt*/**',)
  267. def test_capture_archive_listing_does_not_raise():
  268. flexmock(module.environment).should_receive('make_environment')
  269. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  270. flexmock(module).should_receive('execute_command_and_capture_output').and_return('')
  271. flexmock(module).should_receive('make_list_command')
  272. module.capture_archive_listing(
  273. repository_path='repo',
  274. archive='archive',
  275. config={},
  276. local_borg_version=flexmock(),
  277. global_arguments=flexmock(),
  278. )
  279. def test_list_archive_calls_borg_with_flags():
  280. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  281. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  282. flexmock(module.logger).answer = lambda message: None
  283. list_arguments = argparse.Namespace(
  284. archive='archive',
  285. paths=None,
  286. format=None,
  287. json=False,
  288. find_paths=None,
  289. prefix=None,
  290. match_archives=None,
  291. sort_by=None,
  292. first=None,
  293. last=None,
  294. )
  295. global_arguments = flexmock()
  296. flexmock(module.feature).should_receive('available').and_return(False)
  297. flexmock(module).should_receive('make_list_command').with_args(
  298. repository_path='repo',
  299. config={},
  300. local_borg_version='1.2.3',
  301. list_arguments=list_arguments,
  302. global_arguments=global_arguments,
  303. local_path='borg',
  304. remote_path=None,
  305. ).and_return(('borg', 'list', 'repo::archive'))
  306. flexmock(module).should_receive('make_find_paths').and_return(())
  307. flexmock(module.environment).should_receive('make_environment')
  308. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  309. flexmock(module).should_receive('execute_command').with_args(
  310. ('borg', 'list', 'repo::archive'),
  311. output_log_level=module.borgmatic.logger.ANSWER,
  312. environment=None,
  313. working_directory=None,
  314. borg_local_path='borg',
  315. borg_exit_codes=None,
  316. ).once()
  317. module.list_archive(
  318. repository_path='repo',
  319. config={},
  320. local_borg_version='1.2.3',
  321. list_arguments=list_arguments,
  322. global_arguments=global_arguments,
  323. )
  324. def test_list_archive_with_archive_and_json_errors():
  325. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  326. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  327. flexmock(module.logger).answer = lambda message: None
  328. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  329. flexmock(module.feature).should_receive('available').and_return(False)
  330. with pytest.raises(ValueError):
  331. module.list_archive(
  332. repository_path='repo',
  333. config={},
  334. local_borg_version='1.2.3',
  335. list_arguments=list_arguments,
  336. global_arguments=flexmock(),
  337. )
  338. def test_list_archive_calls_borg_with_local_path():
  339. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  340. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  341. flexmock(module.logger).answer = lambda message: None
  342. list_arguments = argparse.Namespace(
  343. archive='archive',
  344. paths=None,
  345. format=None,
  346. json=False,
  347. find_paths=None,
  348. prefix=None,
  349. match_archives=None,
  350. sort_by=None,
  351. first=None,
  352. last=None,
  353. )
  354. global_arguments = flexmock()
  355. flexmock(module.feature).should_receive('available').and_return(False)
  356. flexmock(module).should_receive('make_list_command').with_args(
  357. repository_path='repo',
  358. config={},
  359. local_borg_version='1.2.3',
  360. list_arguments=list_arguments,
  361. global_arguments=global_arguments,
  362. local_path='borg2',
  363. remote_path=None,
  364. ).and_return(('borg2', 'list', 'repo::archive'))
  365. flexmock(module).should_receive('make_find_paths').and_return(())
  366. flexmock(module.environment).should_receive('make_environment')
  367. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  368. flexmock(module).should_receive('execute_command').with_args(
  369. ('borg2', 'list', 'repo::archive'),
  370. output_log_level=module.borgmatic.logger.ANSWER,
  371. environment=None,
  372. working_directory=None,
  373. borg_local_path='borg2',
  374. borg_exit_codes=None,
  375. ).once()
  376. module.list_archive(
  377. repository_path='repo',
  378. config={},
  379. local_borg_version='1.2.3',
  380. list_arguments=list_arguments,
  381. global_arguments=global_arguments,
  382. local_path='borg2',
  383. )
  384. def test_list_archive_calls_borg_using_exit_codes():
  385. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  386. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  387. flexmock(module.logger).answer = lambda message: None
  388. list_arguments = argparse.Namespace(
  389. archive='archive',
  390. paths=None,
  391. json=False,
  392. find_paths=None,
  393. prefix=None,
  394. match_archives=None,
  395. sort_by=None,
  396. first=None,
  397. last=None,
  398. )
  399. global_arguments = flexmock()
  400. flexmock(module.feature).should_receive('available').and_return(False)
  401. borg_exit_codes = flexmock()
  402. flexmock(module).should_receive('make_list_command').with_args(
  403. repository_path='repo',
  404. config={'borg_exit_codes': borg_exit_codes},
  405. local_borg_version='1.2.3',
  406. list_arguments=list_arguments,
  407. global_arguments=global_arguments,
  408. local_path='borg',
  409. remote_path=None,
  410. ).and_return(('borg', 'list', 'repo::archive'))
  411. flexmock(module).should_receive('make_find_paths').and_return(())
  412. flexmock(module.environment).should_receive('make_environment')
  413. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  414. flexmock(module).should_receive('execute_command').with_args(
  415. ('borg', 'list', 'repo::archive'),
  416. output_log_level=module.borgmatic.logger.ANSWER,
  417. environment=None,
  418. working_directory=None,
  419. borg_local_path='borg',
  420. borg_exit_codes=borg_exit_codes,
  421. ).once()
  422. module.list_archive(
  423. repository_path='repo',
  424. config={'borg_exit_codes': borg_exit_codes},
  425. local_borg_version='1.2.3',
  426. list_arguments=list_arguments,
  427. global_arguments=global_arguments,
  428. )
  429. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  430. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  431. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  432. flexmock(module.logger).answer = lambda message: None
  433. glob_paths = ('**/*foo.txt*/**',)
  434. list_arguments = argparse.Namespace(
  435. archive=None,
  436. json=False,
  437. find_paths=['foo.txt'],
  438. prefix=None,
  439. match_archives=None,
  440. sort_by=None,
  441. first=None,
  442. last=None,
  443. )
  444. flexmock(module.feature).should_receive('available').and_return(False)
  445. flexmock(module.repo_list).should_receive('make_repo_list_command').and_return(
  446. ('borg', 'list', 'repo'),
  447. )
  448. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  449. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  450. ('borg', 'list', 'repo'),
  451. environment=None,
  452. working_directory=None,
  453. borg_local_path='borg',
  454. borg_exit_codes=None,
  455. ).and_return('archive1\narchive2').once()
  456. flexmock(module).should_receive('make_list_command').and_return(
  457. ('borg', 'list', 'repo::archive1'),
  458. ).and_return(('borg', 'list', 'repo::archive2'))
  459. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  460. flexmock(module.environment).should_receive('make_environment')
  461. flexmock(module).should_receive('execute_command').with_args(
  462. ('borg', 'list', 'repo::archive1', *glob_paths),
  463. output_log_level=module.borgmatic.logger.ANSWER,
  464. environment=None,
  465. working_directory=None,
  466. borg_local_path='borg',
  467. borg_exit_codes=None,
  468. ).once()
  469. flexmock(module).should_receive('execute_command').with_args(
  470. ('borg', 'list', 'repo::archive2', *glob_paths),
  471. output_log_level=module.borgmatic.logger.ANSWER,
  472. environment=None,
  473. working_directory=None,
  474. borg_local_path='borg',
  475. borg_exit_codes=None,
  476. ).once()
  477. module.list_archive(
  478. repository_path='repo',
  479. config={},
  480. local_borg_version='1.2.3',
  481. list_arguments=list_arguments,
  482. global_arguments=flexmock(),
  483. )
  484. def test_list_archive_calls_borg_with_archive():
  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. list_arguments = argparse.Namespace(
  489. archive='archive',
  490. paths=None,
  491. json=False,
  492. find_paths=None,
  493. prefix=None,
  494. match_archives=None,
  495. sort_by=None,
  496. first=None,
  497. last=None,
  498. )
  499. global_arguments = flexmock()
  500. flexmock(module.feature).should_receive('available').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=list_arguments,
  506. global_arguments=global_arguments,
  507. local_path='borg',
  508. remote_path=None,
  509. ).and_return(('borg', 'list', 'repo::archive'))
  510. flexmock(module).should_receive('make_find_paths').and_return(())
  511. flexmock(module.environment).should_receive('make_environment')
  512. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  513. flexmock(module).should_receive('execute_command').with_args(
  514. ('borg', 'list', 'repo::archive'),
  515. output_log_level=module.borgmatic.logger.ANSWER,
  516. environment=None,
  517. working_directory=None,
  518. borg_local_path='borg',
  519. borg_exit_codes=None,
  520. ).once()
  521. module.list_archive(
  522. repository_path='repo',
  523. config={},
  524. local_borg_version='1.2.3',
  525. list_arguments=list_arguments,
  526. global_arguments=global_arguments,
  527. )
  528. def test_list_archive_without_archive_delegates_to_list_repository():
  529. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  530. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  531. flexmock(module.logger).answer = lambda message: None
  532. list_arguments = argparse.Namespace(
  533. archive=None,
  534. short=None,
  535. format=None,
  536. json=None,
  537. prefix=None,
  538. match_archives=None,
  539. sort_by=None,
  540. first=None,
  541. last=None,
  542. find_paths=None,
  543. )
  544. flexmock(module.feature).should_receive('available').and_return(False)
  545. flexmock(module.repo_list).should_receive('list_repository')
  546. flexmock(module.environment).should_receive('make_environment').never()
  547. flexmock(module).should_receive('execute_command').never()
  548. module.list_archive(
  549. repository_path='repo',
  550. config={},
  551. local_borg_version='1.2.3',
  552. list_arguments=list_arguments,
  553. global_arguments=flexmock(),
  554. )
  555. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  556. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  557. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  558. flexmock(module.logger).answer = lambda message: None
  559. list_arguments = argparse.Namespace(
  560. archive=None,
  561. short=None,
  562. format=None,
  563. json=None,
  564. prefix=None,
  565. match_archives=None,
  566. sort_by=None,
  567. first=None,
  568. last=None,
  569. find_paths=None,
  570. )
  571. flexmock(module.feature).should_receive('available').and_return(True)
  572. flexmock(module.repo_list).should_receive('list_repository')
  573. flexmock(module.environment).should_receive('make_environment').never()
  574. flexmock(module).should_receive('execute_command').never()
  575. module.list_archive(
  576. repository_path='repo',
  577. config={},
  578. local_borg_version='1.2.3',
  579. list_arguments=list_arguments,
  580. global_arguments=flexmock(),
  581. )
  582. @pytest.mark.parametrize(
  583. 'archive_filter_flag',
  584. (
  585. 'prefix',
  586. 'match_archives',
  587. 'sort_by',
  588. 'first',
  589. 'last',
  590. ),
  591. )
  592. def test_list_archive_with_archive_ignores_archive_filter_flag(
  593. archive_filter_flag,
  594. ):
  595. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  596. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  597. flexmock(module.logger).answer = lambda message: None
  598. global_arguments = flexmock()
  599. default_filter_flags = {
  600. 'prefix': None,
  601. 'match_archives': None,
  602. 'sort_by': None,
  603. 'first': None,
  604. 'last': None,
  605. }
  606. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}} # noqa: PIE800
  607. flexmock(module.feature).should_receive('available').with_args(
  608. module.feature.Feature.REPO_LIST,
  609. '1.2.3',
  610. ).and_return(False)
  611. flexmock(module).should_receive('make_list_command').with_args(
  612. repository_path='repo',
  613. config={},
  614. local_borg_version='1.2.3',
  615. list_arguments=argparse.Namespace(
  616. archive='archive',
  617. paths=None,
  618. json=False,
  619. find_paths=None,
  620. **default_filter_flags,
  621. ),
  622. global_arguments=global_arguments,
  623. local_path='borg',
  624. remote_path=None,
  625. ).and_return(('borg', 'list', 'repo::archive'))
  626. flexmock(module).should_receive('make_find_paths').and_return(())
  627. flexmock(module.environment).should_receive('make_environment')
  628. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  629. flexmock(module).should_receive('execute_command').with_args(
  630. ('borg', 'list', 'repo::archive'),
  631. output_log_level=module.borgmatic.logger.ANSWER,
  632. environment=None,
  633. working_directory=None,
  634. borg_local_path='borg',
  635. borg_exit_codes=None,
  636. ).once()
  637. module.list_archive(
  638. repository_path='repo',
  639. config={},
  640. local_borg_version='1.2.3',
  641. list_arguments=argparse.Namespace(
  642. archive='archive',
  643. paths=None,
  644. json=False,
  645. find_paths=None,
  646. **altered_filter_flags,
  647. ),
  648. global_arguments=global_arguments,
  649. )
  650. @pytest.mark.parametrize(
  651. 'archive_filter_flag',
  652. (
  653. 'prefix',
  654. 'match_archives',
  655. 'sort_by',
  656. 'first',
  657. 'last',
  658. ),
  659. )
  660. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_repo_list(
  661. archive_filter_flag,
  662. ):
  663. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  664. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  665. flexmock(module.logger).answer = lambda message: None
  666. default_filter_flags = {
  667. 'prefix': None,
  668. 'match_archives': None,
  669. 'sort_by': None,
  670. 'first': None,
  671. 'last': None,
  672. }
  673. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}} # noqa: PIE800
  674. glob_paths = ('**/*foo.txt*/**',)
  675. global_arguments = flexmock()
  676. flexmock(module.feature).should_receive('available').and_return(True)
  677. flexmock(module.repo_list).should_receive('make_repo_list_command').with_args(
  678. repository_path='repo',
  679. config={},
  680. local_borg_version='1.2.3',
  681. repo_list_arguments=argparse.Namespace(
  682. repository='repo',
  683. short=True,
  684. format=None,
  685. json=None,
  686. **altered_filter_flags,
  687. ),
  688. global_arguments=global_arguments,
  689. local_path='borg',
  690. remote_path=None,
  691. ).and_return(('borg', 'repo-list', '--repo', 'repo'))
  692. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  693. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  694. ('borg', 'repo-list', '--repo', 'repo'),
  695. environment=None,
  696. working_directory=None,
  697. borg_local_path='borg',
  698. borg_exit_codes=None,
  699. ).and_return('archive1\narchive2').once()
  700. flexmock(module).should_receive('make_list_command').with_args(
  701. repository_path='repo',
  702. config={},
  703. local_borg_version='1.2.3',
  704. list_arguments=argparse.Namespace(
  705. repository='repo',
  706. archive='archive1',
  707. paths=None,
  708. short=True,
  709. format=None,
  710. json=None,
  711. find_paths=['foo.txt'],
  712. **default_filter_flags,
  713. ),
  714. global_arguments=global_arguments,
  715. local_path='borg',
  716. remote_path=None,
  717. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  718. flexmock(module).should_receive('make_list_command').with_args(
  719. repository_path='repo',
  720. config={},
  721. local_borg_version='1.2.3',
  722. list_arguments=argparse.Namespace(
  723. repository='repo',
  724. archive='archive2',
  725. paths=None,
  726. short=True,
  727. format=None,
  728. json=None,
  729. find_paths=['foo.txt'],
  730. **default_filter_flags,
  731. ),
  732. global_arguments=global_arguments,
  733. local_path='borg',
  734. remote_path=None,
  735. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  736. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  737. flexmock(module.environment).should_receive('make_environment')
  738. flexmock(module).should_receive('execute_command').with_args(
  739. ('borg', 'list', '--repo', 'repo', 'archive1', *glob_paths),
  740. output_log_level=module.borgmatic.logger.ANSWER,
  741. environment=None,
  742. working_directory=None,
  743. borg_local_path='borg',
  744. borg_exit_codes=None,
  745. ).once()
  746. flexmock(module).should_receive('execute_command').with_args(
  747. ('borg', 'list', '--repo', 'repo', 'archive2', *glob_paths),
  748. output_log_level=module.borgmatic.logger.ANSWER,
  749. environment=None,
  750. working_directory=None,
  751. borg_local_path='borg',
  752. borg_exit_codes=None,
  753. ).once()
  754. module.list_archive(
  755. repository_path='repo',
  756. config={},
  757. local_borg_version='1.2.3',
  758. list_arguments=argparse.Namespace(
  759. repository='repo',
  760. archive=None,
  761. paths=None,
  762. short=True,
  763. format=None,
  764. json=None,
  765. find_paths=['foo.txt'],
  766. **altered_filter_flags,
  767. ),
  768. global_arguments=global_arguments,
  769. )
  770. def test_list_archive_calls_borg_with_working_directory():
  771. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  772. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  773. flexmock(module.logger).answer = lambda message: None
  774. list_arguments = argparse.Namespace(
  775. archive='archive',
  776. paths=None,
  777. json=False,
  778. find_paths=None,
  779. prefix=None,
  780. match_archives=None,
  781. sort_by=None,
  782. first=None,
  783. last=None,
  784. )
  785. global_arguments = flexmock()
  786. flexmock(module.feature).should_receive('available').and_return(False)
  787. flexmock(module).should_receive('make_list_command').with_args(
  788. repository_path='repo',
  789. config={'working_directory': '/working/dir'},
  790. local_borg_version='1.2.3',
  791. list_arguments=list_arguments,
  792. global_arguments=global_arguments,
  793. local_path='borg',
  794. remote_path=None,
  795. ).and_return(('borg', 'list', 'repo::archive'))
  796. flexmock(module).should_receive('make_find_paths').and_return(())
  797. flexmock(module.environment).should_receive('make_environment')
  798. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  799. '/working/dir',
  800. )
  801. flexmock(module).should_receive('execute_command').with_args(
  802. ('borg', 'list', 'repo::archive'),
  803. output_log_level=module.borgmatic.logger.ANSWER,
  804. environment=None,
  805. working_directory='/working/dir',
  806. borg_local_path='borg',
  807. borg_exit_codes=None,
  808. ).once()
  809. module.list_archive(
  810. repository_path='repo',
  811. config={'working_directory': '/working/dir'},
  812. local_borg_version='1.2.3',
  813. list_arguments=list_arguments,
  814. global_arguments=global_arguments,
  815. )