forked from go-enry/go-enry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinguist_corpus_test.go
More file actions
128 lines (111 loc) · 3.61 KB
/
Copy pathlinguist_corpus_test.go
File metadata and controls
128 lines (111 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package enry
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/go-enry/go-enry/v2/data"
"github.com/go-enry/go-enry/v2/regex"
"github.com/stretchr/testify/suite"
)
type linguistCorpusSuite struct {
enryBaseTestSuite
}
func Test_EnryOnLinguistCorpus(t *testing.T) {
suite.Run(t, new(linguistCorpusSuite))
}
// First part of the test_blob.rb#test_language
// https://github.com/github/linguist/blob/59b2d88b2242e6062384e5fb876668cc30ead951/test/test_blob.rb#L258
func (s *linguistCorpusSuite) TestLinguistSamples() {
const filenamesDir = "filenames"
var cornerCases = map[string]bool{
"textobj-rubyblock.vba": true, // unsupported negative lookahead RE syntax (https://github.com/github/linguist/blob/8083cb5a89cee2d99f5a988f165994d0243f0d1e/lib/linguist/heuristics.yml#L521)
// .es and .ice fail heuristics parsing, but do not fail any tests
// 'Adblock Filter List' hack https://github.com/github/linguist/blob/bf853f1c663903e3ee35935189760191f1c45e1c/lib/linguist/heuristics.yml#L680-L702
"Imperial Units Remover.txt": true,
"abp-filters-anti-cv.txt": true,
"anti-facebook.txt": true,
"fake-news.txt": true,
"test_rules.txt": true,
// backreference in .plist heuristics for "XML Property List" language https://github.com/go-enry/go-enry/pull/169#discussion_r1319889500
// upsteam fix comming in https://github.com/go-enry/go-enry/pull/169#issuecomment-1708840755
"ff-man.plist": true,
"info.min.plist": true,
"info.plist": true,
"man.plist": true,
}
var total, failed, ok, other int
var expected string
filepath.Walk(s.samplesDir, func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
if f.Name() != filenamesDir {
expected, _ = data.LanguageByAlias(f.Name())
}
return nil
}
filename := filepath.Base(path)
content, _ := ioutil.ReadFile(path)
total++
got := GetLanguage(filename, content)
if got == OtherLanguage {
got = "Other"
other++
}
if expected == got {
ok++
} else {
failed++
}
errMsg := fmt.Sprintf("file: %q\texpected: %q\tgot: %q\n", path, expected, got)
if _, ok := cornerCases[filename]; ok {
s.T().Logf(fmt.Sprintf("\t[corner case] %s", errMsg))
} else {
s.Equal(expected, got, errMsg)
}
return nil
})
s.T().Logf("\ttotal files: %d, ok: %d, failed: %d, other: %d\n", total, ok, failed, other)
}
// Second part of the test_blob.rb#test_language
// https://github.com/github/linguist/blob/59b2d88b2242e6062384e5fb876668cc30ead951/test/test_blob.rb#L275
func (s *linguistCorpusSuite) TestLinguistGenericFixtures() {
if regex.Name != regex.Oniguruma {
s.T().Skip("requires Ruby regexp support")
}
const filenamesDir = "filenames"
var total, failed, ok, multiple int
var expected string
filepath.Walk(filepath.Join(s.testFixturesDir, "Generic"), func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
if f.Name() != filenamesDir {
expected, _ = data.LanguageByAlias(f.Name())
}
return nil
}
filename := filepath.Base(path)
content, _ := ioutil.ReadFile(path)
total++
result := GetLanguagesByContent(filename, content, nil)
var obtained, status string
switch len(result) {
case 0:
obtained = ""
case 1:
obtained = result[0]
default:
obtained = ""
multiple++
}
if expected == obtained {
status = "ok"
ok++
} else {
status = "failed"
failed++
}
s.Equal(expected, obtained, fmt.Sprintf("%s\texpected: %s\tobtained: %s\tstatus: %s\n", filename, expected, obtained, status))
return nil
})
s.T().Logf("\t\ttotal files: %d, ok: %d, failed: %d, multiple: %d\n", total, ok, failed, multiple)
}