buildPlugin uses Go to build a plugin from one of the source files in the plugins directory. It returns the path to the built plugin binary. To allow for multiple plugins to be built in parallel, the plugin is compiled with a unique identifier in the binary. This is done by setting a linker flag wit
(t *testing.T, ctx context.Context)
| 62 | // To allow for multiple plugins to be built in parallel, the plugin is compiled with a unique |
| 63 | // identifier in the binary. This is done by setting a linker flag with the -ldflags option. |
| 64 | func buildPlugin(t *testing.T, ctx context.Context) (string, error) { |
| 65 | t.Helper() |
| 66 | |
| 67 | randomName, err := randomString() |
| 68 | if err != nil { |
| 69 | return "", err |
| 70 | } |
| 71 | |
| 72 | goBin, err := exec.LookPath("/usr/local/go/bin/go") |
| 73 | if err != nil { |
| 74 | return "", err |
| 75 | } |
| 76 | installPath := filepath.Join(os.Getenv("GOPATH"), "bin", randomName) |
| 77 | |
| 78 | pluginContent, err := plugins.ReadFile("plugins/basic.go") |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | dir := fs.NewDir(t, "plugin_build") |
| 83 | if err := os.WriteFile(dir.Join("main.go"), pluginContent, 0o644); err != nil { |
| 84 | return "", err |
| 85 | } |
| 86 | defer dir.Remove() |
| 87 | |
| 88 | cmd := exec.CommandContext(ctx, goBin, "build", "-ldflags", |
| 89 | fmt.Sprintf("-X 'main.UNIQUEME=%s'", randomName), |
| 90 | "-o", installPath, dir.Join("main.go")) |
| 91 | |
| 92 | cmd.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 93 | |
| 94 | if out, err := cmd.CombinedOutput(); err != nil { |
| 95 | return "", fmt.Errorf("error building basic plugin bin: %s: %w", string(out), err) |
| 96 | } |
| 97 | |
| 98 | return installPath, nil |
| 99 | } |
| 100 | |
| 101 | func randomString() (string, error) { |
| 102 | b := make([]byte, 8) |
no test coverage detected
searching dependent graphs…