github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/imports/mod_cache_test.go (about)

     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  	"reflect"
    10  	"sort"
    11  	"testing"
    12  )
    13  
    14  func TestDirectoryPackageInfoReachedStatus(t *testing.T) {
    15  	tests := []struct {
    16  		info       directoryPackageInfo
    17  		target     directoryPackageStatus
    18  		wantStatus bool
    19  		wantError  bool
    20  	}{
    21  		{
    22  			info: directoryPackageInfo{
    23  				status: directoryScanned,
    24  				err:    nil,
    25  			},
    26  			target:     directoryScanned,
    27  			wantStatus: true,
    28  		},
    29  		{
    30  			info: directoryPackageInfo{
    31  				status: directoryScanned,
    32  				err:    fmt.Errorf("error getting to directory scanned"),
    33  			},
    34  			target:     directoryScanned,
    35  			wantStatus: true,
    36  			wantError:  true,
    37  		},
    38  		{
    39  			info:       directoryPackageInfo{},
    40  			target:     directoryScanned,
    41  			wantStatus: false,
    42  		},
    43  	}
    44  
    45  	for _, tt := range tests {
    46  		gotStatus, gotErr := tt.info.reachedStatus(tt.target)
    47  		if gotErr != nil {
    48  			if !tt.wantError {
    49  				t.Errorf("unexpected error: %s", gotErr)
    50  			}
    51  			continue
    52  		}
    53  
    54  		if tt.wantStatus != gotStatus {
    55  			t.Errorf("reached status expected: %v, got: %v", tt.wantStatus, gotStatus)
    56  		}
    57  	}
    58  }
    59  
    60  func TestModCacheInfo(t *testing.T) {
    61  	m := &dirInfoCache{
    62  		dirs: make(map[string]*directoryPackageInfo),
    63  	}
    64  
    65  	dirInfo := []struct {
    66  		dir  string
    67  		info directoryPackageInfo
    68  	}{
    69  		{
    70  			dir: "mypackage",
    71  			info: directoryPackageInfo{
    72  				status:                 directoryScanned,
    73  				dir:                    "mypackage",
    74  				nonCanonicalImportPath: "example.com/mypackage",
    75  			},
    76  		},
    77  		{
    78  			dir: "bad package",
    79  			info: directoryPackageInfo{
    80  				status: directoryScanned,
    81  				err:    fmt.Errorf("bad package"),
    82  			},
    83  		},
    84  		{
    85  			dir: "mypackage/other",
    86  			info: directoryPackageInfo{
    87  				dir:                    "mypackage/other",
    88  				nonCanonicalImportPath: "example.com/mypackage/other",
    89  			},
    90  		},
    91  	}
    92  
    93  	for _, d := range dirInfo {
    94  		m.Store(d.dir, d.info)
    95  	}
    96  
    97  	for _, d := range dirInfo {
    98  		val, ok := m.Load(d.dir)
    99  		if !ok {
   100  			t.Errorf("directory not loaded: %s", d.dir)
   101  		}
   102  
   103  		if !reflect.DeepEqual(d.info, val) {
   104  			t.Errorf("expected: %v, got: %v", d.info, val)
   105  		}
   106  	}
   107  
   108  	var wantKeys []string
   109  	for _, d := range dirInfo {
   110  		wantKeys = append(wantKeys, d.dir)
   111  	}
   112  	sort.Strings(wantKeys)
   113  
   114  	gotKeys := m.Keys()
   115  	sort.Strings(gotKeys)
   116  
   117  	if len(gotKeys) != len(wantKeys) {
   118  		t.Errorf("different length of keys. expected: %d, got: %d", len(wantKeys), len(gotKeys))
   119  	}
   120  
   121  	for i, want := range wantKeys {
   122  		if want != gotKeys[i] {
   123  			t.Errorf("%d: expected %s, got %s", i, want, gotKeys[i])
   124  		}
   125  	}
   126  }