tests
This commit is contained in:
parent
180da1c6df
commit
bead3a606f
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
.linguist
|
5
Makefile
Normal file
5
Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
samples:
|
||||
git clone git@github.com:github/linguist.git .linguist\
|
||||
|
||||
test: samples
|
||||
go test -v ./...
|
@ -1,24 +0,0 @@
|
||||
package slinguist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type UtilsSuite struct{}
|
||||
|
||||
var _ = Suite(&UtilsSuite{})
|
||||
|
||||
func (s *UtilsSuite) TestGetLanguage(c *C) {
|
||||
c.Assert(GetLanguage("foo.foo"), Equals, "Other")
|
||||
c.Assert(GetLanguage("foo.go"), Equals, "Go")
|
||||
c.Assert(GetLanguage("foo.go.php"), Equals, "PHP")
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) TestGetLanguageExtensions(c *C) {
|
||||
c.Assert(GetLanguageExtensions("foo"), HasLen, 0)
|
||||
c.Assert(GetLanguageExtensions("C"), Not(HasLen), 0)
|
||||
}
|
13
common_test.go
Normal file
13
common_test.go
Normal file
@ -0,0 +1,13 @@
|
||||
package slinguist
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func Test(t *testing.T) { TestingT(t) }
|
||||
|
||||
type TSuite struct{}
|
||||
|
||||
var _ = Suite(&TSuite{})
|
@ -15,9 +15,9 @@ func GetLanguageByContent(filename string, content []byte) (lang string, safe bo
|
||||
return GetLanguageByExtension(filename)
|
||||
}
|
||||
|
||||
type langMatcher func([]byte) (string, bool)
|
||||
type languageMatcher func([]byte) (string, bool)
|
||||
|
||||
var matchers = map[string]langMatcher{
|
||||
var matchers = map[string]languageMatcher{
|
||||
".cl": clExtLanguage,
|
||||
".cls": clsExtLanguage,
|
||||
".m": mExtLanguage,
|
||||
|
31
content_test.go
Normal file
31
content_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package slinguist
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func (s *TSuite) TestGetLanguageByContent(c *C) {
|
||||
s.testGetLanguageByContent(c, "C")
|
||||
}
|
||||
|
||||
func (s *TSuite) testGetLanguageByContent(c *C, expected string) {
|
||||
files, err := filepath.Glob(path.Join(".linguist/samples", expected, "*"))
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
for _, file := range files {
|
||||
s, _ := os.Stat(file)
|
||||
if s.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
content, _ := ioutil.ReadFile(file)
|
||||
|
||||
obtained, _ := GetLanguageByContent(path.Base(file), content)
|
||||
c.Assert(obtained, Equals, expected, Commentf(file))
|
||||
}
|
||||
}
|
22
extension_test.go
Normal file
22
extension_test.go
Normal file
@ -0,0 +1,22 @@
|
||||
package slinguist
|
||||
|
||||
import . "gopkg.in/check.v1"
|
||||
|
||||
func (s *TSuite) TestGetLanguageByExtension(c *C) {
|
||||
lang, safe := GetLanguageByExtension("foo.foo")
|
||||
c.Assert(lang, Equals, "Other")
|
||||
c.Assert(safe, Equals, false)
|
||||
|
||||
lang, safe = GetLanguageByExtension("foo.go")
|
||||
c.Assert(lang, Equals, "Go")
|
||||
c.Assert(safe, Equals, true)
|
||||
|
||||
lang, safe = GetLanguageByExtension("foo.go.php")
|
||||
c.Assert(lang, Equals, "PHP")
|
||||
c.Assert(safe, Equals, false)
|
||||
}
|
||||
|
||||
func (s *TSuite) TestGetLanguageExtensions(c *C) {
|
||||
c.Assert(GetLanguageExtensions("foo"), HasLen, 0)
|
||||
c.Assert(GetLanguageExtensions("C"), Not(HasLen), 0)
|
||||
}
|
@ -8,6 +8,45 @@ import (
|
||||
"gopkg.in/toqueteos/substring.v1"
|
||||
)
|
||||
|
||||
func IsConfiguration(path string) bool {
|
||||
lang, _ := GetLanguageByExtension(path)
|
||||
_, is := configurationLanguages[lang]
|
||||
|
||||
return is
|
||||
}
|
||||
|
||||
func IsDotFile(path string) bool {
|
||||
return strings.HasPrefix(filepath.Base(path), ".")
|
||||
}
|
||||
|
||||
func IsVendor(path string) bool {
|
||||
return findIndex(path, vendorMatchers) >= 0
|
||||
}
|
||||
|
||||
func IsDocumentation(path string) bool {
|
||||
return findIndex(path, documentationMatchers) >= 0
|
||||
}
|
||||
|
||||
func findIndex(path string, matchers substring.StringsMatcher) int {
|
||||
return matchers.MatchIndex(path)
|
||||
}
|
||||
|
||||
const sniffLen = 8000
|
||||
|
||||
//IsBinary detects if data is a binary value based on:
|
||||
//http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
|
||||
func IsBinary(data []byte) bool {
|
||||
if len(data) > sniffLen {
|
||||
data = data[:sniffLen]
|
||||
}
|
||||
|
||||
if bytes.IndexByte(data, byte(0)) == -1 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// From github/linguist.
|
||||
// curl https://raw.githubusercontent.com/github/linguist/master/lib/linguist/vendor.yml | python -c 'import sys, yaml; l = yaml.load(sys.stdin.read()); print "var skipped = []*regexp.Regexp{\n" + "\n".join(["\tregexp.MustCompile(`" + i + "`)," for i in l]) + "\n}"'
|
||||
|
||||
@ -241,46 +280,6 @@ var documentationMatchers = substring.Or(
|
||||
substring.Regexp(`^[Ss]amples/`),
|
||||
)
|
||||
|
||||
var configurationLanguages = []string{
|
||||
"XML", "JSON", "TOML", "YAML", "INI", "SQL",
|
||||
}
|
||||
|
||||
func VendorIndex(path string) int {
|
||||
return findIndex(path, vendorMatchers)
|
||||
}
|
||||
|
||||
func DocumentationIndex(path string) int {
|
||||
return findIndex(path, documentationMatchers)
|
||||
}
|
||||
|
||||
func findIndex(path string, matchers substring.StringsMatcher) int {
|
||||
return matchers.MatchIndex(path)
|
||||
}
|
||||
|
||||
func IsVendor(path string) bool {
|
||||
return VendorIndex(path) >= 0
|
||||
}
|
||||
|
||||
func IsDotFile(path string) bool {
|
||||
return strings.HasPrefix(filepath.Base(path), ".")
|
||||
}
|
||||
|
||||
func IsDocumentation(path string) bool {
|
||||
return DocumentationIndex(path) >= 0
|
||||
}
|
||||
|
||||
const sniffLen = 8000
|
||||
|
||||
//IsBinary detects if data is a binary value based on:
|
||||
//http://git.kernel.org/cgit/git/git.git/tree/xdiff-interface.c?id=HEAD#n198
|
||||
func IsBinary(data []byte) bool {
|
||||
if len(data) > sniffLen {
|
||||
data = data[:sniffLen]
|
||||
}
|
||||
|
||||
if bytes.IndexByte(data, byte(0)) == -1 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
var configurationLanguages = map[string]bool{
|
||||
"XML": true, "JSON": true, "TOML": true, "YAML": true, "INI": true, "SQL": true,
|
||||
}
|
@ -7,7 +7,7 @@ import (
|
||||
. "gopkg.in/check.v1"
|
||||
)
|
||||
|
||||
func (s *UtilsSuite) TestIsVendor(c *C) {
|
||||
func (s *TSuite) TestIsVendor(c *C) {
|
||||
c.Assert(IsVendor("foo/bar"), Equals, false)
|
||||
c.Assert(IsVendor("foo/vendor/foo"), Equals, true)
|
||||
c.Assert(IsVendor(".travis.yml"), Equals, true)
|
||||
@ -31,18 +31,18 @@ func (s *UtilsSuite) TestIsVendor(c *C) {
|
||||
c.Assert(IsVendor("foo/bar/html5-3.6-respond-1.4.2.js"), Equals, true)
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) TestIsDocumentation(c *C) {
|
||||
func (s *TSuite) TestIsDocumentation(c *C) {
|
||||
c.Assert(IsDocumentation("foo"), Equals, false)
|
||||
c.Assert(IsDocumentation("README"), Equals, true)
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) TestIsConfiguration(c *C) {
|
||||
func (s *TSuite) TestIsConfiguration(c *C) {
|
||||
c.Assert(IsConfiguration("foo"), Equals, false)
|
||||
c.Assert(IsConfiguration("foo.ini"), Equals, true)
|
||||
c.Assert(IsConfiguration("foo.json"), Equals, true)
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) TestIsBinary(c *C) {
|
||||
func (s *TSuite) TestIsBinary(c *C) {
|
||||
c.Assert(IsBinary([]byte("foo")), Equals, false)
|
||||
|
||||
binary := []byte{0}
|
||||
@ -58,13 +58,13 @@ const (
|
||||
jsPath = "some/random/dir/file.js"
|
||||
)
|
||||
|
||||
func (s *UtilsSuite) BenchmarkVendor(c *C) {
|
||||
func (s *TSuite) BenchmarkVendor(c *C) {
|
||||
for i := 0; i < c.N; i++ {
|
||||
_ = IsVendor(htmlPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) BenchmarkVendorJS(c *C) {
|
||||
func (s *TSuite) BenchmarkVendorJS(c *C) {
|
||||
for i := 0; i < c.N; i++ {
|
||||
_ = IsVendor(jsPath)
|
||||
}
|
||||
@ -192,13 +192,13 @@ func isVendorRegexp(s string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) BenchmarkVendorRegexp(c *C) {
|
||||
func (s *TSuite) BenchmarkVendorRegexp(c *C) {
|
||||
for i := 0; i < c.N; i++ {
|
||||
_ = isVendorRegexp(htmlPath)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UtilsSuite) BenchmarkVendorRegexpJS(c *C) {
|
||||
func (s *TSuite) BenchmarkVendorRegexpJS(c *C) {
|
||||
for i := 0; i < c.N; i++ {
|
||||
_ = isVendorRegexp(htmlPath)
|
||||
}
|
Loading…
Reference in New Issue
Block a user