test_flags.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.borg import flags as module
  4. def test_make_flags_formats_string_value():
  5. assert module.make_flags('foo', 'bar') == ('--foo', 'bar')
  6. def test_make_flags_formats_integer_value():
  7. assert module.make_flags('foo', 3) == ('--foo', '3')
  8. def test_make_flags_formats_true_value():
  9. assert module.make_flags('foo', True) == ('--foo',)
  10. def test_make_flags_omits_false_value():
  11. assert module.make_flags('foo', False) == ()
  12. def test_make_flags_formats_name_with_underscore():
  13. assert module.make_flags('posix_me_harder', 'okay') == ('--posix-me-harder', 'okay')
  14. def test_make_flags_from_arguments_flattens_and_sorts_multiple_arguments():
  15. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  16. flexmock(module).should_receive('make_flags').with_args('baz', 'quux').and_return(
  17. ('baz', 'quux')
  18. )
  19. arguments = flexmock(foo='bar', baz='quux')
  20. assert module.make_flags_from_arguments(arguments) == ('baz', 'quux', 'foo', 'bar')
  21. def test_make_flags_from_arguments_excludes_underscored_argument_names():
  22. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  23. arguments = flexmock(foo='bar', _baz='quux')
  24. assert module.make_flags_from_arguments(arguments) == ('foo', 'bar')
  25. def test_make_flags_from_arguments_omits_excludes():
  26. flexmock(module).should_receive('make_flags').with_args('foo', 'bar').and_return(('foo', 'bar'))
  27. arguments = flexmock(foo='bar', baz='quux')
  28. assert module.make_flags_from_arguments(arguments, excludes=('baz', 'other')) == ('foo', 'bar')
  29. def test_make_repository_flags_with_borg_features_includes_repo_flag():
  30. flexmock(module.feature).should_receive('available').and_return(True)
  31. assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
  32. '--repo',
  33. 'repo',
  34. )
  35. def test_make_repository_flags_without_borg_features_includes_omits_flag():
  36. flexmock(module.feature).should_receive('available').and_return(False)
  37. assert module.make_repository_flags(repository_path='repo', local_borg_version='1.2.3') == (
  38. 'repo',
  39. )
  40. def test_make_repository_archive_flags_with_borg_features_separates_repository_and_archive():
  41. flexmock(module.feature).should_receive('available').and_return(True)
  42. assert module.make_repository_archive_flags(
  43. repository_path='repo', archive='archive', local_borg_version='1.2.3'
  44. ) == ('--repo', 'repo', 'archive',)
  45. def test_make_repository_archive_flags_with_borg_features_joins_repository_and_archive():
  46. flexmock(module.feature).should_receive('available').and_return(False)
  47. assert module.make_repository_archive_flags(
  48. repository_path='repo', archive='archive', local_borg_version='1.2.3'
  49. ) == ('repo::archive',)
  50. @pytest.mark.parametrize(
  51. 'archive_name_format,feature_available,expected_result',
  52. (
  53. (None, True, ()),
  54. ('', True, ()),
  55. (
  56. '{hostname}-docs-{now}', # noqa: FS003
  57. True,
  58. ('--match-archives', 'sh:{hostname}-docs-*'), # noqa: FS003
  59. ),
  60. ('{utcnow}-docs-{user}', True, ('--match-archives', 'sh:*-docs-{user}')), # noqa: FS003
  61. ('{fqdn}-{pid}', True, ('--match-archives', 'sh:{fqdn}-*')), # noqa: FS003
  62. (
  63. 'stuff-{now:%Y-%m-%dT%H:%M:%S.%f}', # noqa: FS003
  64. True,
  65. ('--match-archives', 'sh:stuff-*'),
  66. ),
  67. ('{hostname}-docs-{now}', False, ('--glob-archives', '{hostname}-docs-*')), # noqa: FS003
  68. ('{utcnow}-docs-{user}', False, ('--glob-archives', '*-docs-{user}')), # noqa: FS003
  69. ),
  70. )
  71. def test_make_match_archives_flags_makes_flags_with_globs(
  72. archive_name_format, feature_available, expected_result
  73. ):
  74. flexmock(module.feature).should_receive('available').and_return(feature_available)
  75. assert (
  76. module.make_match_archives_flags(archive_name_format, local_borg_version=flexmock())
  77. == expected_result
  78. )