mirror of
https://github.com/golang/go
synced 2024-11-18 14:04:45 -07:00
1e586a538e
Now that we're storing module information per-directory, we don't need to pre-compute the need for a replace statement. And we never have the package name in the place it used to be set. Change-Id: I3b0845dc49f52f8c449840410dbb786fe903d29d Reviewed-on: https://go-review.googlesource.com/c/tools/+/212861 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
123 lines
2.4 KiB
Go
123 lines
2.4 KiB
Go
package imports
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
)
|
|
|
|
func TestDirectoryPackageInfoReachedStatus(t *testing.T) {
|
|
tests := []struct {
|
|
info directoryPackageInfo
|
|
target directoryPackageStatus
|
|
wantStatus bool
|
|
wantError bool
|
|
}{
|
|
{
|
|
info: directoryPackageInfo{
|
|
status: directoryScanned,
|
|
err: nil,
|
|
},
|
|
target: directoryScanned,
|
|
wantStatus: true,
|
|
},
|
|
{
|
|
info: directoryPackageInfo{
|
|
status: directoryScanned,
|
|
err: fmt.Errorf("error getting to directory scanned"),
|
|
},
|
|
target: directoryScanned,
|
|
wantStatus: true,
|
|
wantError: true,
|
|
},
|
|
{
|
|
info: directoryPackageInfo{},
|
|
target: directoryScanned,
|
|
wantStatus: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
gotStatus, gotErr := tt.info.reachedStatus(tt.target)
|
|
if gotErr != nil {
|
|
if !tt.wantError {
|
|
t.Errorf("unexpected error: %s", gotErr)
|
|
}
|
|
continue
|
|
}
|
|
|
|
if tt.wantStatus != gotStatus {
|
|
t.Errorf("reached status expected: %v, got: %v", tt.wantStatus, gotStatus)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestModCacheInfo(t *testing.T) {
|
|
m := &dirInfoCache{
|
|
dirs: make(map[string]*directoryPackageInfo),
|
|
}
|
|
|
|
dirInfo := []struct {
|
|
dir string
|
|
info directoryPackageInfo
|
|
}{
|
|
{
|
|
dir: "mypackage",
|
|
info: directoryPackageInfo{
|
|
status: directoryScanned,
|
|
dir: "mypackage",
|
|
nonCanonicalImportPath: "example.com/mypackage",
|
|
},
|
|
},
|
|
{
|
|
dir: "bad package",
|
|
info: directoryPackageInfo{
|
|
status: directoryScanned,
|
|
err: fmt.Errorf("bad package"),
|
|
},
|
|
},
|
|
{
|
|
dir: "mypackage/other",
|
|
info: directoryPackageInfo{
|
|
dir: "mypackage/other",
|
|
nonCanonicalImportPath: "example.com/mypackage/other",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, d := range dirInfo {
|
|
m.Store(d.dir, d.info)
|
|
}
|
|
|
|
for _, d := range dirInfo {
|
|
val, ok := m.Load(d.dir)
|
|
if !ok {
|
|
t.Errorf("directory not loaded: %s", d.dir)
|
|
}
|
|
|
|
if !reflect.DeepEqual(d.info, val) {
|
|
t.Errorf("expected: %v, got: %v", d.info, val)
|
|
}
|
|
}
|
|
|
|
var wantKeys []string
|
|
for _, d := range dirInfo {
|
|
wantKeys = append(wantKeys, d.dir)
|
|
}
|
|
sort.Strings(wantKeys)
|
|
|
|
gotKeys := m.Keys()
|
|
sort.Strings(gotKeys)
|
|
|
|
if len(gotKeys) != len(wantKeys) {
|
|
t.Errorf("different length of keys. expected: %d, got: %d", len(wantKeys), len(gotKeys))
|
|
}
|
|
|
|
for i, want := range wantKeys {
|
|
if want != gotKeys[i] {
|
|
t.Errorf("%d: expected %s, got %s", i, want, gotKeys[i])
|
|
}
|
|
}
|
|
}
|