From 887bc6a4be6f5dba0ad7cdb112cce5b5ec906efd Mon Sep 17 00:00:00 2001 From: Huitse Tai Date: Wed, 18 Oct 2017 12:18:52 +0800 Subject: [PATCH 1/2] make IsDotFile do not treat '.' as true Signed-off-by: Huitse Tai --- utils.go | 3 ++- utils_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/utils.go b/utils.go index 0944e4f..4eb4a69 100644 --- a/utils.go +++ b/utils.go @@ -40,7 +40,8 @@ func IsConfiguration(path string) bool { // IsDotFile returns whether or not path has dot as a prefix. func IsDotFile(path string) bool { - return strings.HasPrefix(filepath.Base(path), ".") + base := filepath.Base(path) + return strings.HasPrefix(base, ".") && len(base) > len(".") } // IsVendor returns whether or not path is a vendor path. diff --git a/utils_test.go b/utils_test.go index 267d617..6473f1c 100644 --- a/utils_test.go +++ b/utils_test.go @@ -82,6 +82,22 @@ func (s *EnryTestSuite) TestIsBinary() { } } +func (s *EnryTestSuite) TestIsDotFile() { + tests := []struct { + name string + path string + expected bool + }{ + {name: "TestIsDotFile_1", path: "foo/bar/./", expected: false}, + {name: "TestIsDotFile_2", path: "./", expected: false}, + } + + for _, test := range tests { + is := IsDotFile(test.path) + assert.Equal(s.T(), test.expected, is, fmt.Sprintf("%v: is = %v, expected: %v", test.name, is, test.expected)) + } +} + func TestFileCountListSort(t *testing.T) { sampleData := FileCountList{{"a", 8}, {"b", 65}, {"c", 20}, {"d", 90}} const ascending = "ASC" From a786f6175e494ed884ceaef92be44480d07685ff Mon Sep 17 00:00:00 2001 From: Huitse Tai Date: Wed, 18 Oct 2017 12:50:33 +0800 Subject: [PATCH 2/2] patch IsDotFile function compatible with both '.' and '..' Signed-off-by: Huitse Tai --- utils.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils.go b/utils.go index 4eb4a69..57d378d 100644 --- a/utils.go +++ b/utils.go @@ -40,8 +40,9 @@ func IsConfiguration(path string) bool { // IsDotFile returns whether or not path has dot as a prefix. func IsDotFile(path string) bool { + path = filepath.Clean(path) base := filepath.Base(path) - return strings.HasPrefix(base, ".") && len(base) > len(".") + return strings.HasPrefix(base, ".") && base != "." && base != ".." } // IsVendor returns whether or not path is a vendor path.