From a7cfa65953a3ab1f26a2d0b046acae02d4ebebb1 Mon Sep 17 00:00:00 2001 From: Alfredo Beaumont Date: Thu, 7 Dec 2017 16:42:02 +0100 Subject: [PATCH 1/2] tests: Add testcases for empty filenames Signed-off-by: Alfredo Beaumont --- common_test.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/common_test.go b/common_test.go index 74920e7..b2a5f15 100644 --- a/common_test.go +++ b/common_test.go @@ -78,6 +78,7 @@ func (s *EnryTestSuite) TestGetLanguage() { {name: "TestGetLanguage_2", filename: "foo.m", content: []byte(":- module"), expected: "Mercury"}, {name: "TestGetLanguage_3", filename: "foo.m", content: nil, expected: OtherLanguage}, {name: "TestGetLanguage_4", filename: "foo.mo", content: []byte{0xDE, 0x12, 0x04, 0x95, 0x00, 0x00, 0x00, 0x00}, expected: OtherLanguage}, + {name: "TestGetLanguage_5", filename: "", content: nil, expected: OtherLanguage}, } for _, test := range tests { @@ -129,11 +130,17 @@ func (s *EnryTestSuite) TestGetLanguagesByModelineLinguist() { {name: "TestGetLanguagesByModelineLinguist_29", filename: filepath.Join(modelinesDir, "ruby11"), expected: []string{"Ruby"}}, {name: "TestGetLanguagesByModelineLinguist_30", filename: filepath.Join(modelinesDir, "ruby12"), expected: []string{"Ruby"}}, {name: "TestGetLanguagesByModelineLinguist_31", filename: filepath.Join(s.samplesDir, "C/main.c"), expected: nil}, + {name: "TestGetLanguagesByModelineLinguist_32", filename: "", expected: nil}, } for _, test := range tests { - content, err := ioutil.ReadFile(test.filename) - assert.NoError(s.T(), err) + var content []byte + var err error + + if test.filename != "" { + content, err = ioutil.ReadFile(test.filename) + assert.NoError(s.T(), err) + } languages := GetLanguagesByModeline(test.filename, content, test.candidates) assert.Equal(s.T(), test.expected, languages, fmt.Sprintf("%v: languages = %v, expected: %v", test.name, languages, test.expected)) @@ -183,6 +190,7 @@ func (s *EnryTestSuite) TestGetLanguagesByFilename() { {name: "TestGetLanguagesByFilename_6", filename: "Vagrantfile", expected: []string{"Ruby"}}, {name: "TestGetLanguagesByFilename_7", filename: "_vimrc", expected: []string{"Vim script"}}, {name: "TestGetLanguagesByFilename_8", filename: "pom.xml", expected: []string{"Maven POM"}}, + {name: "TestGetLanguagesByFilename_9", filename: "", expected: nil}, } for _, test := range tests { @@ -244,6 +252,7 @@ func (s *EnryTestSuite) TestGetLanguagesByExtension() { {name: "TestGetLanguagesByExtension_1", filename: "foo.foo", expected: nil}, {name: "TestGetLanguagesByExtension_2", filename: "foo.go", expected: []string{"Go"}}, {name: "TestGetLanguagesByExtension_3", filename: "foo.go.php", expected: []string{"Hack", "PHP"}}, + {name: "TestGetLanguagesByExtension_4", filename: "", expected: nil}, } for _, test := range tests { @@ -265,11 +274,17 @@ func (s *EnryTestSuite) TestGetLanguagesByClassifier() { {name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, expected: "C++"}, {name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"ruby"}, expected: "Ruby"}, {name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(s.samplesDir, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, expected: "Python"}, + {name: "TestGetLanguagesByClassifier_7", filename: "", candidates: []string{"python"}, expected: OtherLanguage}, } for _, test := range test { - content, err := ioutil.ReadFile(test.filename) - assert.NoError(s.T(), err) + var content []byte + var err error + + if test.filename != "" { + content, err = ioutil.ReadFile(test.filename) + assert.NoError(s.T(), err) + } languages := GetLanguagesByClassifier(test.filename, content, test.candidates) var language string @@ -297,7 +312,7 @@ func (s *EnryTestSuite) TestGetLanguagesBySpecificClassifier() { {name: "TestGetLanguagesByClassifier_4", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"python", "ruby", "c++"}, classifier: DefaultClassifier, expected: "C++"}, {name: "TestGetLanguagesByClassifier_5", filename: filepath.Join(s.samplesDir, "C/blob.c"), candidates: []string{"ruby"}, classifier: DefaultClassifier, expected: "Ruby"}, {name: "TestGetLanguagesByClassifier_6", filename: filepath.Join(s.samplesDir, "Python/django-models-base.py"), candidates: []string{"python", "ruby", "c", "c++"}, classifier: DefaultClassifier, expected: "Python"}, - {name: "TestGetLanguagesByClassifier_6", filename: os.DevNull, candidates: nil, classifier: DefaultClassifier, expected: OtherLanguage}, + {name: "TestGetLanguagesByClassifier_7", filename: os.DevNull, candidates: nil, classifier: DefaultClassifier, expected: OtherLanguage}, } for _, test := range test { From c590beb039b0dadcb9ca7da72170fbc1f6a616a0 Mon Sep 17 00:00:00 2001 From: Alfredo Beaumont Date: Thu, 7 Dec 2017 16:45:19 +0100 Subject: [PATCH 2/2] common: Return nil on empty filenames Signed-off-by: Alfredo Beaumont --- common.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/common.go b/common.go index 3ccd34b..9db5a69 100644 --- a/common.go +++ b/common.go @@ -266,6 +266,10 @@ func GetLanguagesByVimModeline(_ string, content []byte, _ []string) []string { // GetLanguagesByFilename returns a slice of possible languages for the given filename. // It complies with the signature to be a Strategy type. func GetLanguagesByFilename(filename string, _ []byte, _ []string) []string { + if filename == "" { + return nil + } + return data.LanguagesByFilename[filepath.Base(filename)] } @@ -385,6 +389,10 @@ func getDotIndexes(filename string) []int { // GetLanguagesByContent returns a slice of possible languages for the given content. // It complies with the signature to be a Strategy type. func GetLanguagesByContent(filename string, content []byte, _ []string) []string { + if filename == "" { + return nil + } + ext := strings.ToLower(filepath.Ext(filename)) fnMatcher, ok := data.ContentMatchers[ext] if !ok {