2019-07-19 13:21:23 -06:00
|
|
|
package imports
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-04 12:02:48 -07:00
|
|
|
"reflect"
|
2019-07-19 10:41:29 -06:00
|
|
|
"sort"
|
2019-07-19 13:21:23 -06:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 10:41:29 -06:00
|
|
|
|
|
|
|
func TestModCacheInfo(t *testing.T) {
|
2019-10-09 17:05:30 -06:00
|
|
|
m := &dirInfoCache{
|
|
|
|
dirs: make(map[string]*directoryPackageInfo),
|
2019-07-19 10:41:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
dirInfo := []struct {
|
|
|
|
dir string
|
|
|
|
info directoryPackageInfo
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
dir: "mypackage",
|
|
|
|
info: directoryPackageInfo{
|
|
|
|
status: directoryScanned,
|
|
|
|
dir: "mypackage",
|
|
|
|
nonCanonicalImportPath: "example.com/mypackage",
|
|
|
|
needsReplace: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
needsReplace: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:02:48 -07:00
|
|
|
if !reflect.DeepEqual(d.info, val) {
|
2019-07-19 10:41:29 -06:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|