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

This is an archive of the discontinued Mercurial Phabricator instance.

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

Authored by durin42 on Sep 15 2018, 1:23 AM.

Details

Summary

I'll use this in an upcoming test. The decorator dancing in this is
more complicated than I'd like, but it beats repeating all this code
everywhere.

Diff Detail

Repository
rHG Mercurial
Lint
Lint Skipped
Unit
Unit Tests Skipped

Event Timeline

durin42 created this revision.Sep 15 2018, 1:23 AM
yuja added a subscriber: yuja.Sep 15 2018, 3:12 AM

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.

This revision was automatically updated to reflect the committed changes.