Skip to content
Merged
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
20 changes: 15 additions & 5 deletions _cmptest/llcppgend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ const llcppgGoVersion = "1.20.14"
var (
// avoid conan install race condition
conanInstallMutex sync.Mutex
// avoid llgo run race condition
llgoRunMutex sync.Mutex
)

type testCase struct {
Expand Down Expand Up @@ -234,9 +232,10 @@ func runDemos(t *testing.T, logFile *os.File, demosPath string, pkgname, pkgpath
t.Fatal(err)
}

// only can lock out of loop,will got ld64.lld: error: undefined symbol: math.Float32bits
llgoRunMutex.Lock()
defer llgoRunMutex.Unlock()
llgoRunTempDir, err := os.MkdirTemp("", "llgo-run")
if err != nil {
t.Fatal(err)
}

for _, demo := range demos {
if !demo.IsDir() {
Expand All @@ -246,6 +245,8 @@ func runDemos(t *testing.T, logFile *os.File, demosPath string, pkgname, pkgpath
demoCmd := command(logFile, demoPath, "llgo", "run", ".")
demoCmd.Env = append(demoCmd.Env, llgoEnv()...)
demoCmd.Env = append(demoCmd.Env, pcPathEnv(pcPath)...)
demoCmd.Env = append(demoCmd.Env, tempDirEnv(llgoRunTempDir)...)

err = demoCmd.Run()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -280,6 +281,15 @@ func goVerEnv() string {
return fmt.Sprintf("GOTOOLCHAIN=go%s", llcppgGoVersion)
}

func tempDirEnv(tempDir string) []string {
return []string{
fmt.Sprintf("TMPDIR=%s", tempDir),
fmt.Sprintf("TEMP=%s", tempDir),
fmt.Sprintf("TMP=%s", tempDir),
fmt.Sprintf("GOTMPDIR=%s", tempDir),
}
}

func copyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
Expand Down
36 changes: 24 additions & 12 deletions cmd/llcppgtest/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
llcppg "github.com/goplus/llcppg/config"
)

var llgoRunMu sync.Mutex

var mkdirTempLazily = sync.OnceValue(func() string {
if env := os.Getenv("LLCPPG_TEST_LOG_DIR"); env != "" {
return env
Expand Down Expand Up @@ -137,11 +135,6 @@ func RunGenPkgDemo(demoRoot string, confDir string) error {
return fmt.Errorf("%s: go fmt failed in %s: %w", demoPkgName, genPkgDir, err)
}

if err = runCommand(tempLog, genPkgDir, "llgo", "build", "."); err != nil {
return fmt.Errorf("%s: llgo build failed in %s: %w", demoPkgName, genPkgDir, err)
}
fmt.Printf("%s: llgo build success\n", demoPkgName)

demosPath := filepath.Join(demoRoot, "demo")
// init mods to test package,because the demo is dependent on the gen pkg
if err = runCommand(tempLog, demoRoot, "go", "mod", "init", "demo"); err != nil {
Expand All @@ -164,15 +157,22 @@ func RunGenPkgDemo(demoRoot string, confDir string) error {
return fmt.Errorf("%s: failed to read demo directory: %v", demoPkgName, err)
}

// start to test demos via llgo run
// to avoid potential racy, we must grab the lock
llgoRunMu.Lock()
defer llgoRunMu.Unlock()
llgoRunTempDir, err := os.MkdirTemp("", "llgo-run")
if err != nil {
return err
}

for _, demo := range demos {
if demo.IsDir() {
fmt.Printf("%s: Running demo: %s\n", demoPkgName, demo.Name())
if demoErr := runCommand(tempLog, filepath.Join(demosPath, demo.Name()), "llgo", "run", "."); demoErr != nil {

// avoid racy
if demoErr := runCommandWithTempDir(
tempLog,
filepath.Join(demosPath, demo.Name()),
llgoRunTempDir,
"llgo", "run", "-v", ".",
); demoErr != nil {
return fmt.Errorf("%s: failed to run demo: %s: %w", demoPkgName, demo.Name(), demoErr)
}
}
Expand Down Expand Up @@ -255,3 +255,15 @@ func runCommand(logFile *os.File, dir, command string, args ...string) error {
cmd.Stderr = logFile
return cmd.Run()
}

func runCommandWithTempDir(logFile *os.File, dir, tempDir string, command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.Dir = dir
cmd.Stdout = logFile
cmd.Stderr = logFile
cmd.Env = append(os.Environ(), fmt.Sprintf("TMPDIR=%s", tempDir))
cmd.Env = append(cmd.Env, fmt.Sprintf("TEMP=%s", tempDir))
cmd.Env = append(cmd.Env, fmt.Sprintf("TMP=%s", tempDir))
cmd.Env = append(cmd.Env, fmt.Sprintf("GOTMPDIR=%s", tempDir))
return cmd.Run()
}
Loading