| 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 imports |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "os/exec" |
| 10 | "reflect" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | func TestDirectoryPackageInfoReachedStatus(t *testing.T) { |
| 18 | tests := []struct { |
| 19 | info directoryPackageInfo |
| 20 | target directoryPackageStatus |
| 21 | wantStatus bool |
| 22 | wantError bool |
| 23 | }{ |
| 24 | { |
| 25 | info: directoryPackageInfo{ |
| 26 | status: directoryScanned, |
| 27 | err: nil, |
| 28 | }, |
| 29 | target: directoryScanned, |
| 30 | wantStatus: true, |
| 31 | }, |
| 32 | { |
| 33 | info: directoryPackageInfo{ |
| 34 | status: directoryScanned, |
| 35 | err: fmt.Errorf("error getting to directory scanned"), |
| 36 | }, |
| 37 | target: directoryScanned, |
| 38 | wantStatus: true, |
| 39 | wantError: true, |
| 40 | }, |
| 41 | { |
| 42 | info: directoryPackageInfo{}, |
| 43 | target: directoryScanned, |
| 44 | wantStatus: false, |
| 45 | }, |
| 46 | } |
| 47 | |
| 48 | for _, tt := range tests { |
| 49 | gotStatus, gotErr := tt.info.reachedStatus(tt.target) |
| 50 | if gotErr != nil { |
| 51 | if !tt.wantError { |
| 52 | t.Errorf("unexpected error: %s", gotErr) |
| 53 | } |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | if tt.wantStatus != gotStatus { |
| 58 | t.Errorf("reached status expected: %v, got: %v", tt.wantStatus, gotStatus) |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestModCacheInfo(t *testing.T) { |
| 64 | m := NewDirInfoCache() |
| 65 | |
| 66 | dirInfo := []struct { |
| 67 | dir string |
| 68 | info directoryPackageInfo |
| 69 | }{ |
| 70 | { |
| 71 | dir: "mypackage", |
| 72 | info: directoryPackageInfo{ |
| 73 | status: directoryScanned, |
| 74 | dir: "mypackage", |
| 75 | nonCanonicalImportPath: "example.com/mypackage", |
| 76 | }, |
| 77 | }, |
| 78 | { |
| 79 | dir: "bad package", |
| 80 | info: directoryPackageInfo{ |
| 81 | status: directoryScanned, |
| 82 | err: fmt.Errorf("bad package"), |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | dir: "mypackage/other", |
| 87 | info: directoryPackageInfo{ |
| 88 | dir: "mypackage/other", |
| 89 | nonCanonicalImportPath: "example.com/mypackage/other", |
| 90 | }, |
| 91 | }, |
| 92 | } |
| 93 | |
| 94 | for _, d := range dirInfo { |
| 95 | m.Store(d.dir, d.info) |
| 96 | } |
| 97 | |
| 98 | for _, d := range dirInfo { |
| 99 | val, ok := m.Load(d.dir) |
| 100 | if !ok { |
| 101 | t.Errorf("directory not loaded: %s", d.dir) |
| 102 | } |
| 103 | |
| 104 | if !reflect.DeepEqual(d.info, val) { |
| 105 | t.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 | sort.Strings(wantKeys) |
| 114 | |
| 115 | gotKeys := m.Keys() |
| 116 | sort.Strings(gotKeys) |
| 117 | |
| 118 | if len(gotKeys) != len(wantKeys) { |
| 119 | t.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.Errorf("%d: expected %s, got %s", i, want, gotKeys[i]) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func BenchmarkScanModuleCache(b *testing.B) { |
| 130 | output, err := exec.Command("go", "env", "GOMODCACHE").Output() |
| 131 | if err != nil { |
| 132 | b.Fatal(err) |
| 133 | } |
| 134 | gomodcache := strings.TrimSpace(string(output)) |
| 135 | cache := NewDirInfoCache() |
| 136 | start := time.Now() |
| 137 | ScanModuleCache(gomodcache, cache, nil) |
| 138 | b.Logf("initial scan took %v", time.Since(start)) |
| 139 | |
| 140 | for b.Loop() { |
| 141 | ScanModuleCache(gomodcache, cache, nil) |
| 142 | } |
| 143 | } |