From b5441048b35544920609839edacbdf593e04b3dc Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Wed, 6 Sep 2023 20:46:47 +0300 Subject: [PATCH 1/5] test: actually a correct LanguagesByFilename case :/ --- common_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common_test.go b/common_test.go index 4e36193..4bfa255 100644 --- a/common_test.go +++ b/common_test.go @@ -275,7 +275,7 @@ func (s *enryTestSuite) TestGetLanguagesByFilename() { {name: "TestGetLanguagesByFilename_7", filename: "_vimrc", expected: []string{"Vim Script"}}, {name: "TestGetLanguagesByFilename_8", filename: "pom.xml", expected: []string{"Maven POM"}}, {name: "TestGetLanguagesByFilename_9", filename: "", expected: nil}, - {name: "TestGetLanguagesByFilename_10", filename: "hi.py", expected: []string{"Python"}}, + {name: "TestGetLanguagesByFilename_10", filename: "hi.py", expected: nil}, // LanguagesByFilename is only for the well-known file names (.gitignore, etc) } for _, test := range tests { From b41b4e14fe454144d7d4fc43996698e8f6797dc7 Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Wed, 6 Sep 2023 21:03:08 +0300 Subject: [PATCH 2/5] test: refactor a single maybeCloneLinguist() impl --- common_test.go | 39 +-------------- .../generator/generator_test.go | 40 +++------------- internal/tests/utils.go | 48 +++++++++++++++++++ 3 files changed, 57 insertions(+), 70 deletions(-) create mode 100644 internal/tests/utils.go diff --git a/common_test.go b/common_test.go index 4bfa255..cfef1cc 100644 --- a/common_test.go +++ b/common_test.go @@ -4,12 +4,12 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" "strings" "testing" "github.com/go-enry/go-enry/v2/data" + "github.com/go-enry/go-enry/v2/internal/tests" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -19,43 +19,8 @@ import ( const linguistURL = "https://github.com/github/linguist.git" const linguistClonedEnvVar = "ENRY_TEST_REPO" -// not a part of the test Suite as benchmark does not use testify 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", "--depth", "100", 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 + return tests.MaybeCloneLinguist(linguistClonedEnvVar, linguistURL, data.LinguistCommit) } type enryBaseTestSuite struct { diff --git a/internal/code-generator/generator/generator_test.go b/internal/code-generator/generator/generator_test.go index 7c041b7..cbe749a 100644 --- a/internal/code-generator/generator/generator_test.go +++ b/internal/code-generator/generator/generator_test.go @@ -5,11 +5,12 @@ import ( "fmt" "io/ioutil" "os" - "os/exec" "path/filepath" "strings" "testing" + "github.com/go-enry/go-enry/v2/data" + "github.com/go-enry/go-enry/v2/internal/tests" "github.com/go-enry/go-enry/v2/internal/tokenizer" "github.com/stretchr/testify/assert" @@ -18,8 +19,8 @@ import ( ) var ( - linguistURL = "https://github.com/github/linguist.git" linguistClonedEnvVar = "ENRY_TEST_REPO" + linguistURL = "https://github.com/github/linguist.git" commit = "bf853f1c663903e3ee35935189760191f1c45e1c" samplesDir = "samples" languagesFile = filepath.Join("lib", "linguist", "languages.yml") @@ -120,38 +121,11 @@ func Test_GeneratorTestSuite(t *testing.T) { suite.Run(t, new(GeneratorTestSuite)) } -func (s *GeneratorTestSuite) maybeCloneLinguist() { - var err error - s.tmpLinguistDir = os.Getenv(linguistClonedEnvVar) - isLinguistCloned := s.tmpLinguistDir != "" - if !isLinguistCloned { - s.tmpLinguistDir, err = ioutil.TempDir("", "linguist-") - require.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", "--depth", "100", linguistURL, s.tmpLinguistDir) - err = cmd.Run() - require.NoError(s.T(), err) - s.isCleanupNeeded = true - } - - cwd, err := os.Getwd() - require.NoError(s.T(), err) - - err = os.Chdir(s.tmpLinguistDir) - require.NoError(s.T(), err) - - cmd := exec.Command("git", "checkout", commit) - err = cmd.Run() - require.NoError(s.T(), err) - - err = os.Chdir(cwd) - require.NoError(s.T(), err) -} - func (s *GeneratorTestSuite) SetupSuite() { - s.maybeCloneLinguist() + var err error + s.tmpLinguistDir, s.isCleanupNeeded, err = tests.MaybeCloneLinguist(linguistClonedEnvVar, linguistURL, data.LinguistCommit) + require.NoError(s.T(), err) + s.testCases = []testCase{ { name: "Extensions()", diff --git a/internal/tests/utils.go b/internal/tests/utils.go new file mode 100644 index 0000000..4d74e30 --- /dev/null +++ b/internal/tests/utils.go @@ -0,0 +1,48 @@ +package tests + +import ( + "fmt" + "io/ioutil" + "os" + "os/exec" +) + +// Re-used by the packages: enry (test), enry (benchmark) and code-generator (test). +// Does not rely on testify, panics on errors so that there always is a trace to the caller. +func MaybeCloneLinguist(envVar, url, commit string) (string, bool, error) { + var err error + linguistTmpDir := os.Getenv(envVar) + isCleanupNeeded := false + isLinguistCloned := linguistTmpDir != "" + if !isLinguistCloned { + linguistTmpDir, err = ioutil.TempDir("", "linguist-") + if err != nil { + return "", false, err + } + + isCleanupNeeded = true + cmd := exec.Command("git", "clone", "--depth", "100", url, linguistTmpDir) + if err := cmd.Run(); err != nil { + panic(fmt.Errorf("%s: %w", cmd.String(), err)) + } + } + + cwd, err := os.Getwd() + if err != nil { + panic(err) + } + + if err = os.Chdir(linguistTmpDir); err != nil { + panic(err) + } + + cmd := exec.Command("git", "checkout", commit) + if err := cmd.Run(); err != nil { + panic(fmt.Errorf("%s: %w", cmd.String(), err)) + } + + if err = os.Chdir(cwd); err != nil { + panic(fmt.Errorf("%s: %w", cmd.String(), err)) + } + return linguistTmpDir, isCleanupNeeded, nil +} From 59bfa6e3e3d8d8e44fe5ddc71301d7349caae302 Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Wed, 6 Sep 2023 23:23:43 +0300 Subject: [PATCH 3/5] test: increase Linguist shallow clone depth --- internal/tests/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/tests/utils.go b/internal/tests/utils.go index 4d74e30..0e2b411 100644 --- a/internal/tests/utils.go +++ b/internal/tests/utils.go @@ -21,7 +21,7 @@ func MaybeCloneLinguist(envVar, url, commit string) (string, bool, error) { } isCleanupNeeded = true - cmd := exec.Command("git", "clone", "--depth", "100", url, linguistTmpDir) + cmd := exec.Command("git", "clone", "--depth", "150", url, linguistTmpDir) if err := cmd.Run(); err != nil { panic(fmt.Errorf("%s: %w", cmd.String(), err)) } From 62cb8695d25abcba42d1f90b4c443f94f5829f87 Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Thu, 7 Sep 2023 00:14:18 +0300 Subject: [PATCH 4/5] ci: python build verbosity --- .github/workflows/pyTest.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pyTest.yml b/.github/workflows/pyTest.yml index 5cd0f72..f0f4a0f 100644 --- a/.github/workflows/pyTest.yml +++ b/.github/workflows/pyTest.yml @@ -21,7 +21,7 @@ jobs: if [ -f python/requirements.dev.txt ]; then pip install -r python/requirements.dev.txt; fi - name: Build and install package run: | - pip install -e python + pip -v install -e python - name: Test run: | pytest python/ From c7dc5fb77aed3a94cacb5264ca915b1e15cfca56 Mon Sep 17 00:00:00 2001 From: Alex Bezzubov Date: Wed, 13 Sep 2023 23:40:35 +0200 Subject: [PATCH 5/5] ci: force distutils for building python --- .github/workflows/pyTest.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pyTest.yml b/.github/workflows/pyTest.yml index f0f4a0f..25898e2 100644 --- a/.github/workflows/pyTest.yml +++ b/.github/workflows/pyTest.yml @@ -21,7 +21,8 @@ jobs: if [ -f python/requirements.dev.txt ]; then pip install -r python/requirements.dev.txt; fi - name: Build and install package run: | - pip -v install -e python + pip install setuptools wheel + pip -v install --no-use-pep517 -e python - name: Test run: | pytest python/