From 250519bb51e63dd0f1a6fda3d1fca2c7d60058de Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Thu, 28 Sep 2017 20:58:13 +0200 Subject: [PATCH 1/3] Add the external test linguist dir from env var This allows to use a cached directory with linguist instead of cloning and speeds up the tests by -10s on my local machine. Signed-off-by: Vadim Markovtsev --- common_test.go | 25 +++++++++++------ .../generator/generator_test.go | 27 ++++++++++++------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/common_test.go b/common_test.go index 8c53a6f..019e743 100644 --- a/common_test.go +++ b/common_test.go @@ -20,6 +20,7 @@ type EnryTestSuite struct { suite.Suite repoLinguist string samplesDir string + cloned bool } func TestEnryTestSuite(t *testing.T) { @@ -28,14 +29,20 @@ func TestEnryTestSuite(t *testing.T) { func (s *EnryTestSuite) SetupSuite() { var err error - s.repoLinguist, err = ioutil.TempDir("", "linguist-") - assert.NoError(s.T(), err) + s.repoLinguist = os.Getenv("ENRY_TEST_REPO") + s.cloned = len(s.repoLinguist) == 0 + if s.cloned { + s.repoLinguist, err = ioutil.TempDir("", "linguist-") + assert.NoError(s.T(), err) + } s.samplesDir = filepath.Join(s.repoLinguist, "samples") - cmd := exec.Command("git", "clone", linguistURL, s.repoLinguist) - err = cmd.Run() - assert.NoError(s.T(), err) + if s.cloned { + cmd := exec.Command("git", "clone", linguistURL, s.repoLinguist) + err = cmd.Run() + assert.NoError(s.T(), err) + } cwd, err := os.Getwd() assert.NoError(s.T(), err) @@ -43,7 +50,7 @@ func (s *EnryTestSuite) SetupSuite() { err = os.Chdir(s.repoLinguist) assert.NoError(s.T(), err) - cmd = exec.Command("git", "checkout", data.LinguistCommit) + cmd := exec.Command("git", "checkout", data.LinguistCommit) err = cmd.Run() assert.NoError(s.T(), err) @@ -52,8 +59,10 @@ func (s *EnryTestSuite) SetupSuite() { } func (s *EnryTestSuite) TearDownSuite() { - err := os.RemoveAll(s.repoLinguist) - assert.NoError(s.T(), err) + if s.cloned { + err := os.RemoveAll(s.repoLinguist) + assert.NoError(s.T(), err) + } } func (s *EnryTestSuite) TestGetLanguage() { diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index efac26a..9177731 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -13,7 +13,7 @@ import ( ) const ( - lingustURL = "https://github.com/github/linguist.git" + linguistURL = "https://github.com/github/linguist.git" commit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68" samplesDir = "samples" languagesFile = "lib/linguist/languages.yml" @@ -80,6 +80,7 @@ const ( type GeneratorTestSuite struct { suite.Suite tmpLinguist string + cloned bool } func TestGeneratorTestSuite(t *testing.T) { @@ -88,12 +89,18 @@ func TestGeneratorTestSuite(t *testing.T) { func (s *GeneratorTestSuite) SetupSuite() { var err error - s.tmpLinguist, err = ioutil.TempDir("", "linguist-") - assert.NoError(s.T(), err) + s.tmpLinguist = os.Getenv("ENRY_TEST_REPO") + s.cloned = len(s.tmpLinguist) == 0 + if s.cloned { + s.tmpLinguist, err = ioutil.TempDir("", "linguist-") + assert.NoError(s.T(), err) + } - cmd := exec.Command("git", "clone", lingustURL, s.tmpLinguist) - err = cmd.Run() - assert.NoError(s.T(), err) + if s.cloned { + cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist) + err = cmd.Run() + assert.NoError(s.T(), err) + } cwd, err := os.Getwd() assert.NoError(s.T(), err) @@ -101,7 +108,7 @@ func (s *GeneratorTestSuite) SetupSuite() { err = os.Chdir(s.tmpLinguist) 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) @@ -110,8 +117,10 @@ func (s *GeneratorTestSuite) SetupSuite() { } func (s *GeneratorTestSuite) TearDownSuite() { - err := os.RemoveAll(s.tmpLinguist) - assert.NoError(s.T(), err) + if s.cloned { + err := os.RemoveAll(s.tmpLinguist) + assert.NoError(s.T(), err) + } } func (s *GeneratorTestSuite) TestGenerationFiles() { From c97a180da5f7c55269f84dbc907805040ba8dc0e Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Thu, 26 Oct 2017 15:37:18 +0200 Subject: [PATCH 2/3] Fix review suggestions Signed-off-by: Vadim Markovtsev --- benchmark_test.go | 26 +++++++++++++------ common_test.go | 5 ++-- .../generator/generator_test.go | 7 ++--- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/benchmark_test.go b/benchmark_test.go index 9ad8289..ca42191 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -23,6 +23,7 @@ var ( overcomeLanguages []string samples []*sample samplesDir string + cloned bool ) func TestMain(m *testing.M) { @@ -35,7 +36,9 @@ func TestMain(m *testing.M) { if err := cloneLinguist(linguistURL); err != nil { log.Fatal(err) } - defer os.RemoveAll(filepath.Dir(samplesDir)) + if cloned { + defer os.RemoveAll(filepath.Dir(samplesDir)) + } var err error samples, err = getSamples(samplesDir) @@ -47,16 +50,23 @@ func TestMain(m *testing.M) { } func cloneLinguist(linguistURL string) error { - repoLinguist, err := ioutil.TempDir("", "linguist-") - if err != nil { - return err + 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") - cmd := exec.Command("git", "clone", linguistURL, repoLinguist) - if err := cmd.Run(); err != nil { - return err + if cloned { + cmd := exec.Command("git", "clone", linguistURL, repoLinguist) + if err := cmd.Run(); err != nil { + return err + } } cwd, err := os.Getwd() @@ -68,7 +78,7 @@ func cloneLinguist(linguistURL string) error { return err } - cmd = exec.Command("git", "checkout", data.LinguistCommit) + cmd := exec.Command("git", "checkout", data.LinguistCommit) if err := cmd.Run(); err != nil { return err } diff --git a/common_test.go b/common_test.go index 019e743..de332de 100644 --- a/common_test.go +++ b/common_test.go @@ -15,6 +15,7 @@ import ( ) const linguistURL = "https://github.com/github/linguist.git" +const linguistClonedEnvVar = "ENRY_TEST_REPO" type EnryTestSuite struct { suite.Suite @@ -29,8 +30,8 @@ func TestEnryTestSuite(t *testing.T) { func (s *EnryTestSuite) SetupSuite() { var err error - s.repoLinguist = os.Getenv("ENRY_TEST_REPO") - s.cloned = len(s.repoLinguist) == 0 + s.repoLinguist = os.Getenv(linguistClonedEnvVar) + s.cloned = s.repoLinguist == "" if s.cloned { s.repoLinguist, err = ioutil.TempDir("", "linguist-") assert.NoError(s.T(), err) diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 9177731..07ba0df 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -13,7 +13,8 @@ import ( ) const ( - linguistURL = "https://github.com/github/linguist.git" + linguistURL = "https://github.com/github/linguist.git" + linguistClonedEnvVar = "ENRY_TEST_REPO" commit = "d5c8db3fb91963c4b2762ca2ea2ff7cfac109f68" samplesDir = "samples" languagesFile = "lib/linguist/languages.yml" @@ -89,8 +90,8 @@ func TestGeneratorTestSuite(t *testing.T) { func (s *GeneratorTestSuite) SetupSuite() { var err error - s.tmpLinguist = os.Getenv("ENRY_TEST_REPO") - s.cloned = len(s.tmpLinguist) == 0 + s.tmpLinguist = os.Getenv(linguistClonedEnvVar) + s.cloned = s.tmpLinguist == "" if s.cloned { s.tmpLinguist, err = ioutil.TempDir("", "linguist-") assert.NoError(s.T(), err) From 09d6add804e8da1217ffee01141242d9d3d5cf08 Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Thu, 26 Oct 2017 17:02:58 +0200 Subject: [PATCH 3/3] Fix review Signed-off-by: Vadim Markovtsev --- internal/code-generator/generator/generator_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 07ba0df..26926df 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -95,9 +95,6 @@ func (s *GeneratorTestSuite) SetupSuite() { if s.cloned { s.tmpLinguist, err = ioutil.TempDir("", "linguist-") assert.NoError(s.T(), err) - } - - if s.cloned { cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist) err = cmd.Run() assert.NoError(s.T(), err)