test_pattern.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. import io
  2. import sys
  3. import pytest
  4. from flexmock import flexmock
  5. from borgmatic.actions import pattern as module
  6. from borgmatic.borg.pattern import Pattern, Pattern_source, Pattern_style, Pattern_type
  7. @pytest.mark.parametrize(
  8. 'pattern_line,expected_pattern',
  9. (
  10. ('R /foo', Pattern('/foo', source=Pattern_source.CONFIG)),
  11. ('P sh', Pattern('sh', Pattern_type.PATTERN_STYLE, source=Pattern_source.CONFIG)),
  12. ('+ /foo*', Pattern('/foo*', Pattern_type.INCLUDE, source=Pattern_source.CONFIG)),
  13. (
  14. '+ sh:/foo*',
  15. Pattern(
  16. '/foo*', Pattern_type.INCLUDE, Pattern_style.SHELL, source=Pattern_source.CONFIG
  17. ),
  18. ),
  19. ),
  20. )
  21. def test_parse_pattern_transforms_pattern_line_to_instance(pattern_line, expected_pattern):
  22. module.parse_pattern(pattern_line) == expected_pattern
  23. def test_parse_pattern_with_invalid_pattern_line_errors():
  24. with pytest.raises(ValueError):
  25. module.parse_pattern('/foo')
  26. def test_collect_patterns_converts_source_directories():
  27. assert module.collect_patterns({'source_directories': ['/foo', '/bar']}) == (
  28. Pattern('/foo', source=Pattern_source.CONFIG),
  29. Pattern('/bar', source=Pattern_source.CONFIG),
  30. )
  31. def test_collect_patterns_parses_config_patterns():
  32. flexmock(module).should_receive('parse_pattern').with_args('R /foo').and_return(Pattern('/foo'))
  33. flexmock(module).should_receive('parse_pattern').with_args('# comment').never()
  34. flexmock(module).should_receive('parse_pattern').with_args('').never()
  35. flexmock(module).should_receive('parse_pattern').with_args(' ').never()
  36. flexmock(module).should_receive('parse_pattern').with_args('R /bar').and_return(Pattern('/bar'))
  37. assert module.collect_patterns({'patterns': ['R /foo', '# comment', '', ' ', 'R /bar']}) == (
  38. Pattern('/foo'),
  39. Pattern('/bar'),
  40. )
  41. def test_collect_patterns_converts_exclude_patterns():
  42. assert module.collect_patterns({'exclude_patterns': ['/foo', '/bar', 'sh:**/baz']}) == (
  43. Pattern(
  44. '/foo', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH, source=Pattern_source.CONFIG
  45. ),
  46. Pattern(
  47. '/bar', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH, source=Pattern_source.CONFIG
  48. ),
  49. Pattern(
  50. '**/baz', Pattern_type.NO_RECURSE, Pattern_style.SHELL, source=Pattern_source.CONFIG
  51. ),
  52. )
  53. def test_collect_patterns_reads_config_patterns_from_file():
  54. builtins = flexmock(sys.modules['builtins'])
  55. builtins.should_receive('open').with_args('file1.txt').and_return(io.StringIO('R /foo'))
  56. builtins.should_receive('open').with_args('file2.txt').and_return(
  57. io.StringIO('R /bar\n# comment\n\n \nR /baz')
  58. )
  59. flexmock(module).should_receive('parse_pattern').with_args('R /foo').and_return(Pattern('/foo'))
  60. flexmock(module).should_receive('parse_pattern').with_args('# comment').never()
  61. flexmock(module).should_receive('parse_pattern').with_args('').never()
  62. flexmock(module).should_receive('parse_pattern').with_args(' ').never()
  63. flexmock(module).should_receive('parse_pattern').with_args('R /bar').and_return(Pattern('/bar'))
  64. flexmock(module).should_receive('parse_pattern').with_args('R /baz').and_return(Pattern('/baz'))
  65. assert module.collect_patterns({'patterns_from': ['file1.txt', 'file2.txt']}) == (
  66. Pattern('/foo'),
  67. Pattern('/bar'),
  68. Pattern('/baz'),
  69. )
  70. def test_collect_patterns_errors_on_missing_config_patterns_from_file():
  71. builtins = flexmock(sys.modules['builtins'])
  72. builtins.should_receive('open').with_args('file1.txt').and_raise(FileNotFoundError)
  73. flexmock(module).should_receive('parse_pattern').never()
  74. with pytest.raises(ValueError):
  75. module.collect_patterns({'patterns_from': ['file1.txt', 'file2.txt']})
  76. def test_collect_patterns_reads_config_exclude_from_file():
  77. builtins = flexmock(sys.modules['builtins'])
  78. builtins.should_receive('open').with_args('file1.txt').and_return(io.StringIO('/foo'))
  79. builtins.should_receive('open').with_args('file2.txt').and_return(
  80. io.StringIO('/bar\n# comment\n\n \n/baz')
  81. )
  82. flexmock(module).should_receive('parse_pattern').with_args(
  83. '! /foo', default_style=Pattern_style.FNMATCH
  84. ).and_return(Pattern('/foo', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH))
  85. flexmock(module).should_receive('parse_pattern').with_args(
  86. '! /bar', default_style=Pattern_style.FNMATCH
  87. ).and_return(Pattern('/bar', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH))
  88. flexmock(module).should_receive('parse_pattern').with_args('# comment').never()
  89. flexmock(module).should_receive('parse_pattern').with_args('').never()
  90. flexmock(module).should_receive('parse_pattern').with_args(' ').never()
  91. flexmock(module).should_receive('parse_pattern').with_args(
  92. '! /baz', default_style=Pattern_style.FNMATCH
  93. ).and_return(Pattern('/baz', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH))
  94. assert module.collect_patterns({'exclude_from': ['file1.txt', 'file2.txt']}) == (
  95. Pattern('/foo', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH),
  96. Pattern('/bar', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH),
  97. Pattern('/baz', Pattern_type.NO_RECURSE, Pattern_style.FNMATCH),
  98. )
  99. def test_collect_patterns_errors_on_missing_config_exclude_from_file():
  100. builtins = flexmock(sys.modules['builtins'])
  101. builtins.should_receive('open').with_args('file1.txt').and_raise(OSError)
  102. flexmock(module).should_receive('parse_pattern').never()
  103. with pytest.raises(ValueError):
  104. module.collect_patterns({'exclude_from': ['file1.txt', 'file2.txt']})
  105. def test_expand_directory_with_basic_path_passes_it_through():
  106. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  107. flexmock(module.glob).should_receive('glob').and_return([])
  108. paths = module.expand_directory('foo', None)
  109. assert paths == ['foo']
  110. def test_expand_directory_with_glob_expands():
  111. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  112. flexmock(module.glob).should_receive('glob').and_return(['foo', 'food'])
  113. paths = module.expand_directory('foo*', None)
  114. assert paths == ['foo', 'food']
  115. def test_expand_directory_strips_off_working_directory():
  116. flexmock(module.os.path).should_receive('expanduser').and_return('foo')
  117. flexmock(module.glob).should_receive('glob').with_args('/working/dir/foo').and_return([]).once()
  118. paths = module.expand_directory('foo', working_directory='/working/dir')
  119. assert paths == ['foo']
  120. def test_expand_directory_globs_working_directory_and_strips_it_off():
  121. flexmock(module.os.path).should_receive('expanduser').and_return('foo*')
  122. flexmock(module.glob).should_receive('glob').with_args('/working/dir/foo*').and_return(
  123. ['/working/dir/foo', '/working/dir/food']
  124. ).once()
  125. paths = module.expand_directory('foo*', working_directory='/working/dir')
  126. assert paths == ['foo', 'food']
  127. def test_expand_directory_with_slashdot_hack_globs_working_directory_and_strips_it_off():
  128. flexmock(module.os.path).should_receive('expanduser').and_return('./foo*')
  129. flexmock(module.glob).should_receive('glob').with_args('/working/dir/./foo*').and_return(
  130. ['/working/dir/./foo', '/working/dir/./food']
  131. ).once()
  132. paths = module.expand_directory('./foo*', working_directory='/working/dir')
  133. assert paths == ['./foo', './food']
  134. def test_expand_directory_with_working_directory_matching_start_of_directory_does_not_strip_it_off():
  135. flexmock(module.os.path).should_receive('expanduser').and_return('/working/dir/foo')
  136. flexmock(module.glob).should_receive('glob').with_args('/working/dir/foo').and_return(
  137. ['/working/dir/foo']
  138. ).once()
  139. paths = module.expand_directory('/working/dir/foo', working_directory='/working/dir')
  140. assert paths == ['/working/dir/foo']
  141. def test_expand_patterns_flattens_expanded_directories():
  142. flexmock(module).should_receive('expand_directory').with_args('~/foo', None).and_return(
  143. ['/root/foo']
  144. )
  145. flexmock(module).should_receive('expand_directory').with_args('bar*', None).and_return(
  146. ['bar', 'barf']
  147. )
  148. paths = module.expand_patterns((Pattern('~/foo'), Pattern('bar*')))
  149. assert paths == (Pattern('/root/foo'), Pattern('bar'), Pattern('barf'))
  150. def test_expand_patterns_with_working_directory_passes_it_through():
  151. flexmock(module).should_receive('expand_directory').with_args('foo', '/working/dir').and_return(
  152. ['/working/dir/foo']
  153. )
  154. patterns = module.expand_patterns((Pattern('foo'),), working_directory='/working/dir')
  155. assert patterns == (Pattern('/working/dir/foo'),)
  156. def test_expand_patterns_does_not_expand_skip_paths():
  157. flexmock(module).should_receive('expand_directory').with_args('/foo', None).and_return(['/foo'])
  158. flexmock(module).should_receive('expand_directory').with_args('/bar*', None).never()
  159. patterns = module.expand_patterns((Pattern('/foo'), Pattern('/bar*')), skip_paths=('/bar*',))
  160. assert patterns == (Pattern('/foo'), Pattern('/bar*'))
  161. def test_expand_patterns_considers_none_as_no_patterns():
  162. assert module.expand_patterns(None) == ()
  163. def test_expand_patterns_expands_tildes_and_globs_in_root_patterns():
  164. flexmock(module.os.path).should_receive('expanduser').never()
  165. flexmock(module).should_receive('expand_directory').and_return(
  166. ['/root/foo/one', '/root/foo/two']
  167. )
  168. paths = module.expand_patterns((Pattern('~/foo/*'),))
  169. assert paths == (Pattern('/root/foo/one'), Pattern('/root/foo/two'))
  170. def test_expand_patterns_expands_only_tildes_in_non_root_patterns():
  171. flexmock(module).should_receive('expand_directory').never()
  172. flexmock(module.os.path).should_receive('expanduser').and_return('/root/bar/*')
  173. paths = module.expand_patterns((Pattern('~/bar/*', Pattern_type.INCLUDE),))
  174. assert paths == (Pattern('/root/bar/*', Pattern_type.INCLUDE),)
  175. def test_get_existent_path_or_parent_passes_through_existent_path():
  176. flexmock(module.os.path).should_receive('exists').and_return(True)
  177. assert module.get_existent_path_or_parent('/foo/bar/baz') == '/foo/bar/baz'
  178. def test_get_existent_path_or_parent_with_non_existent_path_returns_none():
  179. flexmock(module.os.path).should_receive('exists').and_return(False)
  180. assert module.get_existent_path_or_parent('/foo/bar/baz') is None
  181. def test_get_existent_path_or_parent_with_non_existent_path_returns_existent_parent():
  182. flexmock(module.os.path).should_receive('exists').with_args('/foo/bar/baz*').and_return(False)
  183. flexmock(module.os.path).should_receive('exists').with_args('/foo/bar').and_return(True)
  184. flexmock(module.os.path).should_receive('exists').with_args('/foo').never()
  185. flexmock(module.os.path).should_receive('exists').with_args('/').never()
  186. assert module.get_existent_path_or_parent('/foo/bar/baz*') == '/foo/bar'
  187. def test_get_existent_path_or_parent_with_non_existent_path_returns_existent_grandparent():
  188. flexmock(module.os.path).should_receive('exists').with_args('/foo/bar/baz*').and_return(False)
  189. flexmock(module.os.path).should_receive('exists').with_args('/foo/bar').and_return(False)
  190. flexmock(module.os.path).should_receive('exists').with_args('/foo').and_return(True)
  191. flexmock(module.os.path).should_receive('exists').with_args('/').never()
  192. assert module.get_existent_path_or_parent('/foo/bar/baz*') == '/foo'
  193. def test_get_existent_path_or_parent_with_end_to_end_test_prefix_returns_none():
  194. flexmock(module.os.path).should_receive('exists').never()
  195. assert module.get_existent_path_or_parent('/e2e/foo/bar/baz') is None
  196. def test_device_map_patterns_gives_device_id_per_path():
  197. flexmock(module).should_receive('get_existent_path_or_parent').replace_with(lambda path: path)
  198. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  199. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=66))
  200. device_map = module.device_map_patterns(
  201. (
  202. Pattern('/foo'),
  203. Pattern('^/bar', type=Pattern_type.INCLUDE, style=Pattern_style.REGULAR_EXPRESSION),
  204. )
  205. )
  206. assert device_map == (
  207. Pattern('/foo', device=55),
  208. Pattern(
  209. '^/bar', type=Pattern_type.INCLUDE, style=Pattern_style.REGULAR_EXPRESSION, device=66
  210. ),
  211. )
  212. def test_device_map_patterns_with_missing_path_does_not_error():
  213. flexmock(module).should_receive('get_existent_path_or_parent').with_args('/foo').and_return(
  214. '/foo'
  215. )
  216. flexmock(module).should_receive('get_existent_path_or_parent').with_args('/bar').and_return(
  217. None
  218. )
  219. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  220. flexmock(module.os).should_receive('stat').with_args('/bar').never()
  221. device_map = module.device_map_patterns((Pattern('/foo'), Pattern('/bar')))
  222. assert device_map == (
  223. Pattern('/foo', device=55),
  224. Pattern('/bar'),
  225. )
  226. def test_device_map_patterns_uses_working_directory_to_construct_path():
  227. flexmock(module).should_receive('get_existent_path_or_parent').replace_with(lambda path: path)
  228. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  229. flexmock(module.os).should_receive('stat').with_args('/working/dir/bar').and_return(
  230. flexmock(st_dev=66)
  231. )
  232. device_map = module.device_map_patterns(
  233. (Pattern('/foo'), Pattern('bar')), working_directory='/working/dir'
  234. )
  235. assert device_map == (
  236. Pattern('/foo', device=55),
  237. Pattern('bar', device=66),
  238. )
  239. def test_device_map_patterns_with_existing_device_id_does_not_overwrite_it():
  240. flexmock(module).should_receive('get_existent_path_or_parent').replace_with(lambda path: path)
  241. flexmock(module.os).should_receive('stat').with_args('/foo').and_return(flexmock(st_dev=55))
  242. flexmock(module.os).should_receive('stat').with_args('/bar').and_return(flexmock(st_dev=100))
  243. device_map = module.device_map_patterns((Pattern('/foo'), Pattern('/bar', device=66)))
  244. assert device_map == (
  245. Pattern('/foo', device=55),
  246. Pattern('/bar', device=66),
  247. )
  248. @pytest.mark.parametrize(
  249. 'patterns,expected_patterns',
  250. (
  251. ((Pattern('/', device=1), Pattern('/root', device=1)), (Pattern('/', device=1),)),
  252. ((Pattern('/', device=1), Pattern('/root/', device=1)), (Pattern('/', device=1),)),
  253. (
  254. (Pattern('/', device=1), Pattern('/root', device=2)),
  255. (Pattern('/', device=1), Pattern('/root', device=2)),
  256. ),
  257. ((Pattern('/root', device=1), Pattern('/', device=1)), (Pattern('/', device=1),)),
  258. (
  259. (Pattern('/root', device=1), Pattern('/root/foo', device=1)),
  260. (Pattern('/root', device=1),),
  261. ),
  262. (
  263. (Pattern('/root/', device=1), Pattern('/root/foo', device=1)),
  264. (Pattern('/root/', device=1),),
  265. ),
  266. (
  267. (Pattern('/root', device=1), Pattern('/root/foo/', device=1)),
  268. (Pattern('/root', device=1),),
  269. ),
  270. (
  271. (Pattern('/root', device=1), Pattern('/root/foo', device=2)),
  272. (Pattern('/root', device=1), Pattern('/root/foo', device=2)),
  273. ),
  274. (
  275. (Pattern('/root/foo', device=1), Pattern('/root', device=1)),
  276. (Pattern('/root', device=1),),
  277. ),
  278. (
  279. (Pattern('/root', device=None), Pattern('/root/foo', device=None)),
  280. (Pattern('/root'), Pattern('/root/foo')),
  281. ),
  282. (
  283. (
  284. Pattern('/root', device=1),
  285. Pattern('/etc', device=1),
  286. Pattern('/root/foo/bar', device=1),
  287. ),
  288. (Pattern('/root', device=1), Pattern('/etc', device=1)),
  289. ),
  290. (
  291. (
  292. Pattern('/root', device=1),
  293. Pattern('/root/foo', device=1),
  294. Pattern('/root/foo/bar', device=1),
  295. ),
  296. (Pattern('/root', device=1),),
  297. ),
  298. ((Pattern('/dup', device=1), Pattern('/dup', device=1)), (Pattern('/dup', device=1),)),
  299. (
  300. (Pattern('/foo', device=1), Pattern('/bar', device=1)),
  301. (Pattern('/foo', device=1), Pattern('/bar', device=1)),
  302. ),
  303. (
  304. (Pattern('/foo', device=1), Pattern('/bar', device=2)),
  305. (Pattern('/foo', device=1), Pattern('/bar', device=2)),
  306. ),
  307. ((Pattern('/root/foo', device=1),), (Pattern('/root/foo', device=1),)),
  308. (
  309. (Pattern('/', device=1), Pattern('/root', Pattern_type.INCLUDE, device=1)),
  310. (Pattern('/', device=1), Pattern('/root', Pattern_type.INCLUDE, device=1)),
  311. ),
  312. (
  313. (Pattern('/root', Pattern_type.INCLUDE, device=1), Pattern('/', device=1)),
  314. (Pattern('/root', Pattern_type.INCLUDE, device=1), Pattern('/', device=1)),
  315. ),
  316. ),
  317. )
  318. def test_deduplicate_patterns_omits_child_paths_on_the_same_filesystem(patterns, expected_patterns):
  319. assert module.deduplicate_patterns(patterns) == expected_patterns
  320. def test_process_patterns_includes_patterns():
  321. flexmock(module).should_receive('deduplicate_patterns').and_return(
  322. (Pattern('foo'), Pattern('bar'))
  323. )
  324. flexmock(module).should_receive('device_map_patterns').and_return({})
  325. flexmock(module).should_receive('expand_patterns').with_args(
  326. (Pattern('foo'), Pattern('bar')),
  327. working_directory='/working',
  328. skip_paths=set(),
  329. ).and_return(()).once()
  330. assert module.process_patterns(
  331. (Pattern('foo'), Pattern('bar')),
  332. working_directory='/working',
  333. ) == [Pattern('foo'), Pattern('bar')]
  334. def test_process_patterns_skips_expand_for_requested_paths():
  335. skip_paths = {flexmock()}
  336. flexmock(module).should_receive('deduplicate_patterns').and_return(
  337. (Pattern('foo'), Pattern('bar'))
  338. )
  339. flexmock(module).should_receive('device_map_patterns').and_return({})
  340. flexmock(module).should_receive('expand_patterns').with_args(
  341. (Pattern('foo'), Pattern('bar')),
  342. working_directory='/working',
  343. skip_paths=skip_paths,
  344. ).and_return(()).once()
  345. assert module.process_patterns(
  346. (Pattern('foo'), Pattern('bar')),
  347. working_directory='/working',
  348. skip_expand_paths=skip_paths,
  349. ) == [Pattern('foo'), Pattern('bar')]