Skip to content

How to integrate with xgo? #1400

@Envek

Description

@Envek

Hey, thanks for the Ginkgo, it is nice to use, love it.

I would like to use xgo mocks to easily test various failure modes of underlying libraries, but it doesn't seem to work.

For example, I want to convert following go test (NB: it must be executed with xgo test to work):

patch_test.go
package patch_test

import (
	"testing"
	"github.com/xhd2015/xgo/runtime/mock"
)

func greet(s string) string {
	return "hello " + s
}

func TestPatchFunc(t *testing.T) {
	mock.Patch(greet, func(s string) string {
		return "mock " + s
	})

	res := greet("world")
	if res != "mock world" {
		t.Fatalf("expect patched result to be %q, actual: %q", "mock world", res)
	}
}
Run results
$ xgo test .
ok  	patch	0.004s

to Ginkgo (after bootstrapping):

patch_suite_test.go
package patch_test

import (
	"testing"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

func TestMock(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Mock Suite")
}
patch_test.go
package patch_test

import (
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
	"github.com/xhd2015/xgo/runtime/mock"
)

func greet(s string) string {
	return "hello " + s
}

var _ = Describe("Mocking with Xgo from Ginkgo", func() {
	It("works", func(ctx SpecContext) {
		unpatch := mock.Patch(greet, func(s string) string {
			return "mock " + s
		})
		defer unpatch()

		Expect(greet("world")).To(Equal("mock world"))
	})
})
Run results
$ xgo exec ginkgo ./... 

Running Suite: Mock Suite - /home/envek/Code/mock
=========================================================
Random Seed: 1714532009

Will run 1 of 1 specs
------------------------------
• [PANICKED] [0.000 seconds]
Mocking with Xgo from Ginkgo [It] works
/home/envek/Code/mock/mock_test.go:60

  [PANICKED] Test Panicked
  In [It] at: /home/envek/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.28/mock/mock.go:54 @ 05/01/24 11:53:30.098

  failed to setup mock for: mock_test.greet

  Full Stack Trace
    github.com/xhd2015/xgo/runtime/mock.getFunc({0x82d060, 0x9081a8})
    	/home/envek/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.28/mock/mock.go:54 +0xe7
    github.com/xhd2015/xgo/runtime/mock.Patch({0x82d060, 0x9081a8}, {0x82d060, 0x9081c0})
    	/home/envek/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.28/mock/patch.go:51 +0x2af
    mock_test.init.func1.1({0x0?, 0x0?})
    	/home/envek/Code/mock/mock_test.go:61 +0x3b
------------------------------

Summarizing 1 Failure:
  [PANICKED!] Mocking with Xgo from Ginkgo [It] works
  /home/envek/go/pkg/mod/github.com/xhd2015/xgo/runtime@v1.0.28/mock/mock.go:54

Ran 1 of 1 Specs in 0.000 seconds
FAIL! -- 0 Passed | 1 Failed | 0 Pending | 0 Skipped
--- FAIL: TestMock (0.00s)
FAIL

Ginkgo ran 1 suite in 354.453564ms

Test Suite Failed
exit status 1

But as it can be seen that when test run either via xgo test . or via xgo exec ginkgo ./..., xgo loses its under-the-hood tweaks. I suppose that ginkgo somehow re-executes tests in a new process maybe (didn't dive into ginkgo internals yet)

Is it possible to use xgo with ginkgo? How to properly set it up?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions