Skip to content

Commit

Permalink
fix, Transpile task must always use relative paths for origin files c…
Browse files Browse the repository at this point in the history
…ause the model is expected to build the absolute path

Part of #300
  • Loading branch information
bauersimon committed Sep 5, 2024
1 parent 19011da commit 7abda1d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion evaluate/task/task-transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (t *TaskTranspile) unpackTranspilerPackage(ctx evaltask.Context, logger *lo
if !ok {
return nil, "", pkgerrors.Errorf("the language extension %q is not supported", filepath.Ext(file.Name()))
}
originFilePathsWithLanguage[filepath.Join(packagePathAbsolute, "implementation", file.Name())] = originLanguage
originFilePathsWithLanguage[filepath.Join("implementation", file.Name())] = originLanguage
}

stubFilePath, err = packageSourceFile(logger, packagePathAbsolute, ctx.Language)
Expand Down
29 changes: 17 additions & 12 deletions evaluate/task/task-transpile_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package task

import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/symflower/eval-dev-quality/language/java"
"github.com/symflower/eval-dev-quality/language/ruby"
"github.com/symflower/eval-dev-quality/log"
"github.com/symflower/eval-dev-quality/model"
modeltesting "github.com/symflower/eval-dev-quality/model/testing"
evaltask "github.com/symflower/eval-dev-quality/task"
"github.com/zimmski/osutil"
Expand All @@ -36,6 +39,12 @@ func TestTaskTranspileRun(t *testing.T) {
})
}

validateContext := func(t *testing.T, c model.Context) {
arguments, ok := c.Arguments.(*TaskArgumentsTranspile)
require.True(t, ok, fmt.Sprintf("%T != TaskArgumentsTranspile", arguments))
assert.True(t, strings.HasPrefix(arguments.OriginFilePath, "implementation"+string(os.PathSeparator)), fmt.Sprintf("%q must be a relative path", arguments.OriginFilePath))
}

t.Run("Transpile Java into Go", func(t *testing.T) {
{
temporaryDirectoryPath := t.TempDir()
Expand All @@ -59,7 +68,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

validate(t, &tasktesting.TestCaseTask{
Name: "Single test case",
Expand Down Expand Up @@ -113,7 +122,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

transpiledSourceFilePath = "isSorted.go"
transpiledSourceFileContent = bytesutil.StringTrimIndentations(`
Expand All @@ -128,7 +137,7 @@ func TestTaskTranspileRun(t *testing.T) {
return i == len(a)-1
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

validate(t, &tasktesting.TestCaseTask{
Name: "Multiple test cases",
Expand Down Expand Up @@ -191,7 +200,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

validate(t, &tasktesting.TestCaseTask{
Name: "Single test case",
Expand Down Expand Up @@ -245,7 +254,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

transpiledSourceFilePath = filepath.Join("src", "main", "java", "com", "eval", "IsSorted.java")
transpiledSourceFileContent = bytesutil.StringTrimIndentations(`
Expand All @@ -262,7 +271,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

validate(t, &tasktesting.TestCaseTask{
Name: "Multiple test cases",
Expand Down Expand Up @@ -317,7 +326,7 @@ func TestTaskTranspileRun(t *testing.T) {
}
}
`)
modelMock.RegisterGenerateSuccess(t, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)
modelMock.RegisterGenerateSuccess(t, validateContext, transpiledSourceFilePath, transpiledSourceFileContent, metricstesting.AssessmentsWithProcessingTime).Times(2)

validate(t, &tasktesting.TestCaseTask{
Name: "Model generated test with unused import",
Expand Down Expand Up @@ -603,11 +612,7 @@ func TestTaskTranspileUnpackTranspilerPackage(t *testing.T) {
actualOriginFilePathsWithLanguage, actualStubFilePath, actualErr := taskTranspile.unpackTranspilerPackage(ctx, logger, tc.PackagePath)
require.NoError(t, actualErr)

expectedOriginFilePathsWithLanguage := map[string]language.Language{}
for filePath, language := range tc.ExpectedOriginFilePathsWithLanguage {
expectedOriginFilePathsWithLanguage[filepath.Join(repository.DataPath(), tc.PackagePath, filePath)] = language
}
assert.Equal(t, expectedOriginFilePathsWithLanguage, actualOriginFilePathsWithLanguage)
assert.Equal(t, tc.ExpectedOriginFilePathsWithLanguage, actualOriginFilePathsWithLanguage)
assert.Equal(t, tc.ExpectedStubFilePath, actualStubFilePath)
})
}
Expand Down
5 changes: 4 additions & 1 deletion model/testing/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ func (m *MockCapabilityRepairCode) RegisterGenerateError(err error) *mock.Call {
}

// RegisterGenerateSuccess registers a mock call for successful generation.
func (m *MockCapabilityTranspile) RegisterGenerateSuccess(t *testing.T, filePath string, fileContent string, assessment metrics.Assessments) *mock.Call {
func (m *MockCapabilityTranspile) RegisterGenerateSuccess(t *testing.T, validateContext func(t *testing.T, c model.Context), filePath string, fileContent string, assessment metrics.Assessments) *mock.Call {
return m.On("Transpile", mock.Anything).Return(assessment, nil).Run(func(args mock.Arguments) {
ctx := args.Get(0).(model.Context)
if validateContext != nil {
validateContext(t, ctx)
}
require.NoError(t, os.WriteFile(filepath.Join(ctx.RepositoryPath, filePath), []byte(fileContent), 0600))
})
}
Expand Down

0 comments on commit 7abda1d

Please sign in to comment.