refactoring: unify, extract&reuse maybeCloneLinguist()

This commit is contained in:
Alex Bezzubov
2022-11-23 19:06:24 +01:00
parent a3304aa121
commit bb7a81ede4
3 changed files with 76 additions and 105 deletions

View File

@ -6,11 +6,8 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/go-enry/go-enry/v2/data"
)
type sample struct {
@ -23,22 +20,21 @@ var (
overcomeLanguage string
overcomeLanguages []string
samples []*sample
samplesDir string
cloned bool
)
func TestMain(m *testing.M) {
flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too")
flag.Parse()
if err := cloneLinguist(linguistURL); err != nil {
tmpLinguistDir, cleanupNeeded, err := maybeCloneLinguist()
if err != nil {
log.Fatal(err)
}
if cloned {
defer os.RemoveAll(filepath.Dir(samplesDir))
if cleanupNeeded {
defer os.RemoveAll(tmpLinguistDir)
}
var err error
samplesDir := filepath.Join(tmpLinguistDir, "samples")
samples, err = getSamples(samplesDir)
if err != nil {
log.Fatal(err)
@ -47,47 +43,6 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func cloneLinguist(linguistURL string) error {
repoLinguist := os.Getenv(linguistClonedEnvVar)
cloned = repoLinguist == ""
if cloned {
var err error
repoLinguist, err = ioutil.TempDir("", "linguist-")
if err != nil {
return err
}
}
samplesDir = filepath.Join(repoLinguist, "samples")
if cloned {
cmd := exec.Command("git", "clone", linguistURL, repoLinguist)
if err := cmd.Run(); err != nil {
return err
}
}
cwd, err := os.Getwd()
if err != nil {
return err
}
if err = os.Chdir(repoLinguist); err != nil {
return err
}
cmd := exec.Command("git", "checkout", data.LinguistCommit)
if err := cmd.Run(); err != nil {
return err
}
if err = os.Chdir(cwd); err != nil {
return err
}
return nil
}
func getSamples(dir string) ([]*sample, error) {
samples := make([]*sample, 0, 2000)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {