Merge pull request #170 from go-enry/tiny-fixes-2

test: fix LanguagesByFilename case & improve logging
This commit is contained in:
Alex 2023-09-14 00:03:53 +02:00 committed by GitHub
commit c3899f12ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 72 deletions

View File

@ -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/

View File

@ -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 {

View File

@ -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()",

48
internal/tests/utils.go Normal file
View File

@ -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
}