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 {

View File

@ -19,10 +19,48 @@ import (
const linguistURL = "https://github.com/github/linguist.git"
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 {
suite.Suite
tmpLinguist string
needToClone bool
tmpLinguistDir string
isCleanupNeeded bool
samplesDir string
testFixturesDir string
}
@ -41,7 +79,7 @@ func (s *EnryTestSuite) TestRegexpEdgeCases() {
}
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)
require.NoError(s.T(), err)
@ -60,40 +98,16 @@ func Test_EnryTestSuite(t *testing.T) {
func (s *EnryTestSuite) SetupSuite() {
var err error
s.tmpLinguist = os.Getenv(linguistClonedEnvVar)
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()
s.tmpLinguistDir, s.isCleanupNeeded, err = maybeCloneLinguist()
assert.NoError(s.T(), err)
err = os.Chdir(s.tmpLinguist)
assert.NoError(s.T(), err)
cmd := exec.Command("git", "checkout", data.LinguistCommit)
err = cmd.Run()
assert.NoError(s.T(), err)
err = os.Chdir(cwd)
assert.NoError(s.T(), err)
s.samplesDir = filepath.Join(s.tmpLinguistDir, "samples")
s.testFixturesDir = filepath.Join(s.tmpLinguistDir, "test", "fixtures")
}
func (s *EnryTestSuite) TearDownSuite() {
if s.needToClone {
err := os.RemoveAll(s.tmpLinguist)
if s.isCleanupNeeded {
err := os.RemoveAll(s.tmpLinguistDir)
assert.NoError(s.T(), err)
}
}
@ -153,7 +167,7 @@ func (s *EnryTestSuite) TestGetLanguages() {
}
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 {
name string

View File

@ -98,7 +98,7 @@ var (
type GeneratorTestSuite struct {
suite.Suite
tmpLinguistDir string
isLinguistCloned bool
isCleanupNeeded bool
testCases []testCase
}
@ -122,13 +122,18 @@ func Test_GeneratorTestSuite(t *testing.T) {
func (s *GeneratorTestSuite) maybeCloneLinguist() {
var err error
s.tmpLinguistDir = os.Getenv(linguistClonedEnvVar)
s.isLinguistCloned = s.tmpLinguistDir != ""
if !s.isLinguistCloned {
isLinguistCloned := s.tmpLinguistDir != ""
if !isLinguistCloned {
s.tmpLinguistDir, err = ioutil.TempDir("", "linguist-")
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)
err = cmd.Run()
assert.NoError(s.T(), err)
s.isCleanupNeeded = true
}
cwd, err := os.Getwd()
assert.NoError(s.T(), err)
@ -136,13 +141,12 @@ func (s *GeneratorTestSuite) maybeCloneLinguist() {
err = os.Chdir(s.tmpLinguistDir)
assert.NoError(s.T(), err)
cmd = exec.Command("git", "checkout", commit)
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() {
@ -280,11 +284,9 @@ func (s *GeneratorTestSuite) SetupSuite() {
}
func (s *GeneratorTestSuite) TearDownSuite() {
if s.isLinguistCloned {
if s.isCleanupNeeded {
err := os.RemoveAll(s.tmpLinguistDir)
if err != nil {
s.T().Logf("Failed to clean up %s after the test.\n", s.tmpLinguistDir)
}
assert.NoError(s.T(), err)
}
}