github.com/ipld/go-ipld-prime@v0.21.0/schema/gen/go/testEngine_plugin_test.go (about)

     1  //go:build cgo && !skipgenbehavtests && !windows
     2  
     3  package gengo
     4  
     5  import (
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"plugin"
    10  	"testing"
    11  
    12  	"github.com/ipld/go-ipld-prime/datamodel"
    13  )
    14  
    15  func objPath(prefix string) string {
    16  	return filepath.Join(tmpGenBuildDir, prefix, "obj.so")
    17  }
    18  
    19  func buildGennedCode(t *testing.T, prefix string, _ string) {
    20  	// Invoke `go build` with flags to create a plugin -- we'll be able to
    21  	//  load into this plugin into this selfsame process momentarily.
    22  	// Use globbing, because these are files outside our module.
    23  	files, err := filepath.Glob(filepath.Join(tmpGenBuildDir, prefix, "*.go"))
    24  	if err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	args := []string{"build", "-o=" + objPath(prefix), "-buildmode=plugin"}
    28  	args = append(args, files...)
    29  	cmd := exec.Command("go", args...)
    30  	cmd.Stdout = os.Stdout
    31  	cmd.Stderr = os.Stderr
    32  	if err := cmd.Run(); err != nil {
    33  		t.Fatalf("genned code failed to compile: %s", err)
    34  	}
    35  }
    36  
    37  func fnPrototypeByName(prefix string) func(string) datamodel.NodePrototype {
    38  	plg, err := plugin.Open(objPath(prefix))
    39  	if err != nil {
    40  		panic(err) // Panic because if this was going to flunk, we expected it to flunk earlier when we ran 'go build'.
    41  	}
    42  	sym, err := plg.Lookup("GetPrototypeByName")
    43  	if err != nil {
    44  		panic(err)
    45  	}
    46  	return sym.(func(string) datamodel.NodePrototype)
    47  }