| 1 | // Copyright 2019 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 importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 10 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 11 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 14 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 15 | ) |
| 16 | |
| 17 | func TestDirectoryPackageInfoReachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/TestDirectoryPackageInfoReachedStatus().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 18 | tests := []struct { |
| 19 | info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo# |
| 20 | target directoryPackageStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageStatus# |
| 21 | wantStatus bool |
| 22 | wantError bool |
| 23 | }{ |
| 24 | { |
| 25 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 26 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status.: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 27 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err.: nil, |
| 28 | }, |
| 29 | target: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 30 | wantStatus: true, |
| 31 | }, |
| 32 | { |
| 33 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 34 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status.: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 35 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("error getting to directory scanned"), |
| 36 | }, |
| 37 | target: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 38 | wantStatus: true, |
| 39 | wantError: true, |
| 40 | }, |
| 41 | { |
| 42 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{}, |
| 43 | target: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 44 | wantStatus: false, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for _, tt := range tests { |
| 49 | gotStatus, gotErr := tt.info.reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(tt.target) |
| 50 | if gotErr != nil { |
| 51 | if !tt.wantError { |
| 52 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected error: %s", gotErr) |
| 53 | } |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | if tt.wantStatus != gotStatus { |
| 58 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("reached status expected: %v, got: %v", tt.wantStatus, gotStatus) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestModCacheInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/TestModCacheInfo().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) { |
| 64 | m := NewDirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewDirInfoCache().() |
| 65 | |
| 66 | dirInfo := []struct { |
| 67 | dir string |
| 68 | info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo# |
| 69 | }{ |
| 70 | { |
| 71 | dir: "mypackage", |
| 72 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 73 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status.: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 74 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir.: "mypackage", |
| 75 | nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath.: "example.com/mypackage", |
| 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | dir: "bad package", |
| 80 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 81 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status.: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 82 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("bad package"), |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | dir: "mypackage/other", |
| 87 | info: directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 88 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir.: "mypackage/other", |
| 89 | nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath.: "example.com/mypackage/other", |
| 90 | }, |
| 91 | }, |
| 92 | } |
| 93 | |
| 94 | for _, d := range dirInfo { |
| 95 | m.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(d.dir, d.info) |
| 96 | } |
| 97 | |
| 98 | for _, d := range dirInfo { |
| 99 | val, ok := m.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(d.dir) |
| 100 | if !ok { |
| 101 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("directory not loaded: %s", d.dir) |
| 102 | } |
| 103 | |
| 104 | if !reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.DeepEqualscip-go gomod github.com/golang/go/src go1.25.0 reflect/DeepEqual().(d.info, val) { |
| 105 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("expected: %v, got: %v", d.info, val) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | var wantKeys []string |
| 110 | for _, d := range dirInfo { |
| 111 | wantKeys = append(wantKeys, d.dir) |
| 112 | } |
| 113 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(wantKeys) |
| 114 | |
| 115 | gotKeys := m.Keysscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Keys().() |
| 116 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(gotKeys) |
| 117 | |
| 118 | if len(gotKeys) != len(wantKeys) { |
| 119 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("different length of keys. expected: %d, got: %d", len(wantKeys), len(gotKeys)) |
| 120 | } |
| 121 | |
| 122 | for i, want := range wantKeys { |
| 123 | if want != gotKeys[i] { |
| 124 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%d: expected %s, got %s", i, want, gotKeys[i]) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func BenchmarkScanModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/BenchmarkScanModuleCache().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) { |
| 130 | output, err := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("go", "env", "GOMODCACHE").Outputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Output().() |
| 131 | if err != nil { |
| 132 | b.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err) |
| 133 | } |
| 134 | gomodcache := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(string(output)) |
| 135 | cache := NewDirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewDirInfoCache().() |
| 136 | start := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 137 | ScanModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ScanModuleCache().(gomodcache, cache, nil) |
| 138 | b.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("initial scan took %v", timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(start)) |
| 139 | |
| 140 | for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() { |
| 141 | ScanModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ScanModuleCache().(gomodcache, cache, nil) |
| 142 | } |
| 143 | } |