diff --git a/cmd/test.go b/cmd/test.go index 72d87517..7102a28d 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -85,9 +85,8 @@ leetgo test w330/`, localPassed, remotePassed := true, true if runLocally { hclog.L().Info("running test locally", "question", q.TitleSlug) - err = lang.RunLocalTest(q) + localPassed, err = lang.RunLocalTest(q) if err != nil { - localPassed = false hclog.L().Error("failed to run test locally", "question", q.TitleSlug, "err", err) } } @@ -95,6 +94,7 @@ leetgo test w330/`, result, err := runTestRemotely(q, c, gen, testLimiter) if err != nil { hclog.L().Error("failed to run test remotely", "question", q.TitleSlug, "err", err) + remotePassed = false } else { cmd.Print(result.Display(q)) remotePassed = result.CorrectAnswer diff --git a/lang/gen.go b/lang/gen.go index f74b2f80..d0fa4273 100644 --- a/lang/gen.go +++ b/lang/gen.go @@ -78,7 +78,7 @@ type NeedInitialization interface { } type LocalTestable interface { - RunLocalTest(q *leetcode.QuestionData, dir string) error + RunLocalTest(q *leetcode.QuestionData, dir string) (bool, error) } type baseLang struct { @@ -446,21 +446,21 @@ func GetSolutionCode(q *leetcode.QuestionData) (string, error) { return strings.Join(codeLinesToKeep, "\n"), nil } -func RunLocalTest(q *leetcode.QuestionData) error { +func RunLocalTest(q *leetcode.QuestionData) (bool, error) { cfg := config.Get() gen := GetGenerator(cfg.Code.Lang) if gen == nil { - return fmt.Errorf("language %s is not supported", cfg.Code.Lang) + return false, fmt.Errorf("language %s is not supported", cfg.Code.Lang) } tester, ok := gen.(LocalTestable) if !ok { - return fmt.Errorf("language %s does not support local test", gen.Slug()) + return false, fmt.Errorf("language %s does not support local test", gen.Slug()) } outDir := getOutDir(q, gen) if !utils.IsExist(outDir) { - return fmt.Errorf("no code generated for %s in language %s", q.TitleSlug, gen.Slug()) + return false, fmt.Errorf("no code generated for %s in language %s", q.TitleSlug, gen.Slug()) } return tester.RunLocalTest(q, outDir) diff --git a/lang/go.go b/lang/go.go index e7a210b7..0a51c20c 100644 --- a/lang/go.go +++ b/lang/go.go @@ -133,21 +133,21 @@ func (g golang) Initialize(outDir string) error { return err } -func (g golang) RunLocalTest(q *leetcode.QuestionData, outDir string) error { +func (g golang) RunLocalTest(q *leetcode.QuestionData, outDir string) (bool, error) { cmd := exec.Command("go", "list", "-m") cmd.Dir = outDir output, err := cmd.Output() if err != nil { - return fmt.Errorf("go list failed: %w", err) + return false, fmt.Errorf("go list failed: %w", err) } modPath := strings.TrimSpace(string(output)) if modPath == "" { - return fmt.Errorf("go mod path is empty") + return false, fmt.Errorf("go mod path is empty") } genResult, err := g.GeneratePaths(q) if err != nil { - return fmt.Errorf("generate paths failed: %w", err) + return false, fmt.Errorf("generate paths failed: %w", err) } path := genResult.Files[0].Path basePath := filepath.Clean(filepath.Dir(path)) @@ -156,9 +156,9 @@ func (g golang) RunLocalTest(q *leetcode.QuestionData, outDir string) error { cmd.Dir = outDir cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - _ = cmd.Run() + err = cmd.Run() - return nil + return err == nil, nil } func (g golang) generateTest(q *leetcode.QuestionData, testcases string) string {