github.com/gopherjs/gopherjs@v1.19.0-beta1.0.20240506212314-27071a8796e4/build/cache/cache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/gopherjs/gopherjs/compiler"
     8  )
     9  
    10  func TestStore(t *testing.T) {
    11  	cacheForTest(t)
    12  
    13  	want := &compiler.Archive{
    14  		ImportPath: "fake/package",
    15  		Imports:    []string{"fake/dep"},
    16  	}
    17  
    18  	bc := BuildCache{}
    19  	if got := bc.LoadArchive(want.ImportPath); got != nil {
    20  		t.Errorf("Got: %s was found in the cache. Want: empty cache.", got.ImportPath)
    21  	}
    22  	bc.StoreArchive(want)
    23  	got := bc.LoadArchive(want.ImportPath)
    24  	if got == nil {
    25  		t.Errorf("Got: %s wan not found in the cache. Want: archive is can be loaded after store.", want.ImportPath)
    26  	}
    27  	if diff := cmp.Diff(want, got); diff != "" {
    28  		t.Errorf("Loaded archive is different from stored (-want,+got):\n%s", diff)
    29  	}
    30  
    31  	// Make sure the package names are a part of the cache key.
    32  	if got := bc.LoadArchive("fake/other"); got != nil {
    33  		t.Errorf("Got: fake/other was found in cache: %#v. Want: nil for packages that weren't cached.", got)
    34  	}
    35  }
    36  
    37  func TestInvalidation(t *testing.T) {
    38  	cacheForTest(t)
    39  
    40  	tests := []struct {
    41  		cache1 BuildCache
    42  		cache2 BuildCache
    43  	}{
    44  		{
    45  			cache1: BuildCache{Minify: true},
    46  			cache2: BuildCache{Minify: false},
    47  		}, {
    48  			cache1: BuildCache{GOOS: "dos"},
    49  			cache2: BuildCache{GOOS: "amiga"},
    50  		}, {
    51  			cache1: BuildCache{GOARCH: "m68k"},
    52  			cache2: BuildCache{GOARCH: "mos6502"},
    53  		}, {
    54  			cache1: BuildCache{GOROOT: "here"},
    55  			cache2: BuildCache{GOROOT: "there"},
    56  		}, {
    57  			cache1: BuildCache{GOPATH: "home"},
    58  			cache2: BuildCache{GOPATH: "away"},
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		a := &compiler.Archive{ImportPath: "package/fake"}
    64  		test.cache1.StoreArchive(a)
    65  
    66  		if got := test.cache2.LoadArchive(a.ImportPath); got != nil {
    67  			t.Logf("-cache1,+cache2:\n%s", cmp.Diff(test.cache1, test.cache2))
    68  			t.Errorf("Got: %v loaded from cache. Want: build parameter change invalidates cache.", got)
    69  		}
    70  	}
    71  }
    72  
    73  func cacheForTest(t *testing.T) {
    74  	t.Helper()
    75  	originalRoot := cacheRoot
    76  	t.Cleanup(func() { cacheRoot = originalRoot })
    77  	cacheRoot = t.TempDir()
    78  }