Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ linters:
linters:
- revive
text: "var-naming"
# internal/native is the FFI boundary: handing pointers to pact_ffi (G103)
# and narrowing Go ints to the fixed-width C types in its signatures
# (G115) is what the package is for. cgo's C.int(x) conversions hid the
# same narrowing from gosec; the backend-neutral call sites spell it in
# Go types. Scoped to these two rules, and to this package.
- path: internal/native/
linters:
- gosec
text: "G103|G115"
# internal/native's switch statements match pact_ffi's C return codes,
# each documented in a comment table directly above the switch; the
# numbers are FFI enum values, not tunable magic numbers. Scoped to
Expand Down
6 changes: 6 additions & 0 deletions command/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ var (
log.Println("[ERROR] Your Pact library installation is out of date and we were unable to download a newer one for you:", err)
os.Exit(1)
}

// Record where the libraries are, for `pact-go test`.
err = i.SetConfigLibDir(i.GetActualLibDir())
if err != nil {
log.Println("[WARN] unable to record the library directory in the pact-go config:", err)
}
},
}
)
Expand Down
60 changes: 60 additions & 0 deletions command/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package command

import (
"errors"
"log"
"os"
"os/exec"

"github.com/spf13/cobra"

"github.com/pact-foundation/pact-go/v2/installer"
)

// libPathEnvVar names the directory the purego and goffi backends load the
// pact_ffi library from at runtime.
const libPathEnvVar = "PACT_LD_LIBRARY_PATH"

var testCmd = &cobra.Command{
Use: "test",
Short: "Run go test with the Pact library path configured",
Long: "Wrapper for 'go test' that sets " + libPathEnvVar + " to the directory the Pact libraries " +
"were installed to, for builds that load them at runtime (CGO_ENABLED=0, or the pact_purego and pact_goffi build tags)",
DisableFlagParsing: true,
Run: func(_ *cobra.Command, args []string) {
// An explicit environment setting wins over the recorded directory.
libDir := os.Getenv(libPathEnvVar)
if libDir == "" {
libDir = installer.GetConfigLibDir()
}
if libDir == "" {
log.Println("[ERROR] no library directory is configured: run `pact-go install`, or set", libPathEnvVar)
os.Exit(1)
}

log.Printf("[INFO] running go test with %s=%s", libPathEnvVar, libDir)

goCmd := exec.Command("go", append([]string{"test"}, args...)...) // #nosec G204 -- runs go test with the caller's own arguments
goCmd.Env = append(os.Environ(), libPathEnvVar+"="+libDir)
goCmd.Stdout = os.Stdout
goCmd.Stderr = os.Stderr
goCmd.Stdin = os.Stdin

err := goCmd.Run()
if err == nil {
return
}

var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
os.Exit(exitErr.ExitCode())
}

log.Printf("[ERROR] failed to run go test: %v", err)
os.Exit(1)
},
}

func init() {
RootCmd.AddCommand(testCmd)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ require (
)

require (
github.com/ebitengine/purego v0.11.3
github.com/go-webgpu/goffi v0.6.8
github.com/golang/snappy v0.0.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spf13/pflag v1.0.9 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/sys v0.48.0 // indirect
golang.org/x/sys v0.48.0
golang.org/x/text v0.42.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1 // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.6.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/ebitengine/purego v0.11.3 h1:OB+JLpdm7ioymhyA+6s5kbb/uOuYycAgL8NNTFc5MLg=
github.com/ebitengine/purego v0.11.3/go.mod h1:DCHPP08djqhNSoTfImcnHYQRZmd0qhakvrozqaEYhGQ=
github.com/go-webgpu/goffi v0.6.8 h1:FjNYbHf+zvJXxbRM/PaTqwuhlgzypBNgA12Vo1updQk=
github.com/go-webgpu/goffi v0.6.8/go.mod h1:wfoxNsJkU+5RFbV1kNN1kunhc1lFHuJKK3zpgx08/uM=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down
22 changes: 22 additions & 0 deletions installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,27 @@ func (i *Installer) SetLibDir(dir string) {
i.libDir = dir
}

// SetConfigLibDir records dir in the pact-go configuration file as the
// directory the libraries were installed to, for `pact-go test` to use.
func (i *Installer) SetConfigLibDir(dir string) error {
log.Printf("[DEBUG] setting config lib dir to %s", dir)
c := i.config.readConfig()
c.LibDir = dir

return i.config.writeConfig(c)
}

// GetConfigLibDir returns the directory the libraries were last installed
// to, as recorded in the pact-go configuration file, or "" if none is.
func GetConfigLibDir() string {
return configuration{}.readConfig().LibDir
}

// GetActualLibDir returns the directory the libraries are installed to.
func (i *Installer) GetActualLibDir() string {
return i.getLibDir()
}

// Force installs over the top.
func (i *Installer) Force(force bool) {
i.force = force
Expand Down Expand Up @@ -746,6 +767,7 @@ type packageMetadata struct {

type pactConfig struct {
Libraries map[string]packageMetadata `yaml:"libraries"`
LibDir string `yaml:"libDir,omitempty"`
}

type configReader interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/native/c_types_unix.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build darwin || linux
//go:build (darwin || linux) && cgo && !pact_purego && !pact_goffi

package native

Expand Down
2 changes: 1 addition & 1 deletion internal/native/c_types_win.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows
//go:build windows && cgo && !pact_purego && !pact_goffi

package native

Expand Down
Loading
Loading