Merge pull request #359 from untitaker/strip-metadata-from-whitespace

metasync: Strip whitespace from values
This commit is contained in:
Markus Unterwaditzer 2016-03-04 15:06:24 +01:00
commit e04511f348
3 changed files with 30 additions and 2 deletions

View file

@ -15,6 +15,8 @@ Version 0.9.1
- Removed leftover debug print statement in ``vdirsyncer discover``, see commit
``3d856749f37639821b148238ef35f1acba82db36``.
- ``metasync`` will now strip whitespace from the start and the end of the
values. See :gh:`358`.
Version 0.9.0
=============

View file

@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-
from hypothesis import given
import hypothesis.strategies as st
import pytest
from vdirsyncer.metasync import MetaSyncConflict, metasync
@ -82,3 +85,17 @@ def test_conflict_x_wins(wins):
assert a.get_meta('foo') == b.get_meta('foo') == status['foo'] == (
'bar' if wins == 'a' else 'baz'
)
@given(s=st.text().filter(lambda x: x.strip() == x), key=st.text())
def test_trailing_newline(s, key):
a = MemoryStorage()
b = MemoryStorage()
status = {}
a.set_meta(key, s + u'\n')
b.set_meta(key, s)
a.set_meta = b.set_meta = blow_up
metasync(a, b, status, keys=[key])
assert status[key] == s

View file

@ -1,4 +1,5 @@
from . import exceptions, log
from .utils.compat import text_type
logger = log.get(__name__)
@ -33,8 +34,8 @@ def metasync(storage_a, storage_b, status, keys, conflict_resolution=None):
_b_to_a()
for key in keys:
a = storage_a.get_meta(key)
b = storage_b.get_meta(key)
a = _normalize_value(storage_a.get_meta(key))
b = _normalize_value(storage_b.get_meta(key))
s = status.get(key)
logger.debug(u'Key: {}'.format(key))
logger.debug(u'A: {}'.format(a))
@ -52,3 +53,11 @@ def metasync(storage_a, storage_b, status, keys, conflict_resolution=None):
for key in set(status) - set(keys):
del status[key]
def _normalize_value(value):
if value is None:
return value
else:
assert isinstance(value, text_type)
return value.strip()