| 1 | // Copyright 2025 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package packagepath_test |
| 6 | |
| 7 | import ( |
| 8 | "testing" |
| 9 | |
| 10 | . "golang.org/x/tools/internal/packagepath" |
| 11 | ) |
| 12 | |
| 13 | func TestCanImport(t *testing.T) { |
| 14 | for _, tt := range []struct { |
| 15 | from string |
| 16 | to string |
| 17 | want bool |
| 18 | }{ |
| 19 | {"fmt", "internal", true}, |
| 20 | {"fmt", "internal/foo", true}, |
| 21 | {"fmt", "fmt/internal/foo", true}, |
| 22 | {"fmt", "cmd/internal/archive", false}, |
| 23 | {"a.com/b", "internal", false}, |
| 24 | {"a.com/b", "xinternal", true}, |
| 25 | {"a.com/b", "internal/foo", false}, |
| 26 | {"a.com/b", "xinternal/foo", true}, |
| 27 | {"a.com/b", "a.com/internal", true}, |
| 28 | {"a.com/b", "a.com/b/internal", true}, |
| 29 | {"a.com/b", "a.com/b/internal/foo", true}, |
| 30 | {"a.com/b", "a.com/c/internal", false}, |
| 31 | {"a.com/b", "a.com/c/xinternal", true}, |
| 32 | {"a.com/b", "a.com/c/internal/foo", false}, |
| 33 | {"a.com/b", "a.com/c/xinternal/foo", true}, |
| 34 | } { |
| 35 | got := CanImport(tt.from, tt.to) |
| 36 | if got != tt.want { |
| 37 | t.Errorf("CanImport(%q, %q) = %v, want %v", tt.from, tt.to, got, tt.want) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestIsStdPackage(t *testing.T) { |
| 43 | testCases := []struct { |
| 44 | pkgpath string |
| 45 | isStd bool |
| 46 | }{ |
| 47 | {pkgpath: "os", isStd: true}, |
| 48 | {pkgpath: "net/http", isStd: true}, |
| 49 | {pkgpath: "vendor/golang.org/x/net/dns/dnsmessage", isStd: true}, |
| 50 | {pkgpath: "golang.org/x/net/dns/dnsmessage", isStd: false}, |
| 51 | {pkgpath: "testdata", isStd: false}, |
| 52 | } |
| 53 | |
| 54 | for _, tc := range testCases { |
| 55 | t.Run(tc.pkgpath, func(t *testing.T) { |
| 56 | got := IsStdPackage(tc.pkgpath) |
| 57 | if got != tc.isStd { |
| 58 | t.Fatalf("got %t want %t", got, tc.isStd) |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |