mirror of
https://github.com/ralsina/tartrazine.git
synced 2024-11-10 05:22:23 +00:00
test: refactor a single maybeCloneLinguist() impl
This commit is contained in:
parent
b5441048b3
commit
b41b4e14fe
@ -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 {
|
||||
|
@ -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
48
internal/tests/utils.go
Normal 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", "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
|
||||
}
|
Loading…
Reference in New Issue
Block a user