Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion evaluate/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Evaluate(ctx *Context) (assessments report.AssessmentPerModelPerLanguagePer
repositoryPath := filepath.Join(language.ID(), RepositoryPlainName)
temporaryRepositoryPath, cleanup, err := TemporaryRepository(ctx.Log, filepath.Join(ctx.TestdataPath, repositoryPath))
if err != nil {
ctx.Log.Panicf("ERROR: unable to create temporary repository path: %s", err)
ctx.Log.Panicf("ERROR: unable to create temporary repository path: %+v", err)
}

defer cleanup()
Expand Down
28 changes: 28 additions & 0 deletions evaluate/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

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

"github.com/symflower/eval-dev-quality/evaluate/metrics"
"github.com/symflower/eval-dev-quality/language"
Expand Down Expand Up @@ -86,6 +87,26 @@ func TemporaryRepository(logger *log.Logger, dataPath string) (temporaryReposito
return "", cleanup, pkgerrors.WithStack(err)
}

// Add a default git configuration.
if err := os.MkdirAll(filepath.Join(temporaryRepositoryPath, ".git"), 0700); err != nil {
return "", cleanup, pkgerrors.WithStack(err)
}
if err := os.WriteFile(filepath.Join(temporaryRepositoryPath, ".git", "config"), bytesutil.TrimIndentations([]byte(`
[user]
name = dummy-name-temporary-repository
email = dummy.mail@temporary.repository
username = dummy_username_temporary_repository
[init]
defaultBranch = main
`)), 0600); err != nil {
return "", cleanup, pkgerrors.WithStack(err)
}
// Overwrite the global and system configs to point to the default one.
environment := map[string]string{
"GIT_CONFIG_GLOBAL": filepath.Join(temporaryRepositoryPath, ".git", "config"),
"GIT_CONFIG_SYSTEM": filepath.Join(temporaryRepositoryPath, ".git", "config"),
}

// Add git repository and commit changes.
out, err := util.CommandWithResult(context.Background(), logger, &util.Command{
Command: []string{
Expand All @@ -94,6 +115,7 @@ func TemporaryRepository(logger *log.Logger, dataPath string) (temporaryReposito
},

Directory: temporaryRepositoryPath,
Env: environment,
})
if err != nil {
return "", cleanup, pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to initialize git repository", out)))
Expand All @@ -107,6 +129,7 @@ func TemporaryRepository(logger *log.Logger, dataPath string) (temporaryReposito
},

Directory: temporaryRepositoryPath,
Env: environment,
})
if err != nil {
return "", cleanup, pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to add files", out)))
Expand All @@ -121,6 +144,7 @@ func TemporaryRepository(logger *log.Logger, dataPath string) (temporaryReposito
},

Directory: temporaryRepositoryPath,
Env: environment,
})
if err != nil {
return "", cleanup, pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to commit", out)))
Expand All @@ -139,6 +163,10 @@ func ResetTemporaryRepository(logger *log.Logger, path string) (err error) {
},

Directory: path,
Env: map[string]string{ // Overwrite the global and system configs to point to the default one.
"GIT_CONFIG_GLOBAL": filepath.Join(path, ".git", "config"),
"GIT_CONFIG_SYSTEM": filepath.Join(path, ".git", "config"),
},
})
if err != nil {
return pkgerrors.WithStack(pkgerrors.Wrap(err, fmt.Sprintf("%s - %s", "unable to clean git repository", out)))
Expand Down
27 changes: 26 additions & 1 deletion evaluate/repository_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evaluate

import (
"context"
"os"
"path/filepath"
"sort"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/symflower/eval-dev-quality/log"
"github.com/symflower/eval-dev-quality/model"
"github.com/symflower/eval-dev-quality/model/symflower"
"github.com/symflower/eval-dev-quality/util"
)

func TestRepository(t *testing.T) {
Expand Down Expand Up @@ -109,6 +111,7 @@ func TestTemporaryRepository(t *testing.T) {

ExpectedTempPathRegex string
ExpectedErr error
ValidateAfter func(t *testing.T, logger *log.Logger, repositoryPath string)
}

validate := func(t *testing.T, tc *testCase) {
Expand All @@ -117,12 +120,22 @@ func TestTemporaryRepository(t *testing.T) {
t.Skipf("Regex tests with paths are not supported on this OS")
}

_, logger := log.Buffer()
logBuffer, logger := log.Buffer()
defer func() {
if t.Failed() {
t.Logf("Log Output:\n%s", logBuffer.String())
}
}()

actualTemporaryRepositoryPath, cleanup, actualErr := TemporaryRepository(logger, filepath.Join(tc.TestDataPath, tc.RepositoryPath))
defer cleanup()

assert.Regexp(t, filepath.Join(os.TempDir(), tc.ExpectedTempPathRegex), actualTemporaryRepositoryPath, actualTemporaryRepositoryPath)
assert.Equal(t, tc.ExpectedErr, actualErr)

if tc.ValidateAfter != nil {
tc.ValidateAfter(t, logger, actualTemporaryRepositoryPath)
}
})
}

Expand All @@ -134,6 +147,18 @@ func TestTemporaryRepository(t *testing.T) {

ExpectedTempPathRegex: `eval-dev-quality\d+\/plain`,
ExpectedErr: nil,
ValidateAfter: func(t *testing.T, logger *log.Logger, repositoryPath string) {
output, err := util.CommandWithResult(context.Background(), logger, &util.Command{
Command: []string{
"git",
"log",
},

Directory: repositoryPath,
})
require.NoError(t, err)
assert.Contains(t, output, "Author: dummy-name-temporary-repository")
},
})
}

Expand Down