From 7b7b98b5c9cfdf217e06a9cdd4acf33d500ffcf4 Mon Sep 17 00:00:00 2001 From: b5 Date: Tue, 10 Sep 2019 08:26:34 -0400 Subject: [PATCH] feat(mock registry): add support for mock registry config set registry.location to "mock" and qri will create a mock registry, we'll use this in tests elsewhere. Long term it'd be nice to not have to bundle testing code with our production binary, but the overhead of maintaining a testing-oriented binary is a little too much at the moment. --- lib/lib.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/lib.go b/lib/lib.go index 8603eb3bd..c4d11ec80 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -28,6 +28,7 @@ import ( "github.com/qri-io/qri/config/migrate" "github.com/qri-io/qri/p2p" "github.com/qri-io/qri/registry/regclient" + regmock "github.com/qri-io/qri/registry/regserver/mock" "github.com/qri-io/qri/remote" "github.com/qri-io/qri/repo" fsrepo "github.com/qri-io/qri/repo/fs" @@ -316,7 +317,7 @@ func NewInstance(ctx context.Context, repoPath string, opts ...Option) (qri *Ins } if inst.registry == nil { - inst.registry = newRegClient(cfg) + inst.registry = newRegClient(ctx, cfg) } if o.repo != nil { @@ -429,13 +430,27 @@ func newStore(ctx context.Context, cfg *config.Config) (store cafs.Filestore, er } } -func newRegClient(cfg *config.Config) (rc *regclient.Client) { - if cfg.Registry != nil && cfg.Registry.Location != "" { - rc = regclient.NewClient(®client.Config{ - Location: cfg.Registry.Location, - }) +func newRegClient(ctx context.Context, cfg *config.Config) (rc *regclient.Client) { + if cfg.Registry != nil { + switch cfg.Registry.Location { + case "mock": + cli, server := regmock.NewMockServerRegistry(regmock.NewMemRegistry()) + log.Infof("mock registry serving at: '%s'", server.URL) + go func() { + <-ctx.Done() + server.Close() + }() + return cli + case "": + return rc + default: + return regclient.NewClient(®client.Config{ + Location: cfg.Registry.Location, + }) + } } - return + + return nil } func newRepo(path string, cfg *config.Config, store cafs.Filestore) (r repo.Repo, err error) {