Files
ReleaseKit/buildinfo/version_integration_test.go

82 lines
2.0 KiB
Go

package buildinfo_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/versioncontract"
)
func TestVersionInConsumerExecutable(t *testing.T) {
repo, err := filepath.Abs("..")
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
module := fmt.Sprintf(`module example.com/versionfixture
go 1.25.0
require gitea.maximumdirect.net/eric/ReleaseKit v0.0.0
replace gitea.maximumdirect.net/eric/ReleaseKit => %s
`, filepath.ToSlash(repo))
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte(module), 0o600); err != nil {
t.Fatal(err)
}
program := `package main
import (
"fmt"
"gitea.maximumdirect.net/eric/ReleaseKit/buildinfo"
)
func main() { fmt.Print(buildinfo.Version()) }
`
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte(program), 0o600); err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
linkerArgs []string
want string
}{
{name: "development build", want: "dev"},
{
name: "injected release version",
linkerArgs: []string{"-X", versioncontract.InjectedVersionLinkerSymbol + "=v1.2.3"},
want: "v1.2.3",
},
} {
t.Run(test.name, func(t *testing.T) {
binary := filepath.Join(t.TempDir(), "versionfixture")
args := []string{"build", "-o", binary}
if len(test.linkerArgs) > 0 {
args = append(args, "-ldflags", fmt.Sprint(test.linkerArgs[0], " ", test.linkerArgs[1]))
}
args = append(args, ".")
build := exec.Command("go", args...)
build.Dir = dir
build.Env = append(os.Environ(), "GOTOOLCHAIN=local", "GOPROXY=off", "GOSUMDB=off")
if output, err := build.CombinedOutput(); err != nil {
t.Fatalf("build consumer fixture: %v\n%s", err, output)
}
command := exec.Command(binary)
output, err := command.CombinedOutput()
if err != nil {
t.Fatalf("run consumer fixture: %v\n%s", err, output)
}
if string(output) != test.want {
t.Fatalf("version output = %q, want %q", output, test.want)
}
})
}
}