Skip to content

Commit df7844e

Browse files
committed
test: add test for content heuristics edge cases
Signed-off-by: Alexander Bezzubov <[email protected]>
1 parent dceb95a commit df7844e

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

common_test.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gopkg.in/src-d/enry.v1/data"
1212

1313
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
1415
"github.com/stretchr/testify/suite"
1516
)
1617

@@ -19,9 +20,35 @@ const linguistClonedEnvVar = "ENRY_TEST_REPO"
1920

2021
type EnryTestSuite struct {
2122
suite.Suite
22-
repoLinguist string
23-
samplesDir string
24-
cloned bool
23+
tmpLinguist string
24+
needToClone bool
25+
samplesDir string
26+
}
27+
28+
func TestRegexpEdgeCases(t *testing.T) {
29+
var regexpEdgeCases = []struct {
30+
lang string
31+
filename string
32+
}{
33+
{lang: "ActionScript", filename: "FooBar.as"},
34+
{lang: "Forth", filename: "asm.fr"},
35+
{lang: "X PixMap", filename: "cc-public_domain_mark_white.pm"},
36+
//{lang: "SQL", filename: "drop_stuff.sql"}, // Classifier strategy fails :/
37+
//{lang: "Fstar", filename: "Hacl.Spec.Bignum.Fmul.fst"}, // *.fst fails on GetLanguagesByExtension,
38+
{lang: "C++", filename: "Types.h"},
39+
}
40+
41+
for _, r := range regexpEdgeCases {
42+
filename := fmt.Sprintf(".linguist/samples/%s/%s", r.lang, r.filename)
43+
44+
content, err := ioutil.ReadFile(filename)
45+
require.NoError(t, err)
46+
47+
lang := GetLanguage(r.filename, content)
48+
t.Logf("File:%s, lang:%s", filename, lang)
49+
50+
assert.EqualValues(t, r.lang, lang)
51+
}
2552
}
2653

2754
func Test_EnryTestSuite(t *testing.T) {
@@ -30,25 +57,24 @@ func Test_EnryTestSuite(t *testing.T) {
3057

3158
func (s *EnryTestSuite) SetupSuite() {
3259
var err error
33-
s.repoLinguist = os.Getenv(linguistClonedEnvVar)
34-
s.cloned = s.repoLinguist == ""
35-
if s.cloned {
36-
s.repoLinguist, err = ioutil.TempDir("", "linguist-")
37-
assert.NoError(s.T(), err)
38-
}
39-
40-
s.samplesDir = filepath.Join(s.repoLinguist, "samples")
41-
42-
if s.cloned {
43-
cmd := exec.Command("git", "clone", linguistURL, s.repoLinguist)
60+
s.tmpLinguist = os.Getenv(linguistClonedEnvVar)
61+
s.needToClone = s.tmpLinguist == ""
62+
if s.needToClone {
63+
s.tmpLinguist, err = ioutil.TempDir("", "linguist-")
64+
require.NoError(s.T(), err)
65+
fmt.Printf("Cloning Linguist repo to '%s' as %s was not set\n",
66+
s.tmpLinguist, linguistClonedEnvVar)
67+
cmd := exec.Command("git", "clone", linguistURL, s.tmpLinguist)
4468
err = cmd.Run()
45-
assert.NoError(s.T(), err)
69+
require.NoError(s.T(), err)
4670
}
71+
s.samplesDir = filepath.Join(s.tmpLinguist, "samples")
72+
s.T().Logf("using samples from %s", s.samplesDir)
4773

4874
cwd, err := os.Getwd()
4975
assert.NoError(s.T(), err)
5076

51-
err = os.Chdir(s.repoLinguist)
77+
err = os.Chdir(s.tmpLinguist)
5278
assert.NoError(s.T(), err)
5379

5480
cmd := exec.Command("git", "checkout", data.LinguistCommit)
@@ -60,8 +86,8 @@ func (s *EnryTestSuite) SetupSuite() {
6086
}
6187

6288
func (s *EnryTestSuite) TearDownSuite() {
63-
if s.cloned {
64-
err := os.RemoveAll(s.repoLinguist)
89+
if s.needToClone {
90+
err := os.RemoveAll(s.tmpLinguist)
6591
assert.NoError(s.T(), err)
6692
}
6793
}
@@ -88,7 +114,7 @@ func (s *EnryTestSuite) TestGetLanguage() {
88114
}
89115

90116
func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() {
91-
var modelinesDir = filepath.Join(s.repoLinguist, "test/fixtures/Data/Modelines")
117+
var modelinesDir = filepath.Join(s.tmpLinguist, "test/fixtures/Data/Modelines")
92118

93119
tests := []struct {
94120
name string
@@ -400,7 +426,7 @@ func (s *EnryTestSuite) TestGetLanguageByAlias() {
400426
func (s *EnryTestSuite) TestLinguistCorpus() {
401427
const filenamesDir = "filenames"
402428
var cornerCases = map[string]bool{
403-
"hello.ms": true,
429+
"hello.ms": true, //TODO add instead .es
404430
}
405431

406432
var total, failed, ok, other int
@@ -443,5 +469,5 @@ func (s *EnryTestSuite) TestLinguistCorpus() {
443469
return nil
444470
})
445471

446-
fmt.Printf("\t\ttotal files: %d, ok: %d, failed: %d, other: %d\n", total, ok, failed, other)
472+
s.T().Logf("\t\ttotal files: %d, ok: %d, failed: %d, other: %d\n", total, ok, failed, other)
447473
}

0 commit comments

Comments
 (0)