]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
not really very happy about this
authorKevin McCarthy <me@kevinmccarthy.org>
Wed, 7 May 2014 06:16:56 +0000 (20:16 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sat, 10 May 2014 21:52:35 +0000 (11:52 -1000)
tests/unit/test_cassettes.py
tests/unit/test_migration.py
vcr/migration.py
vcr/serialize.py

index 1c5b84ca331733cfd9c2866372ee9751dc73e303..4c4818fa1b2f2a091f30812ec90cc2f6ebc16aaa 100644 (file)
@@ -7,10 +7,10 @@ from vcr.errors import UnhandledHTTPRequestError
 
 def test_cassette_load(tmpdir):
     a_file = tmpdir.join('test_cassette.yml')
-    a_file.write(yaml.dump([
+    a_file.write(yaml.dump({'interactions': [
         {'request': {'body': '', 'uri': 'foo', 'method': 'GET', 'headers': {}},
          'response': 'bar'}
-    ]))
+    ]}))
     a_cassette = Cassette.load(str(a_file))
     assert len(a_cassette) == 1
 
index 5f2472c71fb20e6b816215f93200b5498ded88af..7638910e9d6b9e4046cc529cc632e61a4405f904 100644 (file)
@@ -7,7 +7,7 @@ import vcr.migration
 
 
 def test_try_migrate_with_json(tmpdir):
-    cassette = tmpdir.join('cassette').strpath
+    cassette = tmpdir.join('cassette.json').strpath
     shutil.copy('tests/fixtures/migration/old_cassette.json', cassette)
     assert vcr.migration.try_migrate(cassette)
     with open('tests/fixtures/migration/new_cassette.json', 'r') as f:
@@ -18,7 +18,7 @@ def test_try_migrate_with_json(tmpdir):
 
 
 def test_try_migrate_with_yaml(tmpdir):
-    cassette = tmpdir.join('cassette').strpath
+    cassette = tmpdir.join('cassette.yaml').strpath
     shutil.copy('tests/fixtures/migration/old_cassette.yaml', cassette)
     assert vcr.migration.try_migrate(cassette)
     with open('tests/fixtures/migration/new_cassette.yaml', 'r') as f:
index e4c7d3f1964f623957f4ec0c9cda9ae005bc5474..e8ea73a6c18e417d9075ea578b0fdbfa8217567e 100644 (file)
@@ -48,6 +48,7 @@ def build_uri(**parts):
 
 def migrate_json(in_fp, out_fp):
     data = json.load(in_fp)
+    interactions = []
     for item in data:
         req = item['request']
         res = item['response']
@@ -62,9 +63,10 @@ def migrate_json(in_fp, out_fp):
             response_headers.setdefault(k, [])
             response_headers[k].append(v)
         res['headers'] = response_headers
+        interactions.append({'request':req, 'response': res})
 
 
-    json.dump(data, out_fp, indent=4)
+    json.dump({'interactions':interactions, 'version':1}, out_fp, indent=4)
 
 
 def _restore_frozenset():
@@ -93,6 +95,7 @@ def _old_deserialize(cassette_string):
 
 def migrate_yml(in_fp, out_fp):
     (requests, responses) = _old_deserialize(in_fp.read())
+    interactions = []
     for req, res in zip(requests, responses):
         if not isinstance(req, request.Request):
             raise Exception("already migrated")
@@ -114,10 +117,11 @@ def migrate_yml(in_fp, out_fp):
                 response_headers.setdefault(k, [])
                 response_headers[k].append(v)
             res['headers'] = response_headers
+        interactions.append({'request': req._to_dict(), 'response': res})
 
     data = yamlserializer.serialize({
-        "requests": requests,
-        "responses": responses,
+        "interactions": interactions,
+        "version": 1
     })
     out_fp.write(data)
 
@@ -134,14 +138,13 @@ def migrate(file_path, migration_fn):
 
 
 def try_migrate(path):
-    try:  # try to migrate as json
+    if path.endswith('.json'):
         migrate(path, migrate_json)
-    except Exception as e:  # probably the file is not a json
-        try:  # let's try to migrate as yaml
-            migrate(path, migrate_yml)
-        except Exception:  # oops probably the file is not a cassette
-            return False
-    return True
+        return True
+    elif path.endswith('.yaml') or path.endswith('.yml'):
+        migrate(path, migrate_yml)
+        return True
+    return False
 
 
 def main():
index 36275b4a0fa109c09789e3e75b73c9a8e296e624..ac1754ca26de98a134323ea61cecd5a4cdca9a18 100644 (file)
@@ -1,5 +1,6 @@
 from vcr.serializers import compat
 from vcr.request import Request
+import yaml
 
 #version 1 cassettes started with VCR 1.0.x.  Before 1.0.x, there was no versioning.
 CASSETTE_FORMAT_VERSION = 1 
@@ -16,15 +17,25 @@ Serializing: bytestring -> string (yaml persists to utf-8)
 Deserializing: string (yaml converts from utf-8) -> bytestring
 """
 
+def _looks_like_an_old_cassette(data):
+    return isinstance(data, list) and len(data) and 'request' in data[0]
+
+def _warn_about_old_cassette_format(data):
+    raise ValueError(
+        "Your cassette files were generated in an older version "
+        "of VCR. Delete your cassettes or run the migration script."
+        "See http://git.io/mHhLBg for more details."
+    )
 
 def deserialize(cassette_string, serializer):
-    data = serializer.deserialize(cassette_string)
-    if not isinstance(data, dict):
-        raise ValueError(
-            "Your cassette files were generated in an older version "
-            "of VCR. Delete your cassettes or run the migration script."
-            "See http://git.io/mHhLBg for more details."
-        )
+    try:
+        data = serializer.deserialize(cassette_string)
+    # Old cassettes used to use yaml object thingy so I have to
+    # check for some fairly stupid exceptions here
+    except (ImportError, yaml.constructor.ConstructorError):
+        _warn_about_old_cassette_format(data)
+    if _looks_like_an_old_cassette(data):
+        _warn_about_old_cassette_format(data)
 
     requests = [Request._from_dict(r['request']) for r in data['interactions']]
     responses = [compat.convert_to_bytes(r['response']) for r in data['interactions']]