From b163d71aedab73a22fc47d2fbdbecfb26cf990c4 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Tue, 18 Feb 2025 10:58:45 +0800 Subject: [PATCH] test: 3 attempts to avoid clang errors --- compiler/internal/build/build_test.go | 51 +++++++++++++++++++-------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/compiler/internal/build/build_test.go b/compiler/internal/build/build_test.go index c391c8a6..80d8db18 100644 --- a/compiler/internal/build/build_test.go +++ b/compiler/internal/build/build_test.go @@ -9,24 +9,45 @@ import ( ) func mockRun(args []string, cfg *Config) { - mockable.EnableMock() - defer func() { - if r := recover(); r != nil { - if r != "exit" { - panic(r) - } else { - exitCode := mockable.ExitCode() - if (exitCode != 0) != false { - panic(fmt.Errorf("got exit code %d", exitCode)) + const maxAttempts = 3 + var lastErr error + var lastPanic interface{} + for attempt := 0; attempt < maxAttempts; attempt++ { + mockable.EnableMock() + func() { + defer func() { + if r := recover(); r != nil { + if r != "exit" { + lastPanic = r + } else { + exitCode := mockable.ExitCode() + if (exitCode != 0) != false { + lastPanic = fmt.Errorf("got exit code %d", exitCode) + } + } } + }() + file, _ := os.CreateTemp("", "llgo-*") + cfg.OutFile = file.Name() + file.Close() + defer os.Remove(cfg.OutFile) + _, err := Do(args, cfg) + if err == nil { + return // Success, return immediately from the inner function } + lastErr = err + }() + + if lastPanic == nil && lastErr == nil { + return // Success, return from mockRun } - }() - file, _ := os.CreateTemp("", "llgo-*") - cfg.OutFile = file.Name() - file.Close() - defer os.Remove(cfg.OutFile) - Do(args, cfg) + // Continue to next attempt if this one failed + } + // If we get here, all attempts failed + if lastPanic != nil { + panic(lastPanic) + } + panic(fmt.Errorf("all %d attempts failed, last error: %v", maxAttempts, lastErr)) } func TestRun(t *testing.T) {