Skip to content

Commit

Permalink
Merge pull request #60 from symflower/test-file-name
Browse files Browse the repository at this point in the history
Let language decide test file paths instead of hardcoding that
  • Loading branch information
zimmski authored Apr 22, 2024
2 parents c847dc6 + 57c68ca commit d07793f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions language/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (l *Language) Files(log *log.Logger, repositoryPath string) (filePaths []st
return filePaths, nil
}

// TestFilePath returns the file path of a test file given the corresponding file path of the test's source file.
func (l *Language) TestFilePath(projectRootPath string, filePath string) (testFilePath string) {
return strings.TrimSuffix(filePath, ".go") + "_test.go"

}

var languageGoNoTestsMatch = regexp.MustCompile(`(?m)^DONE (\d+) tests.*in (.+?)$`)
var languageGoCoverageMatch = regexp.MustCompile(`(?m)^coverage: (\d+\.?\d+)% of statements`)
var languageGoNoCoverageMatch = regexp.MustCompile(`(?m)^coverage: \[no statements\]$`)
Expand Down
2 changes: 2 additions & 0 deletions language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type Language interface {

// Files returns a list of relative file paths of the repository that should be evaluated.
Files(log *log.Logger, repositoryPath string) (filePaths []string, err error)
// TestFilePath returns the file path of a test file given the corresponding file path of the test's source file.
TestFilePath(projectRootPath string, filePath string) (testFilePath string)

// Execute invokes the language specific testing on the given repository.
Execute(log *log.Logger, repositoryPath string) (coverage float64, err error)
Expand Down
5 changes: 5 additions & 0 deletions language/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (m *MockLanguage) Files(log *log.Logger, repositoryPath string) (filePaths
return args.Get(0).([]string), args.Error(1)
}

// TestFilePath implements language.Language.
func (m *MockLanguage) TestFilePath(projectRootPath string, filePath string) (testFilePath string) {
return m.Called(projectRootPath, filePath).String(0)
}

// ID implements language.Language.
func (m *MockLanguage) ID() (id string) {
return m.Called().String(0)
Expand Down
7 changes: 2 additions & 5 deletions model/llm/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ func (m *Model) GenerateTestsForFile(log *log.Logger, language language.Language

assessment, testContent := prompt.ParseResponse(response)

// TODO Ask the model for the test file name or compute it in a more sophisticated manner.
fileExtension := filepath.Ext(filePath)
testFilePath := filepath.Join(repositoryPath, strings.TrimSuffix(filePath, fileExtension)+"_test"+fileExtension)

if err := os.WriteFile(testFilePath, []byte(testContent), 0644); err != nil {
testFilePath := language.TestFilePath(repositoryPath, filePath)
if err := os.WriteFile(filepath.Join(repositoryPath, testFilePath), []byte(testContent), 0644); err != nil {
return nil, pkgerrors.WithStack(err)
}

Expand Down

0 comments on commit d07793f

Please sign in to comment.