test_list.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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.borgmatic.config.options).should_receive('get_working_directory').and_return(
  227. None
  228. )
  229. flexmock(module).should_receive('execute_command_and_capture_output').and_return('')
  230. flexmock(module).should_receive('make_list_command')
  231. module.capture_archive_listing(
  232. repository_path='repo',
  233. archive='archive',
  234. config={},
  235. local_borg_version=flexmock(),
  236. global_arguments=flexmock(log_json=False),
  237. )
  238. def test_list_archive_calls_borg_with_flags():
  239. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  240. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  241. flexmock(module.logger).answer = lambda message: None
  242. list_arguments = argparse.Namespace(
  243. archive='archive',
  244. paths=None,
  245. json=False,
  246. find_paths=None,
  247. prefix=None,
  248. match_archives=None,
  249. sort_by=None,
  250. first=None,
  251. last=None,
  252. )
  253. global_arguments = flexmock(log_json=False)
  254. flexmock(module.feature).should_receive('available').and_return(False)
  255. flexmock(module).should_receive('make_list_command').with_args(
  256. repository_path='repo',
  257. config={},
  258. local_borg_version='1.2.3',
  259. list_arguments=list_arguments,
  260. global_arguments=global_arguments,
  261. local_path='borg',
  262. remote_path=None,
  263. ).and_return(('borg', 'list', 'repo::archive'))
  264. flexmock(module).should_receive('make_find_paths').and_return(())
  265. flexmock(module.environment).should_receive('make_environment')
  266. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  267. None
  268. )
  269. flexmock(module).should_receive('execute_command').with_args(
  270. ('borg', 'list', 'repo::archive'),
  271. output_log_level=module.borgmatic.logger.ANSWER,
  272. extra_environment=None,
  273. working_directory=None,
  274. borg_local_path='borg',
  275. borg_exit_codes=None,
  276. ).once()
  277. module.list_archive(
  278. repository_path='repo',
  279. config={},
  280. local_borg_version='1.2.3',
  281. list_arguments=list_arguments,
  282. global_arguments=global_arguments,
  283. )
  284. def test_list_archive_with_archive_and_json_errors():
  285. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  286. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  287. flexmock(module.logger).answer = lambda message: None
  288. list_arguments = argparse.Namespace(archive='archive', paths=None, json=True, find_paths=None)
  289. flexmock(module.feature).should_receive('available').and_return(False)
  290. with pytest.raises(ValueError):
  291. module.list_archive(
  292. repository_path='repo',
  293. config={},
  294. local_borg_version='1.2.3',
  295. list_arguments=list_arguments,
  296. global_arguments=flexmock(log_json=False),
  297. )
  298. def test_list_archive_calls_borg_with_local_path():
  299. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  300. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  301. flexmock(module.logger).answer = lambda message: None
  302. list_arguments = argparse.Namespace(
  303. archive='archive',
  304. paths=None,
  305. json=False,
  306. find_paths=None,
  307. prefix=None,
  308. match_archives=None,
  309. sort_by=None,
  310. first=None,
  311. last=None,
  312. )
  313. global_arguments = flexmock(log_json=False)
  314. flexmock(module.feature).should_receive('available').and_return(False)
  315. flexmock(module).should_receive('make_list_command').with_args(
  316. repository_path='repo',
  317. config={},
  318. local_borg_version='1.2.3',
  319. list_arguments=list_arguments,
  320. global_arguments=global_arguments,
  321. local_path='borg2',
  322. remote_path=None,
  323. ).and_return(('borg2', 'list', 'repo::archive'))
  324. flexmock(module).should_receive('make_find_paths').and_return(())
  325. flexmock(module.environment).should_receive('make_environment')
  326. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  327. None
  328. )
  329. flexmock(module).should_receive('execute_command').with_args(
  330. ('borg2', 'list', 'repo::archive'),
  331. output_log_level=module.borgmatic.logger.ANSWER,
  332. extra_environment=None,
  333. working_directory=None,
  334. borg_local_path='borg2',
  335. borg_exit_codes=None,
  336. ).once()
  337. module.list_archive(
  338. repository_path='repo',
  339. config={},
  340. local_borg_version='1.2.3',
  341. list_arguments=list_arguments,
  342. global_arguments=global_arguments,
  343. local_path='borg2',
  344. )
  345. def test_list_archive_calls_borg_using_exit_codes():
  346. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  347. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  348. flexmock(module.logger).answer = lambda message: None
  349. list_arguments = argparse.Namespace(
  350. archive='archive',
  351. paths=None,
  352. json=False,
  353. find_paths=None,
  354. prefix=None,
  355. match_archives=None,
  356. sort_by=None,
  357. first=None,
  358. last=None,
  359. )
  360. global_arguments = flexmock(log_json=False)
  361. flexmock(module.feature).should_receive('available').and_return(False)
  362. borg_exit_codes = flexmock()
  363. flexmock(module).should_receive('make_list_command').with_args(
  364. repository_path='repo',
  365. config={'borg_exit_codes': borg_exit_codes},
  366. local_borg_version='1.2.3',
  367. list_arguments=list_arguments,
  368. global_arguments=global_arguments,
  369. local_path='borg',
  370. remote_path=None,
  371. ).and_return(('borg', 'list', 'repo::archive'))
  372. flexmock(module).should_receive('make_find_paths').and_return(())
  373. flexmock(module.environment).should_receive('make_environment')
  374. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  375. None
  376. )
  377. flexmock(module).should_receive('execute_command').with_args(
  378. ('borg', 'list', 'repo::archive'),
  379. output_log_level=module.borgmatic.logger.ANSWER,
  380. extra_environment=None,
  381. working_directory=None,
  382. borg_local_path='borg',
  383. borg_exit_codes=borg_exit_codes,
  384. ).once()
  385. module.list_archive(
  386. repository_path='repo',
  387. config={'borg_exit_codes': borg_exit_codes},
  388. local_borg_version='1.2.3',
  389. list_arguments=list_arguments,
  390. global_arguments=global_arguments,
  391. )
  392. def test_list_archive_calls_borg_multiple_times_with_find_paths():
  393. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  394. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  395. flexmock(module.logger).answer = lambda message: None
  396. glob_paths = ('**/*foo.txt*/**',)
  397. list_arguments = argparse.Namespace(
  398. archive=None,
  399. json=False,
  400. find_paths=['foo.txt'],
  401. prefix=None,
  402. match_archives=None,
  403. sort_by=None,
  404. first=None,
  405. last=None,
  406. )
  407. flexmock(module.feature).should_receive('available').and_return(False)
  408. flexmock(module.repo_list).should_receive('make_repo_list_command').and_return(
  409. ('borg', 'list', 'repo')
  410. )
  411. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  412. None
  413. )
  414. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  415. ('borg', 'list', 'repo'),
  416. extra_environment=None,
  417. working_directory=None,
  418. borg_local_path='borg',
  419. borg_exit_codes=None,
  420. ).and_return('archive1\narchive2').once()
  421. flexmock(module).should_receive('make_list_command').and_return(
  422. ('borg', 'list', 'repo::archive1')
  423. ).and_return(('borg', 'list', 'repo::archive2'))
  424. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  425. flexmock(module.environment).should_receive('make_environment')
  426. flexmock(module).should_receive('execute_command').with_args(
  427. ('borg', 'list', 'repo::archive1') + glob_paths,
  428. output_log_level=module.borgmatic.logger.ANSWER,
  429. extra_environment=None,
  430. working_directory=None,
  431. borg_local_path='borg',
  432. borg_exit_codes=None,
  433. ).once()
  434. flexmock(module).should_receive('execute_command').with_args(
  435. ('borg', 'list', 'repo::archive2') + glob_paths,
  436. output_log_level=module.borgmatic.logger.ANSWER,
  437. extra_environment=None,
  438. working_directory=None,
  439. borg_local_path='borg',
  440. borg_exit_codes=None,
  441. ).once()
  442. module.list_archive(
  443. repository_path='repo',
  444. config={},
  445. local_borg_version='1.2.3',
  446. list_arguments=list_arguments,
  447. global_arguments=flexmock(log_json=False),
  448. )
  449. def test_list_archive_calls_borg_with_archive():
  450. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  451. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  452. flexmock(module.logger).answer = lambda message: None
  453. list_arguments = argparse.Namespace(
  454. archive='archive',
  455. paths=None,
  456. json=False,
  457. find_paths=None,
  458. prefix=None,
  459. match_archives=None,
  460. sort_by=None,
  461. first=None,
  462. last=None,
  463. )
  464. global_arguments = flexmock(log_json=False)
  465. flexmock(module.feature).should_receive('available').and_return(False)
  466. flexmock(module).should_receive('make_list_command').with_args(
  467. repository_path='repo',
  468. config={},
  469. local_borg_version='1.2.3',
  470. list_arguments=list_arguments,
  471. global_arguments=global_arguments,
  472. local_path='borg',
  473. remote_path=None,
  474. ).and_return(('borg', 'list', 'repo::archive'))
  475. flexmock(module).should_receive('make_find_paths').and_return(())
  476. flexmock(module.environment).should_receive('make_environment')
  477. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  478. None
  479. )
  480. flexmock(module).should_receive('execute_command').with_args(
  481. ('borg', 'list', 'repo::archive'),
  482. output_log_level=module.borgmatic.logger.ANSWER,
  483. extra_environment=None,
  484. working_directory=None,
  485. borg_local_path='borg',
  486. borg_exit_codes=None,
  487. ).once()
  488. module.list_archive(
  489. repository_path='repo',
  490. config={},
  491. local_borg_version='1.2.3',
  492. list_arguments=list_arguments,
  493. global_arguments=global_arguments,
  494. )
  495. def test_list_archive_without_archive_delegates_to_list_repository():
  496. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  497. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  498. flexmock(module.logger).answer = lambda message: None
  499. list_arguments = argparse.Namespace(
  500. archive=None,
  501. short=None,
  502. format=None,
  503. json=None,
  504. prefix=None,
  505. match_archives=None,
  506. sort_by=None,
  507. first=None,
  508. last=None,
  509. find_paths=None,
  510. )
  511. flexmock(module.feature).should_receive('available').and_return(False)
  512. flexmock(module.repo_list).should_receive('list_repository')
  513. flexmock(module.environment).should_receive('make_environment').never()
  514. flexmock(module).should_receive('execute_command').never()
  515. module.list_archive(
  516. repository_path='repo',
  517. config={},
  518. local_borg_version='1.2.3',
  519. list_arguments=list_arguments,
  520. global_arguments=flexmock(log_json=False),
  521. )
  522. def test_list_archive_with_borg_features_without_archive_delegates_to_list_repository():
  523. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  524. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  525. flexmock(module.logger).answer = lambda message: None
  526. list_arguments = argparse.Namespace(
  527. archive=None,
  528. short=None,
  529. format=None,
  530. json=None,
  531. prefix=None,
  532. match_archives=None,
  533. sort_by=None,
  534. first=None,
  535. last=None,
  536. find_paths=None,
  537. )
  538. flexmock(module.feature).should_receive('available').and_return(True)
  539. flexmock(module.repo_list).should_receive('list_repository')
  540. flexmock(module.environment).should_receive('make_environment').never()
  541. flexmock(module).should_receive('execute_command').never()
  542. module.list_archive(
  543. repository_path='repo',
  544. config={},
  545. local_borg_version='1.2.3',
  546. list_arguments=list_arguments,
  547. global_arguments=flexmock(log_json=False),
  548. )
  549. @pytest.mark.parametrize(
  550. 'archive_filter_flag',
  551. (
  552. 'prefix',
  553. 'match_archives',
  554. 'sort_by',
  555. 'first',
  556. 'last',
  557. ),
  558. )
  559. def test_list_archive_with_archive_ignores_archive_filter_flag(
  560. archive_filter_flag,
  561. ):
  562. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  563. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  564. flexmock(module.logger).answer = lambda message: None
  565. global_arguments = flexmock(log_json=False)
  566. default_filter_flags = {
  567. 'prefix': None,
  568. 'match_archives': None,
  569. 'sort_by': None,
  570. 'first': None,
  571. 'last': None,
  572. }
  573. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  574. flexmock(module.feature).should_receive('available').with_args(
  575. module.feature.Feature.REPO_LIST, '1.2.3'
  576. ).and_return(False)
  577. flexmock(module).should_receive('make_list_command').with_args(
  578. repository_path='repo',
  579. config={},
  580. local_borg_version='1.2.3',
  581. list_arguments=argparse.Namespace(
  582. archive='archive', paths=None, json=False, find_paths=None, **default_filter_flags
  583. ),
  584. global_arguments=global_arguments,
  585. local_path='borg',
  586. remote_path=None,
  587. ).and_return(('borg', 'list', 'repo::archive'))
  588. flexmock(module).should_receive('make_find_paths').and_return(())
  589. flexmock(module.environment).should_receive('make_environment')
  590. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  591. None
  592. )
  593. flexmock(module).should_receive('execute_command').with_args(
  594. ('borg', 'list', 'repo::archive'),
  595. output_log_level=module.borgmatic.logger.ANSWER,
  596. extra_environment=None,
  597. working_directory=None,
  598. borg_local_path='borg',
  599. borg_exit_codes=None,
  600. ).once()
  601. module.list_archive(
  602. repository_path='repo',
  603. config={},
  604. local_borg_version='1.2.3',
  605. list_arguments=argparse.Namespace(
  606. archive='archive', paths=None, json=False, find_paths=None, **altered_filter_flags
  607. ),
  608. global_arguments=global_arguments,
  609. )
  610. @pytest.mark.parametrize(
  611. 'archive_filter_flag',
  612. (
  613. 'prefix',
  614. 'match_archives',
  615. 'sort_by',
  616. 'first',
  617. 'last',
  618. ),
  619. )
  620. def test_list_archive_with_find_paths_allows_archive_filter_flag_but_only_passes_it_to_repo_list(
  621. archive_filter_flag,
  622. ):
  623. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  624. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  625. flexmock(module.logger).answer = lambda message: None
  626. default_filter_flags = {
  627. 'prefix': None,
  628. 'match_archives': None,
  629. 'sort_by': None,
  630. 'first': None,
  631. 'last': None,
  632. }
  633. altered_filter_flags = {**default_filter_flags, **{archive_filter_flag: 'foo'}}
  634. glob_paths = ('**/*foo.txt*/**',)
  635. global_arguments = flexmock(log_json=False)
  636. flexmock(module.feature).should_receive('available').and_return(True)
  637. flexmock(module.repo_list).should_receive('make_repo_list_command').with_args(
  638. repository_path='repo',
  639. config={},
  640. local_borg_version='1.2.3',
  641. repo_list_arguments=argparse.Namespace(
  642. repository='repo', short=True, format=None, json=None, **altered_filter_flags
  643. ),
  644. global_arguments=global_arguments,
  645. local_path='borg',
  646. remote_path=None,
  647. ).and_return(('borg', 'repo-list', '--repo', 'repo'))
  648. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  649. None
  650. )
  651. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  652. ('borg', 'repo-list', '--repo', 'repo'),
  653. extra_environment=None,
  654. working_directory=None,
  655. borg_local_path='borg',
  656. borg_exit_codes=None,
  657. ).and_return('archive1\narchive2').once()
  658. flexmock(module).should_receive('make_list_command').with_args(
  659. repository_path='repo',
  660. config={},
  661. local_borg_version='1.2.3',
  662. list_arguments=argparse.Namespace(
  663. repository='repo',
  664. archive='archive1',
  665. paths=None,
  666. short=True,
  667. format=None,
  668. json=None,
  669. find_paths=['foo.txt'],
  670. **default_filter_flags,
  671. ),
  672. global_arguments=global_arguments,
  673. local_path='borg',
  674. remote_path=None,
  675. ).and_return(('borg', 'list', '--repo', 'repo', 'archive1'))
  676. flexmock(module).should_receive('make_list_command').with_args(
  677. repository_path='repo',
  678. config={},
  679. local_borg_version='1.2.3',
  680. list_arguments=argparse.Namespace(
  681. repository='repo',
  682. archive='archive2',
  683. paths=None,
  684. short=True,
  685. format=None,
  686. json=None,
  687. find_paths=['foo.txt'],
  688. **default_filter_flags,
  689. ),
  690. global_arguments=global_arguments,
  691. local_path='borg',
  692. remote_path=None,
  693. ).and_return(('borg', 'list', '--repo', 'repo', 'archive2'))
  694. flexmock(module).should_receive('make_find_paths').and_return(glob_paths)
  695. flexmock(module.environment).should_receive('make_environment')
  696. flexmock(module).should_receive('execute_command').with_args(
  697. ('borg', 'list', '--repo', 'repo', 'archive1') + glob_paths,
  698. output_log_level=module.borgmatic.logger.ANSWER,
  699. extra_environment=None,
  700. working_directory=None,
  701. borg_local_path='borg',
  702. borg_exit_codes=None,
  703. ).once()
  704. flexmock(module).should_receive('execute_command').with_args(
  705. ('borg', 'list', '--repo', 'repo', 'archive2') + glob_paths,
  706. output_log_level=module.borgmatic.logger.ANSWER,
  707. extra_environment=None,
  708. working_directory=None,
  709. borg_local_path='borg',
  710. borg_exit_codes=None,
  711. ).once()
  712. module.list_archive(
  713. repository_path='repo',
  714. config={},
  715. local_borg_version='1.2.3',
  716. list_arguments=argparse.Namespace(
  717. repository='repo',
  718. archive=None,
  719. paths=None,
  720. short=True,
  721. format=None,
  722. json=None,
  723. find_paths=['foo.txt'],
  724. **altered_filter_flags,
  725. ),
  726. global_arguments=global_arguments,
  727. )
  728. def test_list_archive_calls_borg_with_working_directory():
  729. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  730. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  731. flexmock(module.logger).answer = lambda message: None
  732. list_arguments = argparse.Namespace(
  733. archive='archive',
  734. paths=None,
  735. json=False,
  736. find_paths=None,
  737. prefix=None,
  738. match_archives=None,
  739. sort_by=None,
  740. first=None,
  741. last=None,
  742. )
  743. global_arguments = flexmock(log_json=False)
  744. flexmock(module.feature).should_receive('available').and_return(False)
  745. flexmock(module).should_receive('make_list_command').with_args(
  746. repository_path='repo',
  747. config={'working_directory': '/working/dir'},
  748. local_borg_version='1.2.3',
  749. list_arguments=list_arguments,
  750. global_arguments=global_arguments,
  751. local_path='borg',
  752. remote_path=None,
  753. ).and_return(('borg', 'list', 'repo::archive'))
  754. flexmock(module).should_receive('make_find_paths').and_return(())
  755. flexmock(module.environment).should_receive('make_environment')
  756. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  757. '/working/dir',
  758. )
  759. flexmock(module).should_receive('execute_command').with_args(
  760. ('borg', 'list', 'repo::archive'),
  761. output_log_level=module.borgmatic.logger.ANSWER,
  762. extra_environment=None,
  763. working_directory='/working/dir',
  764. borg_local_path='borg',
  765. borg_exit_codes=None,
  766. ).once()
  767. module.list_archive(
  768. repository_path='repo',
  769. config={'working_directory': '/working/dir'},
  770. local_borg_version='1.2.3',
  771. list_arguments=list_arguments,
  772. global_arguments=global_arguments,
  773. )