Skip to content

Commit

Permalink
refactor, Extract the logic that checks if a package has a source and…
Browse files Browse the repository at this point in the history
… test file into a helper, so it can be reused in other tasks

Part of #201
  • Loading branch information
ruiAzevedo19 committed Jul 8, 2024
1 parent 3d4b410 commit 9f2e248
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
19 changes: 2 additions & 17 deletions evaluate/task/task-code-repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,9 @@ func (t *TaskCodeRepair) unpackCodeRepairPackage(ctx evaltask.Context, fileLogge
return "", nil, pkgerrors.Errorf("package %q in repository %q must contain source files with compilation errors", packagePath, ctx.Repository.Name())
}

filePaths, err := ctx.Language.Files(fileLogger, packagePath)
sourceFilePath, err = packageHasSourceAndTestFile(fileLogger, ctx.Repository.Name(), packagePath, ctx.Language)
if err != nil {
return "", nil, pkgerrors.WithStack(err)
} else if len(filePaths) != 2 {
return "", nil, pkgerrors.Errorf("package %q in repository %q must only contain an implementation file and the corresponding test file, but found %#v", packagePath, ctx.Repository.Name(), filePaths)
}
var hasTestFile bool
for _, file := range filePaths {
if strings.HasSuffix(file, ctx.Language.DefaultTestFileSuffix()) {
hasTestFile = true
} else if filepath.Ext(file) == ctx.Language.DefaultFileExtension() {
sourceFilePath = file
}
}
if sourceFilePath == "" {
return "", nil, pkgerrors.Errorf("package %q in repository %q does not contain a source file", packagePath, ctx.Repository.Name())
} else if !hasTestFile {
return "", nil, pkgerrors.Errorf("package %q in repository %q does not contain a test file", packagePath, ctx.Repository.Name())
return "", nil, err
}

return sourceFilePath, mistakes, nil
Expand Down
27 changes: 27 additions & 0 deletions evaluate/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package task
import (
"fmt"
"path/filepath"
"strings"

pkgerrors "github.com/pkg/errors"
"github.com/symflower/eval-dev-quality/language"
"github.com/symflower/eval-dev-quality/log"
"github.com/symflower/eval-dev-quality/model"
evaltask "github.com/symflower/eval-dev-quality/task"
Expand Down Expand Up @@ -83,3 +85,28 @@ func (t *taskLogger) finalize(problems []error) {

t.logClose()
}

// packageHasSourceAndTestFile checks if a package as a source file and the corresponding test file for the given language, and returns the source file path.
func packageHasSourceAndTestFile(log *log.Logger, repositoryName string, packagePath string, language language.Language) (sourceFilePath string, err error) {
filePaths, err := language.Files(log, packagePath)
if err != nil {
return "", pkgerrors.WithStack(err)
} else if len(filePaths) != 2 {
return "", pkgerrors.Errorf("package %q in repository %q must only contain an implementation file and the corresponding test file, but found %#v", packagePath, repositoryName, filePaths)
}
var hasTestFile bool
for _, file := range filePaths {
if strings.HasSuffix(file, language.DefaultTestFileSuffix()) {
hasTestFile = true
} else if filepath.Ext(file) == language.DefaultFileExtension() {
sourceFilePath = file
}
}
if sourceFilePath == "" {
return "", pkgerrors.Errorf("package %q in repository %q does not contain a source file", packagePath, repositoryName)
} else if !hasTestFile {
return "", pkgerrors.Errorf("package %q in repository %q does not contain a test file", packagePath, repositoryName)
}

return sourceFilePath, nil
}

0 comments on commit 9f2e248

Please sign in to comment.