From dc35d20358179e79d20f1fac0368eefd7d657e9d Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Wed, 21 May 2014 01:02:05 -0700 Subject: [PATCH] Use json equality rather than raw text It turns out sometimes json will serialize differently given the same python structure. A check of the two parsed data structures will detect changes more reliably. Change-Id: Id165b36c0fa0fa89730c3507444a41c68bd70fb3 Closes-Bug: #1320262 --- os_collect_config/cache.py | 17 +++++++---------- os_collect_config/tests/test_cache.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/os_collect_config/cache.py b/os_collect_config/cache.py index 689600a..9a287f0 100644 --- a/os_collect_config/cache.py +++ b/os_collect_config/cache.py @@ -57,16 +57,13 @@ def store(name, content): os.rename(new.name, dest_path) if not changed: - with open(dest_path) as now: - if os.path.exists(last_path): - with open(last_path) as then: - for now_line in now: - then_line = then.next() - if then_line != now_line: - changed = True - break - else: - changed = True + if os.path.exists(last_path): + with open(last_path) as then: + then_value = json.load(then) + if then_value != content: + changed = True + else: + changed = True return (changed, dest_path) diff --git a/os_collect_config/tests/test_cache.py b/os_collect_config/tests/test_cache.py index 463c18f..594d5d7 100644 --- a/os_collect_config/tests/test_cache.py +++ b/os_collect_config/tests/test_cache.py @@ -83,5 +83,19 @@ class TestCache(testtools.TestCase): self.assertThat(list_list, matchers.IsInstance(list)) self.assertIn(path, list_list) + def test_cache_ignores_json_inequality(self): + content1 = u'{"a": "value-a", "b": "value-b"}' + content2 = u'{"b": "value-b", "a": "value-a"}' + value1 = json.loads(content1) + value2 = json.loads(content2) + self.assertEqual(value1, value2) + (changed, path) = cache.store('content', value1) + self.assertTrue(changed) + cache.commit('content') + (changed, path) = cache.store('content', value1) + self.assertFalse(changed) + (changed, path) = cache.store('content', value2) + self.assertFalse(changed) + def test_commit_no_cache(self): self.assertEqual(None, cache.commit('neversaved'))