# -*- coding: utf-8 -*-
import os

import pytest
from vcr import VCR


def pytest_addoption(parser):
    group = parser.getgroup('vcr')
    group.addoption(
        '--vcr-record',
        action='store',
        dest='vcr_record',
        default=None,
        choices=['once', 'new_episodes', 'none', 'all'],
        help='Set the recording mode for VCR.py.'
    )
    # TODO: deprecated, remove in a future release
    group.addoption(
        '--vcr-record-mode',
        action='store',
        dest='vcr_record',
        default=None,
        choices=['once', 'new_episodes', 'none', 'all'],
        help='DEPRECATED: use --vcr-record'
    )
    group.addoption(
        '--disable-vcr',
        action='store_true',
        dest='disable_vcr',
        help='Run tests without playing back from VCR.py cassettes'
    )


def pytest_load_initial_conftests(early_config, parser, args):
    early_config.addinivalue_line(
        'markers',
        'vcr: Mark the test as using VCR.py.')


@pytest.fixture(autouse=True)
def _vcr_marker(request):
    marker = request.node.get_closest_marker('vcr')
    if marker:
        request.getfixturevalue('vcr_cassette')


def _update_kwargs(request, kwargs):
    marker = request.node.get_closest_marker('vcr')
    if marker:
        kwargs.update(marker.kwargs)

    record_mode = request.config.getoption('--vcr-record-mode')
    record_mode = request.config.getoption('--vcr-record') or record_mode
    if record_mode:
        kwargs['record_mode'] = record_mode

    if request.config.getoption('--disable-vcr'):
        # Set mode to record but discard all responses to disable both recording and playback
        kwargs['record_mode'] = 'new_episodes'
        kwargs['before_record_response'] = lambda *args, **kwargs: None


@pytest.fixture(scope='module')
def vcr(request, pytestconfig, vcr_config, vcr_cassette_dir, ):
    """The VCR instance"""
    if request.config.getoption('--vcr-record-mode'):
        pytestconfig.warn("C1",
                          "--vcr-record-mode has been deprecated and will be removed in a future "
                          "release. Use --vcr-record instead.")
    kwargs = dict(
        cassette_library_dir=vcr_cassette_dir,
        path_transformer=VCR.ensure_suffix(".yaml"),
    )
    kwargs.update(vcr_config)
    _update_kwargs(request, kwargs)
    vcr = VCR(**kwargs)
    return vcr


@pytest.fixture
def vcr_cassette(request, vcr, vcr_cassette_name):
    """Wrap a test in a VCR.py cassette"""
    kwargs = {}
    _update_kwargs(request, kwargs)
    with vcr.use_cassette(vcr_cassette_name, **kwargs) as cassette:
        yield cassette


@pytest.fixture(scope='module')
def vcr_cassette_dir(request):
    test_dir = request.node.fspath.dirname
    return os.path.join(test_dir, 'cassettes')


@pytest.fixture
def vcr_cassette_name(request):
    """Name of the VCR cassette"""
    f = request.function
    if hasattr(f, '__self__'):
        return f.__self__.__class__.__name__ + '.' + request.node.name
    return request.node.name


@pytest.fixture(scope='module')
def vcr_config():
    """Custom configuration for VCR.py"""
    return {}

@@ -26,16 +26,32 @@
 mport os
+%0Aimport warnings
 %0A%0Aimport
@@ -507,32 +507,43 @@
 ,%0A        dest='
+deprecated_
 vcr_record',%0A   
@@ -1848,22 +1848,8 @@
 est,
- pytestconfig,
  vcr
@@ -1878,16 +1878,16 @@
 dir, ):%0A
+
     %22%22%22T
@@ -1971,58 +1971,22 @@
     
-pytestconfig.warn(%22C1%22,%0A                          
+warnings.warn(
 %22--v
@@ -2076,20 +2076,16 @@
         
-    
 %22release
@@ -2112,16 +2112,58 @@
 nstead.%22
+,%0A                      DeprecationWarning
 )%0A    kw

Fix --vcr-record-mode deprecation warning