2
0

test_constants.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import constants as module
  4. @pytest.mark.parametrize(
  5. 'value,expected_value',
  6. (
  7. ('3', 3),
  8. ('0', 0),
  9. ('-3', -3),
  10. ('1234', 1234),
  11. ('true', True),
  12. ('True', True),
  13. ('false', False),
  14. ('False', False),
  15. ('thing', 'thing'),
  16. ({}, {}),
  17. ({'foo': 'bar'}, {'foo': 'bar'}),
  18. ([], []),
  19. (['foo', 'bar'], ['foo', 'bar']),
  20. ),
  21. )
  22. def test_coerce_scalar_converts_value(value, expected_value):
  23. assert module.coerce_scalar(value) == expected_value
  24. def test_apply_constants_with_empty_constants_passes_through_value():
  25. assert module.apply_constants(value='thing', constants={}) == 'thing'
  26. @pytest.mark.parametrize(
  27. 'value,expected_value',
  28. (
  29. (None, None),
  30. ('thing', 'thing'),
  31. ('{foo}', 'bar'),
  32. ('abc{foo}', 'abcbar'),
  33. ('{foo}xyz', 'barxyz'),
  34. ('{foo}{baz}', 'barquux'),
  35. ('{int}', '3'),
  36. ('{bool}', 'True'),
  37. (['thing', 'other'], ['thing', 'other']),
  38. (['thing', '{foo}'], ['thing', 'bar']),
  39. (['{foo}', '{baz}'], ['bar', 'quux']),
  40. ({'key': 'value'}, {'key': 'value'}),
  41. ({'key': '{foo}'}, {'key': 'bar'}),
  42. ({'key': '{inject}'}, {'key': 'echo hi; naughty-command'}),
  43. ({'before_backup': '{inject}'}, {'before_backup': "'echo hi; naughty-command'"}),
  44. ({'after_backup': '{inject}'}, {'after_backup': "'echo hi; naughty-command'"}),
  45. ({'on_error': '{inject}'}, {'on_error': "'echo hi; naughty-command'"}),
  46. (3, 3),
  47. (True, True),
  48. (False, False),
  49. ),
  50. )
  51. def test_apply_constants_makes_string_substitutions(value, expected_value):
  52. flexmock(module).should_receive('coerce_scalar').replace_with(lambda value: value)
  53. constants = {
  54. 'foo': 'bar',
  55. 'baz': 'quux',
  56. 'int': 3,
  57. 'bool': True,
  58. 'inject': 'echo hi; naughty-command',
  59. }
  60. assert module.apply_constants(value, constants) == expected_value