From c7ae56a472c05fa4b9f875631fb927065f0118ca Mon Sep 17 00:00:00 2001 From: Rui Azevedo Date: Fri, 2 Aug 2024 08:46:24 +0100 Subject: [PATCH] Introduce the Ruby language Part of #300 --- language/ruby/language.go | 74 ++++++++++++++++++++++++++++++++++ language/ruby/language_test.go | 64 +++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 language/ruby/language.go create mode 100644 language/ruby/language_test.go diff --git a/language/ruby/language.go b/language/ruby/language.go new file mode 100644 index 00000000..b4fb12f8 --- /dev/null +++ b/language/ruby/language.go @@ -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 +} diff --git a/language/ruby/language_test.go b/language/ruby/language_test.go new file mode 100644 index 00000000..9c7375b7 --- /dev/null +++ b/language/ruby/language_test.go @@ -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", + }) +}