github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/cmd/factory_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"context"
     5  	"net/rpc"
     6  
     7  	"github.com/qri-io/ioes"
     8  	"github.com/qri-io/qri/config"
     9  	testcfg "github.com/qri-io/qri/config/test"
    10  	"github.com/qri-io/qri/event"
    11  	"github.com/qri-io/qri/lib"
    12  	qhttp "github.com/qri-io/qri/lib/http"
    13  	"github.com/qri-io/qri/p2p"
    14  	"github.com/qri-io/qri/repo"
    15  	"github.com/qri-io/qri/repo/test"
    16  	repotest "github.com/qri-io/qri/repo/test"
    17  )
    18  
    19  // TestFactory is an implementation of the Factory interface for testing purposes
    20  type TestFactory struct {
    21  	ioes.IOStreams
    22  	// path to qri data directory
    23  	repoPath string
    24  	ctors    Constructors
    25  
    26  	inst *lib.Instance
    27  	// Configuration object
    28  	config *config.Config
    29  	node   *p2p.QriNode
    30  	repo   repo.Repo
    31  	rpc    *rpc.Client
    32  }
    33  
    34  // NewTestFactory creates TestFactory object with an in memory test repo
    35  // with an optional registry client. In tests users can create mock registry
    36  // servers and pass in a client connected to that mock, or omit the registry
    37  // client entirely for testing without a designated registry
    38  func NewTestFactory(ctx context.Context) (tf TestFactory, err error) {
    39  	repo, err := test.NewTestRepo()
    40  	if err != nil {
    41  		return
    42  	}
    43  
    44  	cfg := testcfg.DefaultConfigForTesting().Copy()
    45  	tnode, err := p2p.NewTestableQriNode(repo, cfg.P2P, event.NilBus)
    46  	if err != nil {
    47  		return
    48  	}
    49  
    50  	return TestFactory{
    51  		IOStreams: ioes.NewDiscardIOStreams(),
    52  		ctors: Constructors{
    53  			CryptoGenerator: repotest.NewTestCrypto(),
    54  			InitIPFS:        repotest.InitIPFSRepo,
    55  		},
    56  
    57  		repo:   repo,
    58  		rpc:    nil,
    59  		config: cfg,
    60  		node:   tnode.(*p2p.QriNode),
    61  		inst:   lib.NewInstanceFromConfigAndNode(ctx, cfg, tnode.(*p2p.QriNode)),
    62  	}, nil
    63  }
    64  
    65  // NewTestFactoryInstanceOptions is an experimental test factory that allows
    66  // instance configuration overrides
    67  // TODO (b5) - I'm not confident this works perfectly at the moment. Let's add
    68  // more tests to lib.NewInstance before using everywhere
    69  func NewTestFactoryInstanceOptions(ctx context.Context, repoPath string, opts ...lib.Option) (tf TestFactory, err error) {
    70  	repo, err := test.NewTestRepo()
    71  	if err != nil {
    72  		return
    73  	}
    74  
    75  	cfg := testcfg.DefaultConfigForTesting().Copy()
    76  	tnode, err := p2p.NewTestableQriNode(repo, cfg.P2P, event.NilBus)
    77  	if err != nil {
    78  		return
    79  	}
    80  
    81  	opts = append([]lib.Option{
    82  		lib.OptConfig(cfg),
    83  		lib.OptQriNode(tnode.(*p2p.QriNode)),
    84  	}, opts...)
    85  
    86  	inst, err := lib.NewInstance(ctx, repoPath, opts...)
    87  	if err != nil {
    88  		return TestFactory{}, err
    89  	}
    90  
    91  	return TestFactory{
    92  		IOStreams: ioes.NewDiscardIOStreams(),
    93  		ctors: Constructors{
    94  			CryptoGenerator: repotest.NewTestCrypto(),
    95  			InitIPFS:        repotest.InitIPFSRepo,
    96  		},
    97  
    98  		repo:   repo,
    99  		rpc:    nil,
   100  		config: cfg,
   101  		node:   tnode.(*p2p.QriNode),
   102  		inst:   inst,
   103  	}, nil
   104  }
   105  
   106  // Config returns from internal state
   107  func (t TestFactory) Config() (*config.Config, error) {
   108  	return t.config, nil
   109  }
   110  
   111  func (t TestFactory) Instance() (*lib.Instance, error) {
   112  	return t.inst, nil
   113  }
   114  
   115  // RepoPath returns the path to the qri directory from internal state
   116  func (t TestFactory) RepoPath() string {
   117  	return t.repoPath
   118  }
   119  
   120  // CryptoGenerator
   121  func (t TestFactory) Constructors() Constructors {
   122  	return t.ctors
   123  }
   124  
   125  // Init will initialize the internal state
   126  func (t TestFactory) Init() error {
   127  	return nil
   128  }
   129  
   130  // Node returns the internal qri node from state
   131  func (t TestFactory) ConnectionNode() (*p2p.QriNode, error) {
   132  	return t.node, nil
   133  }
   134  
   135  // HTTPClient returns nil for tests
   136  func (t TestFactory) HTTPClient() *qhttp.Client {
   137  	return nil
   138  }