From 355b026adf25658be4978470eb851cb27eb30a3f Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Mon, 11 Nov 2019 16:22:26 -0500 Subject: internal/postgres: ignore self-imports in imported-by counts Do not count package A as importing package B if they are in the same module. Change-Id: Ic0f98af8d72b8fff29e105779c6eb619214335e9 Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/596525 CI-Result: Cloud Build Reviewed-by: Julie Qiu --- internal/stdlib/stdlib.go | 8 ++++++++ internal/stdlib/stdlib_test.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) (limited to 'internal/stdlib') diff --git a/internal/stdlib/stdlib.go b/internal/stdlib/stdlib.go index 8a081cfc..adf1cf49 100644 --- a/internal/stdlib/stdlib.go +++ b/internal/stdlib/stdlib.go @@ -357,6 +357,14 @@ func subTree(r *git.Repository, t *object.Tree, name string) (*object.Tree, erro return nil, os.ErrNotExist } +// Contains reports whether the given import path is part of the Go standard library. +func Contains(path string) bool { + if i := strings.IndexByte(path, '/'); i != -1 { + path = path[:i] + } + return !strings.Contains(path, ".") +} + // References used for Versions during testing. var testRefs = []plumbing.ReferenceName{ "refs/changes/56/93156/13", diff --git a/internal/stdlib/stdlib_test.go b/internal/stdlib/stdlib_test.go index eca3760d..a0116100 100644 --- a/internal/stdlib/stdlib_test.go +++ b/internal/stdlib/stdlib_test.go @@ -189,3 +189,21 @@ func TestVersionForTag(t *testing.T) { } } } + +func TestContains(t *testing.T) { + for _, test := range []struct { + in string + want bool + }{ + {"fmt", true}, + {"encoding/json", true}, + {"something/with.dots", true}, + {"example.com", false}, + {"example.com/fmt", false}, + } { + got := Contains(test.in) + if got != test.want { + t.Errorf("Contains(%q) = %t, want %t", test.in, got, test.want) + } + } +} -- cgit v1.3-6-g1900