Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gogen/packages: DiskCache => Cache; test gogen/packages/cache #413

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type exportPkg struct {
func golistExport(dir string, pkgPath []string) (ret []exportPkg, err error) {
var stdout, stderr bytes.Buffer
var args = make([]string, 0, 3+len(pkgPath))
args = append(args, "list", "-f='{{.ImportPath}}\t{{.Export}}\t{{.Deps}}'", "-export")
args = append(args, "list", "-f={{.ImportPath}}\t{{.Export}}\t{{.Deps}}", "-export")
args = append(args, pkgPath...)
cmd := exec.Command("go", args...)
cmd.Stdout = &stdout
Expand Down Expand Up @@ -144,7 +144,8 @@ var (
func parseExport(line string) (ret exportPkg, err error) {
parts := strings.SplitN(line, "\t", 3)
if len(parts) != 3 {
return ret, errInvalidFormat
err = errInvalidFormat
return
}
deps := parts[2]
if len(deps) > 2 {
Expand All @@ -156,6 +157,11 @@ func parseExport(line string) (ret exportPkg, err error) {

// ----------------------------------------------------------------------------

var (
readFile = os.ReadFile
writeFile = os.WriteFile
)

/*
DiskCache cacheFile format:
<pkgPath> <exportFile> <depPkgNum>
Expand All @@ -168,7 +174,7 @@ DiskCache cacheFile format:

// Load loads the cache from a file.
func (p *Impl) Load(cacheFile string) (err error) {
b, err := os.ReadFile(cacheFile)
b, err := readFile(cacheFile)
if err != nil {
if os.IsNotExist(err) {
err = nil
Expand Down Expand Up @@ -230,7 +236,7 @@ func (p *Impl) Save(cacheFile string) (err error) {
}
return true
})
return os.WriteFile(cacheFile, buf.Bytes(), 0666)
return writeFile(cacheFile, buf.Bytes(), 0666)
}

// ----------------------------------------------------------------------------
128 changes: 128 additions & 0 deletions packages/cache/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
Copyright 2022 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cache

import (
"os"
"testing"

"github.com/goplus/gogen/packages"
)

func dirtyPkgHash(pkgPath string) string {
return ""
}

func nodirtyPkgHash(pkgPath string) string {
return "1"
}

func TestBasic(t *testing.T) {
readFile = func(string) ([]byte, error) {
return nil, os.ErrNotExist
}
c := New(nodirtyPkgHash)
err := c.Load("/not-found/gopkg.cache")
if err != nil {
t.Fatal("Load failed:", err)
}

p := packages.NewImporter(nil)
p.SetCache(c)
pkg, err := p.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import failed:", pkg, err)
}
if v := c.ListTimes(); v != 1 {
t.Fatal("ListTimes:", v)
}
if _, err = p.Import("not-found"); err == nil {
t.Fatal("Import not-found: no error?")
}
if pkg2, err := p.Import("fmt"); err != nil || pkg2 != pkg {
t.Fatal("Import reuse fail:", pkg, pkg2)
}
if v := c.ListTimes(); v != 2 {
t.Fatal("ListTimes:", v)
}
pkg, err = p.Import("github.com/goplus/gogen/internal/foo")
if err != nil {
t.Fatal("Import failed:", pkg, err)
}
if v := c.ListTimes(); v != 3 {
t.Fatal("ListTimes:", v)
}

var result []byte
writeFile = func(_ string, data []byte, _ os.FileMode) error {
result = data
return nil
}
readFile = func(string) ([]byte, error) {
return result, nil
}
c.Save("/not-found/gopkg.cache")

Check warning on line 77 in packages/cache/cache_test.go

View check run for this annotation

qiniu-x / golangci-lint

packages/cache/cache_test.go#L77

Error return value of `c.Save` is not checked (errcheck)

c2 := New(nodirtyPkgHash)
if err = c2.Load("/not-found/gopkg.cache"); err != nil {
t.Fatal("Load failed:", err)
}
p2 := packages.NewImporter(nil)
p2.SetCache(c2)
pkg, err = p2.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import failed:", pkg, err)
}
if v := c2.ListTimes(); v != 0 {
t.Fatal("ListTimes:", v)
}

c3 := New(dirtyPkgHash)
if err = c3.Load("/not-found/gopkg.cache"); err != nil {
t.Fatal("Load failed:", err)
}
p3 := packages.NewImporter(nil)
p3.SetCache(c3)
pkg, err = p3.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import failed:", pkg, err)
}
if v := c3.ListTimes(); v != 1 {
t.Fatal("ListTimes:", v)
}
}

func TestParseExport(t *testing.T) {
if _, e := parseExports("abc"); e != errInvalidFormat {
t.Fatal("parseExports failed:", e)
}
}

func TestErrLoadCache(t *testing.T) {
var c Impl
if err := c.loadCachePkgs([]string{"abc"}); err != errInvalidFormat {
t.Fatal("loadCachePkgs failed:", err)
}
if err := c.loadCachePkgs([]string{"abc\tefg\t3"}); err != errInvalidFormat {
t.Fatal("loadCachePkgs failed:", err)
}
if err := c.loadCachePkgs([]string{"abc\tefg\t1", "x"}); err != errInvalidFormat {
t.Fatal("loadCachePkgs failed:", err)
}
if err := c.loadCachePkgs([]string{"abc\tefg\t1", "\tx"}); err != errInvalidFormat {
t.Fatal("loadCachePkgs failed:", err)
}
}
11 changes: 7 additions & 4 deletions packages/imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ import (

// ----------------------------------------------------------------------------

type DiskCache interface {
// Cache represents a cache for the importer.
type Cache interface {
Find(dir, pkgPath string) (expfile string, err error)
}

// Importer represents a Go package importer.
type Importer struct {
loaded map[string]*types.Package
fset *token.FileSet
dir string
m sync.RWMutex
cache DiskCache
cache Cache
}

// NewImporter creates an Importer object that meets types.ImporterFrom and types.Importer interface.
Expand All @@ -54,11 +56,12 @@ func NewImporter(fset *token.FileSet, workDir ...string) *Importer {
return &Importer{loaded: loaded, fset: fset, dir: dir}
}

// SetDiskCache sets an optional disk cache for the importer.
func (p *Importer) SetDiskCache(cache DiskCache) {
// SetDiskCache sets an optional cache for the importer.
func (p *Importer) SetCache(cache Cache) {
p.cache = cache
}

// Import returns the imported package for the given import path.
func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
return p.ImportFrom(pkgPath, p.dir, 0)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/imp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func (p diskCache) Find(dir, pkgPath string) (expfile string, err error) {
return "", os.ErrNotExist
}

func TestDiskCache(t *testing.T) {
func TestCache(t *testing.T) {
nlist = 0
p := NewImporter(nil)
p.SetDiskCache(diskCache{})
p.SetCache(diskCache{})
_, err := p.Import("fmt")
if err != os.ErrNotExist {
t.Fatal("Import:", err)
}
p.SetDiskCache(diskCache{imp: NewImporter(nil)})
p.SetCache(diskCache{imp: NewImporter(nil)})
pkg, err := p.Import("fmt")
if err != nil || pkg.Path() != "fmt" {
t.Fatal("Import fmt:", pkg, err)
Expand Down