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

     1  //go:build !cgo && !skipgenbehavtests && !windows
     2  
     3  // Confession:
     4  // This build tag specification is NOT sufficient nor necessarily correct --
     5  // it's a vague approximation of what's present in the stdlib 'plugin' package.
     6  // It's also not at all a sure thing that cgo will actually *work* just
     7  // because a build tag hasn't explicitly stated that it *mayn't* -- cgo can
     8  // and will fail for environmental reasons at the point the compiler uses it.
     9  //
    10  // Ideally, there'd be a way to *ask* the plugin package if it's going to
    11  // work or not before we try to use it; unfortunately, at the time of writing,
    12  // it does not appear there is such an ability.
    13  //
    14  // If you run afoul of these build tags somehow (e.g., building plugins isn't
    15  // possible in your environment for some reason), use the 'skipgenbehavtests'
    16  // build tag to right yourself.  That's what it's there for.
    17  
    18  package gengo
    19  
    20  import (
    21  	"fmt"
    22  	"io"
    23  	"os"
    24  	"os/exec"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"github.com/ipld/go-ipld-prime/datamodel"
    29  )
    30  
    31  func buildGennedCode(t *testing.T, prefix string, pkgName string) {
    32  	// Emit a small file with a 'main' method.
    33  	//  'go build' doesn't like it we're in a package called "main" and there isn't one
    34  	//   (and at the same time, plugins demand that they be in a package called 'main',
    35  	//    so 'pkgName' in practice is almost always "main").
    36  	//  I dunno, friend.  I didn't write the rules.
    37  	if pkgName == "main" {
    38  		withFile(filepath.Join(tmpGenBuildDir, prefix, "main.go"), func(w io.Writer) {
    39  			fmt.Fprintf(w, "package %s\n\n", pkgName)
    40  			fmt.Fprintf(w, "func main() {}\n")
    41  		})
    42  	}
    43  
    44  	// Invoke 'go build' -- nothing fancy.
    45  	files, err := filepath.Glob(filepath.Join(tmpGenBuildDir, prefix, "*.go"))
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	args := []string{"build"}
    50  	args = append(args, files...)
    51  	cmd := exec.Command("go", args...)
    52  	cmd.Stdout = os.Stdout
    53  	cmd.Stderr = os.Stderr
    54  	err = cmd.Run()
    55  	if err != nil {
    56  		t.Fatalf("genned code failed to compile: %s", err)
    57  	}
    58  
    59  	t.Skip("behavioral tests for generated code skipped: cgo is required for these tests")
    60  }
    61  
    62  func fnPrototypeByName(prefix string) func(string) datamodel.NodePrototype {
    63  	return nil // unused
    64  }