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" "io/ioutil"
"log" "log"
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/go-enry/go-enry/v2/data"
) )
type sample struct { type sample struct {
@ -23,22 +20,21 @@ var (
overcomeLanguage string overcomeLanguage string
overcomeLanguages []string overcomeLanguages []string
samples []*sample samples []*sample
samplesDir string
cloned bool
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too") flag.BoolVar(&slow, "slow", false, "run benchmarks per sample for strategies too")
flag.Parse() flag.Parse()
if err := cloneLinguist(linguistURL); err != nil { tmpLinguistDir, cleanupNeeded, err := maybeCloneLinguist()
if err != nil {
log.Fatal(err) log.Fatal(err)
} }
if cloned { if cleanupNeeded {
defer os.RemoveAll(filepath.Dir(samplesDir)) defer os.RemoveAll(tmpLinguistDir)
} }
var err error samplesDir := filepath.Join(tmpLinguistDir, "samples")
samples, err = getSamples(samplesDir) samples, err = getSamples(samplesDir)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -47,47 +43,6 @@ func TestMain(m *testing.M) {
os.Exit(m.Run()) 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) { func getSamples(dir string) ([]*sample, error) {
samples := make([]*sample, 0, 2000) samples := make([]*sample, 0, 2000)
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {

View File

@ -19,10 +19,48 @@ import (
const linguistURL = "https://github.com/github/linguist.git" const linguistURL = "https://github.com/github/linguist.git"
const linguistClonedEnvVar = "ENRY_TEST_REPO" const linguistClonedEnvVar = "ENRY_TEST_REPO"
func maybeCloneLinguist() (string, bool, error) {
var err error
linguistTmpDir := os.Getenv(linguistClonedEnvVar)
isCleanupNeeded := false
isLinguistCloned := linguistTmpDir != ""
if !isLinguistCloned {
linguistTmpDir, err = ioutil.TempDir("", "linguist-")
if err != nil {
return "", false, err
}
isCleanupNeeded = true
cmd := exec.Command("git", "clone", linguistURL, linguistTmpDir)
if err := cmd.Run(); err != nil {
return linguistTmpDir, isCleanupNeeded, err
}
}
cwd, err := os.Getwd()
if err != nil {
return linguistTmpDir, isCleanupNeeded, err
}
if err = os.Chdir(linguistTmpDir); err != nil {
return linguistTmpDir, isCleanupNeeded, err
}
cmd := exec.Command("git", "checkout", data.LinguistCommit)
if err := cmd.Run(); err != nil {
return linguistTmpDir, isCleanupNeeded, err
}
if err = os.Chdir(cwd); err != nil {
return linguistTmpDir, isCleanupNeeded, err
}
return linguistTmpDir, isCleanupNeeded, nil
}
type EnryTestSuite struct { type EnryTestSuite struct {
suite.Suite suite.Suite
tmpLinguist string tmpLinguistDir string
needToClone bool isCleanupNeeded bool
samplesDir string samplesDir string
testFixturesDir string testFixturesDir string
} }
@ -41,7 +79,7 @@ func (s *EnryTestSuite) TestRegexpEdgeCases() {
} }
for _, r := range regexpEdgeCases { for _, r := range regexpEdgeCases {
filename := filepath.Join(s.tmpLinguist, "samples", r.lang, r.filename) filename := filepath.Join(s.tmpLinguistDir, "samples", r.lang, r.filename)
content, err := ioutil.ReadFile(filename) content, err := ioutil.ReadFile(filename)
require.NoError(s.T(), err) require.NoError(s.T(), err)
@ -60,40 +98,16 @@ func Test_EnryTestSuite(t *testing.T) {
func (s *EnryTestSuite) SetupSuite() { func (s *EnryTestSuite) SetupSuite() {
var err error var err error
s.tmpLinguist = os.Getenv(linguistClonedEnvVar) s.tmpLinguistDir, s.isCleanupNeeded, err = maybeCloneLinguist()
s.needToClone = s.tmpLinguist == ""
if s.needToClone {
s.tmpLinguist, err = ioutil.TempDir("", "linguist-")
require.NoError(s.T(), err)
s.T().Logf("Cloning Linguist repo to '%s' as %s was not set\n",
s.tmpLinguist, linguistClonedEnvVar)
cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist)
err = cmd.Run()
require.NoError(s.T(), err)
}
s.samplesDir = filepath.Join(s.tmpLinguist, "samples")
s.T().Logf("using samples from %s", s.samplesDir)
s.testFixturesDir = filepath.Join(s.tmpLinguist, "test", "fixtures")
s.T().Logf("using test fixtures from %s", s.samplesDir)
cwd, err := os.Getwd()
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
err = os.Chdir(s.tmpLinguist) s.samplesDir = filepath.Join(s.tmpLinguistDir, "samples")
assert.NoError(s.T(), err) s.testFixturesDir = filepath.Join(s.tmpLinguistDir, "test", "fixtures")
cmd := exec.Command("git", "checkout", data.LinguistCommit)
err = cmd.Run()
assert.NoError(s.T(), err)
err = os.Chdir(cwd)
assert.NoError(s.T(), err)
} }
func (s *EnryTestSuite) TearDownSuite() { func (s *EnryTestSuite) TearDownSuite() {
if s.needToClone { if s.isCleanupNeeded {
err := os.RemoveAll(s.tmpLinguist) err := os.RemoveAll(s.tmpLinguistDir)
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
} }
} }
@ -153,7 +167,7 @@ func (s *EnryTestSuite) TestGetLanguages() {
} }
func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() { func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() {
var modelinesDir = filepath.Join(s.tmpLinguist, "test", "fixtures", "Data", "Modelines") var modelinesDir = filepath.Join(s.tmpLinguistDir, "test", "fixtures", "Data", "Modelines")
tests := []struct { tests := []struct {
name string name string

View File

@ -97,9 +97,9 @@ var (
type GeneratorTestSuite struct { type GeneratorTestSuite struct {
suite.Suite suite.Suite
tmpLinguistDir string tmpLinguistDir string
isLinguistCloned bool isCleanupNeeded bool
testCases []testCase testCases []testCase
} }
type testCase struct { type testCase struct {
@ -122,27 +122,31 @@ func Test_GeneratorTestSuite(t *testing.T) {
func (s *GeneratorTestSuite) maybeCloneLinguist() { func (s *GeneratorTestSuite) maybeCloneLinguist() {
var err error var err error
s.tmpLinguistDir = os.Getenv(linguistClonedEnvVar) s.tmpLinguistDir = os.Getenv(linguistClonedEnvVar)
s.isLinguistCloned = s.tmpLinguistDir != "" isLinguistCloned := s.tmpLinguistDir != ""
if !s.isLinguistCloned { if !isLinguistCloned {
s.tmpLinguistDir, err = ioutil.TempDir("", "linguist-") s.tmpLinguistDir, err = ioutil.TempDir("", "linguist-")
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
s.T().Logf("Cloning Linguist repo to '%s' as %s was not set\n",
s.tmpLinguistDir, linguistClonedEnvVar)
cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguistDir) cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguistDir)
err = cmd.Run() err = cmd.Run()
assert.NoError(s.T(), err) assert.NoError(s.T(), err)
s.isCleanupNeeded = true
cwd, err := os.Getwd()
assert.NoError(s.T(), err)
err = os.Chdir(s.tmpLinguistDir)
assert.NoError(s.T(), err)
cmd = exec.Command("git", "checkout", commit)
err = cmd.Run()
assert.NoError(s.T(), err)
err = os.Chdir(cwd)
assert.NoError(s.T(), err)
} }
cwd, err := os.Getwd()
assert.NoError(s.T(), err)
err = os.Chdir(s.tmpLinguistDir)
assert.NoError(s.T(), err)
cmd := exec.Command("git", "checkout", commit)
err = cmd.Run()
assert.NoError(s.T(), err)
err = os.Chdir(cwd)
assert.NoError(s.T(), err)
} }
func (s *GeneratorTestSuite) SetupSuite() { func (s *GeneratorTestSuite) SetupSuite() {
@ -280,11 +284,9 @@ func (s *GeneratorTestSuite) SetupSuite() {
} }
func (s *GeneratorTestSuite) TearDownSuite() { func (s *GeneratorTestSuite) TearDownSuite() {
if s.isLinguistCloned { if s.isCleanupNeeded {
err := os.RemoveAll(s.tmpLinguistDir) err := os.RemoveAll(s.tmpLinguistDir)
if err != nil { assert.NoError(s.T(), err)
s.T().Logf("Failed to clean up %s after the test.\n", s.tmpLinguistDir)
}
} }
} }