support ldflags rewrites for initialized globals
This commit is contained in:
@@ -8,6 +8,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"github.com/goplus/llgo/internal/mockable"
|
||||
@@ -94,4 +97,65 @@ func TestCmpTest(t *testing.T) {
|
||||
mockRun([]string{"../../cl/_testgo/runtest"}, &Config{Mode: ModeCmpTest})
|
||||
}
|
||||
|
||||
// TestGenerateOutputFilenames removed - functionality moved to filename_test.go
|
||||
const (
|
||||
rewriteMainPkg = "github.com/goplus/llgo/cl/_testgo/rewrite"
|
||||
rewriteDepPkg = rewriteMainPkg + "/dep"
|
||||
rewriteDirPath = "../../cl/_testgo/rewrite"
|
||||
)
|
||||
|
||||
func TestLdFlagsRewriteVars(t *testing.T) {
|
||||
buildRewriteBinary(t, false, "build-main", "build-pkg")
|
||||
buildRewriteBinary(t, false, "rerun-main", "rerun-pkg")
|
||||
}
|
||||
|
||||
func TestLdFlagsRewriteVarsMainAlias(t *testing.T) {
|
||||
buildRewriteBinary(t, true, "alias-main", "alias-pkg")
|
||||
}
|
||||
|
||||
func buildRewriteBinary(t *testing.T, useMainAlias bool, mainVal, depVal string) {
|
||||
t.Helper()
|
||||
binPath := filepath.Join(t.TempDir(), "rewrite")
|
||||
if runtime.GOOS == "windows" {
|
||||
binPath += ".exe"
|
||||
}
|
||||
|
||||
cfg := &Config{Mode: ModeBuild, OutFile: binPath}
|
||||
mainKey := rewriteMainPkg
|
||||
var mainPkgs []string
|
||||
if useMainAlias {
|
||||
mainKey = "main"
|
||||
mainPkgs = []string{rewriteMainPkg}
|
||||
}
|
||||
mainPlain := mainVal + "-plain"
|
||||
depPlain := depVal + "-plain"
|
||||
gorootVal := "goroot-" + mainVal
|
||||
versionVal := "version-" + mainVal
|
||||
addGlobalString(cfg, mainKey+".VarName="+mainVal, mainPkgs)
|
||||
addGlobalString(cfg, mainKey+".VarPlain="+mainPlain, mainPkgs)
|
||||
addGlobalString(cfg, rewriteDepPkg+".VarName="+depVal, nil)
|
||||
addGlobalString(cfg, rewriteDepPkg+".VarPlain="+depPlain, nil)
|
||||
addGlobalString(cfg, "runtime.defaultGOROOT="+gorootVal, nil)
|
||||
addGlobalString(cfg, "runtime.buildVersion="+versionVal, nil)
|
||||
|
||||
if _, err := Do([]string{rewriteDirPath}, cfg); err != nil {
|
||||
t.Fatalf("ModeBuild failed: %v", err)
|
||||
}
|
||||
got := runBinary(t, binPath)
|
||||
want := fmt.Sprintf(
|
||||
"main.VarName: %s\nmain.VarPlain: %s\ndep.VarName: %s\ndep.VarPlain: %s\nruntime.GOROOT(): %s\nruntime.Version(): %s\n",
|
||||
mainVal, mainPlain, depVal, depPlain, gorootVal, versionVal,
|
||||
)
|
||||
if got != want {
|
||||
t.Fatalf("unexpected binary output:\nwant %q\ngot %q", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func runBinary(t *testing.T, path string, args ...string) string {
|
||||
t.Helper()
|
||||
cmd := exec.Command(path, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("failed to run %s: %v\n%s", path, err, output)
|
||||
}
|
||||
return string(output)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user