github.com/goplus/gogen@v1.16.0/packages/cache/cache_test.go (about)

     1  /*
     2  Copyright 2022 The GoPlus Authors (goplus.org)
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  	http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package cache
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/goplus/gogen/packages"
    23  )
    24  
    25  func dirtyPkgHash(pkgPath string, self bool) string {
    26  	return HashInvalid
    27  }
    28  
    29  func nodirtyPkgHash(pkgPath string, self bool) string {
    30  	return "1"
    31  }
    32  
    33  func TestBasic(t *testing.T) {
    34  	readFile = func(string) ([]byte, error) {
    35  		return nil, os.ErrNotExist
    36  	}
    37  	c := New(nodirtyPkgHash)
    38  	err := c.Load("/not-found/gopkg.cache")
    39  	if err != nil {
    40  		t.Fatal("Load failed:", err)
    41  	}
    42  
    43  	p := packages.NewImporter(nil)
    44  	p.SetCache(c)
    45  	pkg, err := p.Import("fmt")
    46  	if err != nil || pkg.Path() != "fmt" {
    47  		t.Fatal("Import failed:", pkg, err)
    48  	}
    49  	if v := c.ListTimes(); v != 1 {
    50  		t.Fatal("ListTimes:", v)
    51  	}
    52  	if _, err = p.Import("not-found"); err == nil {
    53  		t.Fatal("Import not-found: no error?")
    54  	}
    55  	if pkg2, err := p.Import("fmt"); err != nil || pkg2 != pkg {
    56  		t.Fatal("Import reuse fail:", pkg, pkg2)
    57  	}
    58  	if v := c.ListTimes(); v != 2 {
    59  		t.Fatal("ListTimes:", v)
    60  	}
    61  	pkg, err = p.Import("github.com/goplus/gogen/internal/foo")
    62  	if err != nil {
    63  		t.Fatal("Import failed:", pkg, err)
    64  	}
    65  	if v := c.ListTimes(); v != 3 {
    66  		t.Fatal("ListTimes:", v)
    67  	}
    68  
    69  	var result []byte
    70  	writeFile = func(_ string, data []byte, _ os.FileMode) error {
    71  		result = data
    72  		return nil
    73  	}
    74  	readFile = func(string) ([]byte, error) {
    75  		return result, nil
    76  	}
    77  	c.Save("/not-found/gopkg.cache")
    78  
    79  	c2 := New(nodirtyPkgHash)
    80  	if err = c2.Load("/not-found/gopkg.cache"); err != nil {
    81  		t.Fatal("Load failed:", err)
    82  	}
    83  	p2 := packages.NewImporter(nil)
    84  	p2.SetCache(c2)
    85  	pkg, err = p2.Import("fmt")
    86  	if err != nil || pkg.Path() != "fmt" {
    87  		t.Fatal("Import failed:", pkg, err)
    88  	}
    89  	if v := c2.ListTimes(); v != 0 {
    90  		t.Fatal("ListTimes:", v)
    91  	}
    92  
    93  	c3 := New(dirtyPkgHash)
    94  	if err = c3.Load("/not-found/gopkg.cache"); err != nil {
    95  		t.Fatal("Load failed:", err)
    96  	}
    97  	p3 := packages.NewImporter(nil)
    98  	p3.SetCache(c3)
    99  	pkg, err = p3.Import("fmt")
   100  	if err != nil || pkg.Path() != "fmt" {
   101  		t.Fatal("Import failed:", pkg, err)
   102  	}
   103  	if v := c3.ListTimes(); v != 1 {
   104  		t.Fatal("ListTimes:", v)
   105  	}
   106  }
   107  
   108  func TestIsDirty(t *testing.T) {
   109  	c := &pkgCache{"1.a", "a", []depPkg{
   110  		{"b", "b"},
   111  	}}
   112  	if !isDirty(nil, "a", c, func(pkgPath string, self bool) (hash string) {
   113  		if self {
   114  			return pkgPath
   115  		}
   116  		return ""
   117  	}) {
   118  		t.Fatal("isDirty: is not dirty")
   119  	}
   120  }
   121  
   122  func TestParseExport(t *testing.T) {
   123  	if _, e := parseExports("abc"); e != errInvalidFormat {
   124  		t.Fatal("parseExports failed:", e)
   125  	}
   126  }
   127  
   128  func TestErrLoadCache(t *testing.T) {
   129  	var c Impl
   130  	if err := c.loadCachePkgs([]string{"abc"}); err != errInvalidFormat {
   131  		t.Fatal("loadCachePkgs failed:", err)
   132  	}
   133  	if err := c.loadCachePkgs([]string{"abc\tefg\thash\t3"}); err != errInvalidFormat {
   134  		t.Fatal("loadCachePkgs failed:", err)
   135  	}
   136  	if err := c.loadCachePkgs([]string{"abc\tefg\thash\t1", "x"}); err != errInvalidFormat {
   137  		t.Fatal("loadCachePkgs failed:", err)
   138  	}
   139  	if err := c.loadCachePkgs([]string{"abc\tefg\thash\t1", "\tx"}); err != errInvalidFormat {
   140  		t.Fatal("loadCachePkgs failed:", err)
   141  	}
   142  }