示例#1
0
func TestApp_compileUi(t *testing.T) {
	client, server := testNewClientServer(t)
	defer client.Close()

	appMock := server.AppFunc().(*app.Mock)
	appReal, err := client.App()
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	appMock.CompileFunc = func(ctx *app.Context) (*app.CompileResult, error) {
		ctx.Ui.Message("HELLO!")
		return nil, nil
	}

	ui := new(ui.Mock)
	ctx := new(app.Context)
	ctx.Ui = ui

	_, err = appReal.Compile(ctx)
	if !appMock.CompileCalled {
		t.Fatal("compile should be called")
	}
	if err != nil {
		t.Fatalf("bad: %#v", err)
	}

	if ui.MessageBuf[0] != "HELLO!" {
		t.Fatalf("bad: %#v", ui)
	}
}
示例#2
0
文件: app_test.go 项目: mbrodala/otto
func TestAppImplicit(t *testing.T) {
	cases := []struct {
		Dir  string
		Deps []string
	}{
		{
			"implicit-none",
			nil,
		},

		{
			"implicit-redis",
			[]string{"github.com/hashicorp/otto/examples/redis"},
		},
	}

	for _, tc := range cases {
		errPrefix := fmt.Sprintf("In '%s': ", tc.Dir)

		// Build our context we send in
		var ctx app.Context
		ctx.Appfile = &appfile.File{Path: filepath.Join("./test-fixtures", tc.Dir, "Appfile")}

		// Get the implicit file
		var a App
		f, err := a.Implicit(&ctx)
		if err != nil {
			t.Fatalf("%s: %s", errPrefix, err)
		}
		if (len(tc.Deps) == 0) != (f == nil) {
			// Complicated statement above but basically: should be nil if
			// we expected no deps, and should not be nil if we expect deps
			t.Fatalf("%s: deps: %#v\n\ninvalid file: %#v", errPrefix, tc.Deps, f)
		}
		if f == nil {
			continue
		}

		// Build the deps we got and sort them for determinism
		actual := make([]string, 0, len(f.Application.Dependencies))
		for _, dep := range f.Application.Dependencies {
			actual = append(actual, dep.Source)
		}
		sort.Strings(actual)
		sort.Strings(tc.Deps)

		// Test
		if !reflect.DeepEqual(actual, tc.Deps) {
			t.Fatalf("%s\n\ngot: %#v\n\nexpected: %#v", errPrefix, actual, tc.Deps)
		}
	}
}
示例#3
0
func TestDirectory(t *testing.T) {
	// Create the temporary directory for the directory data
	td, err := ioutil.TempDir("", "otto")
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	defer os.RemoveAll(td)

	// Create the actual plugin client/server
	client, server, streams := testNewClientServer(t)
	defer streams.Close()
	defer client.Close()

	// Build a context that points to our bolt directory backend
	ctx := new(app.Context)
	ctx.Directory = &directory.BoltBackend{Dir: td}

	// Create an appMock. The mock has a compile function that actually
	// calls the directory test on it to verify that this works properly.
	//
	// This will verify the backend that is being passed through the
	// RPC layer actually works. We have to test it within the callback
	// since the connection is over after that point.
	appMock := server.AppFunc().(*app.Mock)
	appReal, err := client.App()
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	appMock.CompileFunc = func(ctx *app.Context) (r *app.CompileResult, err error) {
		directory.TestBackend(t, ctx.Directory)
		return
	}

	_, err = appReal.Compile(ctx)
	if !appMock.CompileCalled {
		t.Fatal("compile should be called")
	}
	if err != nil {
		t.Fatalf("bad: %#v", err)
	}
}