From 869c320804fba7f3a568e00242a665cf8b307d36 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 3 Apr 2024 06:00:25 +0200 Subject: [PATCH] fix: local module path matching (#202) Signed-off-by: Fernandez Ludovic --- pkg/section/local_module.go | 2 +- pkg/section/local_module_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 pkg/section/local_module_test.go diff --git a/pkg/section/local_module.go b/pkg/section/local_module.go index 5af74b3..50f41e5 100644 --- a/pkg/section/local_module.go +++ b/pkg/section/local_module.go @@ -18,7 +18,7 @@ type LocalModule struct { } func (m *LocalModule) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity { - if strings.HasPrefix(spec.Path, m.Path) { + if spec.Path == m.Path || strings.HasPrefix(spec.Path, m.Path+"/") { return specificity.LocalModule{} } diff --git a/pkg/section/local_module_test.go b/pkg/section/local_module_test.go new file mode 100644 index 0000000..93702bf --- /dev/null +++ b/pkg/section/local_module_test.go @@ -0,0 +1,18 @@ +package section + +import ( + "testing" + + "github.com/daixiang0/gci/pkg/specificity" +) + +func TestLocalModule_specificity(t *testing.T) { + testCases := []specificityTestData{ + {"example.com/hello", &LocalModule{Path: "example.com/hello"}, specificity.LocalModule{}}, + {"example.com/hello/world", &LocalModule{Path: "example.com/hello"}, specificity.LocalModule{}}, + {"example.com/hello-world", &LocalModule{Path: "example.com/hello"}, specificity.MisMatch{}}, + {"example.com/helloworld", &LocalModule{Path: "example.com/hello"}, specificity.MisMatch{}}, + {"example.com", &LocalModule{Path: "example.com/hello"}, specificity.MisMatch{}}, + } + testSpecificity(t, testCases) +}