github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/rpc/directory_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/otto/app"
     9  	"github.com/hashicorp/otto/directory"
    10  )
    11  
    12  func TestDirectory_impl(t *testing.T) {
    13  	var _ directory.Backend = new(Directory)
    14  }
    15  
    16  func TestDirectory(t *testing.T) {
    17  	// Create the temporary directory for the directory data
    18  	td, err := ioutil.TempDir("", "otto")
    19  	if err != nil {
    20  		t.Fatalf("err: %s", err)
    21  	}
    22  	defer os.RemoveAll(td)
    23  
    24  	// Create the actual plugin client/server
    25  	client, server, streams := testNewClientServer(t)
    26  	defer streams.Close()
    27  	defer client.Close()
    28  
    29  	// Build a context that points to our bolt directory backend
    30  	ctx := new(app.Context)
    31  	ctx.Directory = &directory.BoltBackend{Dir: td}
    32  
    33  	// Create an appMock. The mock has a compile function that actually
    34  	// calls the directory test on it to verify that this works properly.
    35  	//
    36  	// This will verify the backend that is being passed through the
    37  	// RPC layer actually works. We have to test it within the callback
    38  	// since the connection is over after that point.
    39  	appMock := server.AppFunc().(*app.Mock)
    40  	appReal, err := client.App()
    41  	if err != nil {
    42  		t.Fatalf("err: %s", err)
    43  	}
    44  
    45  	appMock.CompileFunc = func(ctx *app.Context) (r *app.CompileResult, err error) {
    46  		directory.TestBackend(t, ctx.Directory)
    47  		return
    48  	}
    49  
    50  	_, err = appReal.Compile(ctx)
    51  	if !appMock.CompileCalled {
    52  		t.Fatal("compile should be called")
    53  	}
    54  	if err != nil {
    55  		t.Fatalf("bad: %#v", err)
    56  	}
    57  }