From bb7a81ede4eeafb14fb2e5f056b6cc9df74a79da Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Wed, 23 Nov 2022 19:06:24 +0100 Subject: [PATCH] refactoring: unify, extract&reuse maybeCloneLinguist() --- benchmark_test.go | 55 ++----------- common_test.go | 80 +++++++++++-------- .../generator/generator_test.go | 46 ++++++----- 3 files changed, 76 insertions(+), 105 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 1c7b98c..252a67d 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -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 { diff --git a/common_test.go b/common_test.go index d99b4de..16e6b1f 100644 --- a/common_test.go +++ b/common_test.go @@ -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 diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 9e3a14e..fc53b69 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -97,9 +97,9 @@ var ( type GeneratorTestSuite struct { suite.Suite - tmpLinguistDir string - isLinguistCloned bool - testCases []testCase + tmpLinguistDir string + isCleanupNeeded bool + testCases []testCase } type testCase struct { @@ -122,27 +122,31 @@ 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) - - 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) + 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) } 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) } }