D4600: phabricator: add support for using the vcr library to mock interactions

Yuya Nishihara yuya at tcha.org
Sat Sep 15 03:11:53 EDT 2018


Queued the series, thanks.

> +_VCR_FLAGS = [
> +    (b'', b'test-vcr', b'',
> +     _(b'Path to a vcr file. If nonexistent, will record a new vcr transcript'
> +       b', otherwise will mock all http requests using the specified vcr file.'
> +       b' (DEPRECATED)'

s/(DEPRECATED)/(ADVANCED)/ in flight.

> +def vcrcommand(name, flags, spec):
> +    fullflags = flags + _VCR_FLAGS
> +    def decorate(fn):
> +        def inner(*args, **kwargs):
> +            cassette = kwargs.pop(r'test_vcr', None)
> +            if cassette:
> +                import hgdemandimport
> +                with hgdemandimport.deactivated():
> +                    import vcr as vcrmod
> +                    from vcr import stubs

Fixed check-module-imports warning in flight.

> +                vcr = vcrmod.VCR(
> +                    serializer=r'json',
> +                    custom_patches=[
> +                        (urlmod, 'httpconnection', stubs.VCRHTTPConnection),
> +                        (urlmod, 'httpsconnection', stubs.VCRHTTPSConnection),
> +                    ])
> +                with vcr.use_cassette(cassette):
> +                    return fn(*args, **kwargs)
> +            return fn(*args, **kwargs)
> +        inner.__name__ = fn.__name__
> +        return command(name, fullflags, spec)(inner)
> +    return decorate

Perhaps, it's easier to extend the registrar.command class instead. Another
option is to turn --test-vcr into a config knob, and hook somewhere in
dispatch.


More information about the Mercurial-devel mailing list