diff --git a/.github/workflows/pyTest.yml b/.github/workflows/pyTest.yml index 5cd0f72..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 install -e python + pip install setuptools wheel + pip -v install --no-use-pep517 -e python - name: Test run: | pytest python/ diff --git a/common_test.go b/common_test.go index 4e36193..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 { @@ -275,7 +240,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 { 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..0e2b411 --- /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", "150", 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 +}