Skip to content

Commit

Permalink
Fix local test passed judgement
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Jan 28, 2023
1 parent c23af24 commit 3050c9a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ 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)
}
}
if runRemotely {
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
Expand Down
10 changes: 5 additions & 5 deletions lang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions lang/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand Down

0 comments on commit 3050c9a

Please sign in to comment.