diff --git a/tests/integration/test_basic.py b/tests/integration/test_basic.py index 250eea6..d7bdb09 100644 --- a/tests/integration/test_basic.py +++ b/tests/integration/test_basic.py @@ -51,11 +51,12 @@ def test_patched_content(tmpdir): assert cass.play_count == 0 with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass: - response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read() + response2 = urllib2.urlopen('http://www.iana.org/domains/reserved').read() assert cass.play_count == 1 + cass._save(force=True) with vcr.use_cassette(str(tmpdir.join('synopsis.yaml'))) as cass: - response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read() + response3 = urllib2.urlopen('http://www.iana.org/domains/reserved').read() assert cass.play_count == 1 assert response == response2 @@ -68,11 +69,12 @@ def test_patched_content_json(tmpdir): assert cass.play_count == 0 with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass: - response2= urllib2.urlopen('http://www.iana.org/domains/reserved').read() + response2 = urllib2.urlopen('http://www.iana.org/domains/reserved').read() assert cass.play_count == 1 + cass._save(force=True) with vcr.use_cassette(str(tmpdir.join('synopsis.json')), serializer='json') as cass: - response3= urllib2.urlopen('http://www.iana.org/domains/reserved').read() + response3 = urllib2.urlopen('http://www.iana.org/domains/reserved').read() assert cass.play_count == 1 assert response == response2 diff --git a/tests/integration/test_disksaver.py b/tests/integration/test_disksaver.py new file mode 100644 index 0000000..bc19fc1 --- /dev/null +++ b/tests/integration/test_disksaver.py @@ -0,0 +1,43 @@ +'''Basic tests about save behavior''' +# coding=utf-8 + +# External imports +import os +import urllib2 + +# Internal imports +import vcr + +def test_disk_saver_nowrite(tmpdir): + '''Ensure that when you close a cassette without changing it it doesn't rewrite the file''' + fname = str(tmpdir.join('synopsis.yaml')) + with vcr.use_cassette(fname) as cass: + urllib2.urlopen('http://www.iana.org/domains/reserved').read() + assert cass.play_count == 0 + last_mod = os.path.getmtime(fname) + + with vcr.use_cassette(fname) as cass: + urllib2.urlopen('http://www.iana.org/domains/reserved').read() + assert cass.play_count == 1 + assert cass.dirty == False + last_mod2 = os.path.getmtime(fname) + + assert last_mod == last_mod2 + +def test_disk_saver_write(tmpdir): + '''Ensure that when you close a cassette with changing it it does rewrite the file''' + fname = str(tmpdir.join('synopsis.yaml')) + with vcr.use_cassette(fname) as cass: + urllib2.urlopen('http://www.iana.org/domains/reserved').read() + assert cass.play_count == 0 + last_mod = os.path.getmtime(fname) + + with vcr.use_cassette(fname) as cass: + urllib2.urlopen('http://www.iana.org/domains/reserved').read() + urllib2.urlopen('http://httpbin.org/').read() + assert cass.play_count == 1 + assert cass.dirty + last_mod2 = os.path.getmtime(fname) + + assert last_mod != last_mod2 + diff --git a/vcr/cassette.py b/vcr/cassette.py index 87c9ed3..84acc80 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -25,6 +25,7 @@ class Cassette(object): self._serializer = serializer self.data = OrderedDict() self.play_counts = Counter() + self.dirty = False @property def play_count(self): @@ -47,22 +48,26 @@ class Cassette(object): def append(self, request, response): '''Add a request, response pair to this cassette''' self.data[request] = response + self.dirty = True def response_of(self, request): '''Find the response corresponding to a request''' return self.data[request] - def _save(self): - save_cassette(self._path, self._as_dict(), serializer=self._serializer) - def _as_dict(self): return {"requests": self.requests, "responses": self.responses} + def _save(self, force=False): + if force or self.dirty: + save_cassette(self._path, self._as_dict(), serializer=self._serializer) + self.dirty = False + def _load(self): try: requests, responses = load_cassette(self._path, serializer=self._serializer) for request, response in zip(requests, responses): self.append(request, response) + self.dirty = False except IOError: pass