From bead3a606ff47c7326d8d488a56dd0f9c2df7c4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1ximo=20Cuadros?= Date: Wed, 13 Jul 2016 22:21:18 +0200 Subject: [PATCH] tests --- .gitignore | 1 + Makefile | 5 ++ basic_test.go | 24 ---------- common_test.go | 13 ++++++ content.go | 4 +- content_test.go | 31 +++++++++++++ extension_test.go | 22 +++++++++ files.go => utils.go | 83 +++++++++++++++++----------------- files_test.go => utils_test.go | 16 +++---- 9 files changed, 123 insertions(+), 76 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile delete mode 100644 basic_test.go create mode 100644 common_test.go create mode 100644 content_test.go create mode 100644 extension_test.go rename files.go => utils.go (95%) rename files_test.go => utils_test.go (95%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7742825 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.linguist diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..47bea3f --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +samples: + git clone git@github.com:github/linguist.git .linguist\ + +test: samples + go test -v ./... \ No newline at end of file diff --git a/basic_test.go b/basic_test.go deleted file mode 100644 index 14f2624..0000000 --- a/basic_test.go +++ /dev/null @@ -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) -} diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..9bca36a --- /dev/null +++ b/common_test.go @@ -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{}) diff --git a/content.go b/content.go index bb7fdd7..7ad7c05 100644 --- a/content.go +++ b/content.go @@ -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, diff --git a/content_test.go b/content_test.go new file mode 100644 index 0000000..bba1ae7 --- /dev/null +++ b/content_test.go @@ -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)) + } +} diff --git a/extension_test.go b/extension_test.go new file mode 100644 index 0000000..653d460 --- /dev/null +++ b/extension_test.go @@ -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) +} diff --git a/files.go b/utils.go similarity index 95% rename from files.go rename to utils.go index 369a1c8..07a2063 100644 --- a/files.go +++ b/utils.go @@ -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, } diff --git a/files_test.go b/utils_test.go similarity index 95% rename from files_test.go rename to utils_test.go index 1dc4406..c1b9e1d 100644 --- a/files_test.go +++ b/utils_test.go @@ -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) }