Skip to content

Commit

Permalink
Merge pull request #311 from symflower/300-ruby-write-tests
Browse files Browse the repository at this point in the history
Introduce the Ruby language
  • Loading branch information
ahumenberger authored Aug 5, 2024
2 parents d3ba2cb + c7ae56a commit 2d0d2c1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 44 deletions.
25 changes: 2 additions & 23 deletions language/golang/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package golang

import (
"context"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

pkgerrors "github.com/pkg/errors"
"github.com/zimmski/osutil"

"github.com/symflower/eval-dev-quality/language"
"github.com/symflower/eval-dev-quality/log"
Expand Down Expand Up @@ -38,26 +36,7 @@ func (l *Language) Name() (id string) {

// Files returns a list of relative file paths of the repository that should be evaluated.
func (l *Language) Files(logger *log.Logger, repositoryPath string) (filePaths []string, err error) {
repositoryPath, err = filepath.Abs(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

fs, err := osutil.FilesRecursive(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

repositoryPath = repositoryPath + string(os.PathSeparator)
for _, f := range fs {
if !strings.HasSuffix(f, ".go") {
continue
}

filePaths = append(filePaths, strings.TrimPrefix(f, repositoryPath))
}

return filePaths, nil
return language.Files(logger, l, repositoryPath)
}

// ImportPath returns the import path of the given source file.
Expand All @@ -67,7 +46,7 @@ func (l *Language) ImportPath(projectRootPath string, filePath string) (importPa

// 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"
return strings.TrimSuffix(filePath, l.DefaultFileExtension()) + l.DefaultTestFileSuffix()
}

// TestFramework returns the human-readable name of the test framework that should be used.
Expand Down
23 changes: 2 additions & 21 deletions language/java/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,7 @@ func (l *Language) Name() (id string) {

// Files returns a list of relative file paths of the repository that should be evaluated.
func (l *Language) Files(logger *log.Logger, repositoryPath string) (filePaths []string, err error) {
repositoryPath, err = filepath.Abs(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

fs, err := osutil.FilesRecursive(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

repositoryPath = repositoryPath + string(os.PathSeparator)
for _, f := range fs {
if !strings.HasSuffix(f, ".java") {
continue
}

filePaths = append(filePaths, strings.TrimPrefix(f, repositoryPath))
}

return filePaths, nil
return language.Files(logger, l, repositoryPath)
}

// ImportPath returns the import path of the given source file.
Expand All @@ -81,7 +62,7 @@ func (l *Language) TestFilePath(projectRootPath string, filePath string) (testFi
filePath = filePath[:l] + t + filePath[l+len(t):]
}

return strings.TrimSuffix(filePath, ".java") + "Test.java"
return strings.TrimSuffix(filePath, l.DefaultFileExtension()) + l.DefaultTestFileSuffix()
}

// TestFramework returns the human-readable name of the test framework that should be used.
Expand Down
26 changes: 26 additions & 0 deletions language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"time"

pkgerrors "github.com/pkg/errors"
"github.com/zimmski/osutil"

"github.com/symflower/eval-dev-quality/log"
)
Expand Down Expand Up @@ -81,6 +83,30 @@ func RepositoriesForLanguage(language Language, testdataPath string) (relativeRe
return relativeRepositoryPaths, nil
}

// Files returns a list of relative file paths of the repository that should be evaluated.
func Files(logger *log.Logger, language Language, repositoryPath string) (filePaths []string, err error) {
repositoryPath, err = filepath.Abs(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

fs, err := osutil.FilesRecursive(repositoryPath)
if err != nil {
return nil, pkgerrors.WithStack(err)
}

repositoryPath = repositoryPath + string(os.PathSeparator)
for _, f := range fs {
if !strings.HasSuffix(f, language.DefaultFileExtension()) {
continue
}

filePaths = append(filePaths, strings.TrimPrefix(f, repositoryPath))
}

return filePaths, nil
}

// TestResult holds the result of running tests.
type TestResult struct {
TestsTotal uint
Expand Down
74 changes: 74 additions & 0 deletions language/ruby/language.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ruby

import (
"path/filepath"
"strings"

"github.com/symflower/eval-dev-quality/language"
"github.com/symflower/eval-dev-quality/log"
)

// Language holds a Ruby language to evaluate a repository.
type Language struct{}

func init() {
language.Register(&Language{})
}

var _ language.Language = (*Language)(nil)

// ID returns the unique ID of this language.
func (l *Language) ID() (id string) {
return "ruby"
}

// Name is the prose name of this language.
func (l *Language) Name() (id string) {
return "Ruby"
}

// Files returns a list of relative file paths of the repository that should be evaluated.
func (l *Language) Files(logger *log.Logger, repositoryPath string) (filePaths []string, err error) {
return language.Files(logger, l, repositoryPath)
}

// ImportPath returns the import path of the given source file.
func (l *Language) ImportPath(projectRootPath string, filePath string) (importPath string) {
return "../lib/" + strings.TrimSuffix(filepath.Base(filePath), l.DefaultFileExtension())
}

// 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) {
filePath = strings.ReplaceAll(filePath, "lib", "test")

return strings.TrimSuffix(filePath, l.DefaultFileExtension()) + l.DefaultTestFileSuffix()
}

// TestFramework returns the human-readable name of the test framework that should be used.
func (l *Language) TestFramework() (testFramework string) {
return "Minitest"
}

// DefaultFileExtension returns the default file extension.
func (l *Language) DefaultFileExtension() string {
return ".rb"
}

// DefaultTestFileSuffix returns the default test file suffix.
func (l *Language) DefaultTestFileSuffix() string {
return "_test.rb"
}

// ExecuteTests invokes the language specific testing on the given repository.
func (l *Language) ExecuteTests(logger *log.Logger, repositoryPath string) (testResult *language.TestResult, problems []error, err error) {
logger.Panic("not implemented")

return testResult, problems, nil
}

// Mistakes builds a Ruby repository and returns the list of mistakes found.
func (l *Language) Mistakes(logger *log.Logger, repositoryPath string) (mistakes []string, err error) {
logger.Panic("not implemented")

return nil, nil
}
64 changes: 64 additions & 0 deletions language/ruby/language_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ruby

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLanguageTestFilePath(t *testing.T) {
type testCase struct {
Name string

ProjectRootPath string
FilePath string

ExpectedTestFilePath string
}

validate := func(t *testing.T, tc *testCase) {
t.Run(tc.Name, func(t *testing.T) {
ruby := Language{}
actualTestFilePath := ruby.TestFilePath(tc.ProjectRootPath, tc.FilePath)

assert.Equal(t, tc.ExpectedTestFilePath, actualTestFilePath)
})
}

validate(t, &testCase{
Name: "Source file",

FilePath: filepath.Join("testdata", "ruby", "plain", "lib", "plain.rb"),

ExpectedTestFilePath: filepath.Join("testdata", "ruby", "plain", "test", "plain_test.rb"),
})
}

func TestLanguageImportPath(t *testing.T) {
type testCase struct {
Name string

ProjectRootPath string
FilePath string

ExpectedImportPath string
}

validate := func(t *testing.T, tc *testCase) {
t.Run(tc.Name, func(t *testing.T) {
ruby := Language{}
actualImportPath := ruby.ImportPath(tc.ProjectRootPath, tc.FilePath)

assert.Equal(t, tc.ExpectedImportPath, actualImportPath)
})
}

validate(t, &testCase{
Name: "Source file",

FilePath: filepath.Join("testdata", "ruby", "plain", "lib", "plain.rb"),

ExpectedImportPath: "../lib/plain",
})
}
2 changes: 2 additions & 0 deletions testdata/ruby/plain/lib/plain.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def plain
end

0 comments on commit 2d0d2c1

Please sign in to comment.