From 9f2e248965f5fdc2f7d1c39bf455397690e3d03f Mon Sep 17 00:00:00 2001 From: Rui Azevedo Date: Mon, 8 Jul 2024 11:19:39 +0100 Subject: [PATCH] refactor, Extract the logic that checks if a package has a source and test file into a helper, so it can be reused in other tasks Part of #201 --- evaluate/task/task-code-repair.go | 19 ++----------------- evaluate/task/task.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/evaluate/task/task-code-repair.go b/evaluate/task/task-code-repair.go index e7ae3676..c7a9fc93 100644 --- a/evaluate/task/task-code-repair.go +++ b/evaluate/task/task-code-repair.go @@ -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 diff --git a/evaluate/task/task.go b/evaluate/task/task.go index cbc266d1..a41d7af6 100644 --- a/evaluate/task/task.go +++ b/evaluate/task/task.go @@ -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" @@ -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 +}