From 1a983cbb60bef528733eeafa5893c1200034cac5 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 11 Feb 2024 04:56:35 +0800 Subject: [PATCH 1/4] TestClassfile --- .github/workflows/go.yml | 2 +- gopmod/classfile.go | 13 +++++++------ gopmod/gopmod_test.go | 31 +++++++++++++++++++++++++++++++ gopmod/module.go | 5 ++--- modload/modtest/modtest.go | 13 +++++++++++++ 5 files changed, 54 insertions(+), 10 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 7836e05..aa1bd95 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -10,7 +10,7 @@ jobs: Test: strategy: matrix: - go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x] + go-version: [1.16.x, 1.18.x, 1.20.x, 1.21.x] os: [ubuntu-latest, windows-latest,macos-11] runs-on: ${{ matrix.os }} steps: diff --git a/gopmod/classfile.go b/gopmod/classfile.go index b7aaf81..62733e0 100644 --- a/gopmod/classfile.go +++ b/gopmod/classfile.go @@ -55,7 +55,7 @@ var ( // If it is, then it checks the fname is a project file or not. func (p *Module) ClassKind(fname string) (isProj, ok bool) { ext := modfile.ClassExt(fname) - if c, ok := p.projects[ext]; ok { + if c, ok := p.projs[ext]; ok { return c.IsProj(ext, fname), true } return @@ -63,13 +63,13 @@ func (p *Module) ClassKind(fname string) (isProj, ok bool) { // IsClass checks ext is a known classfile or not. func (p *Module) IsClass(ext string) (ok bool) { - _, ok = p.projects[ext] + _, ok = p.projs[ext] return } // LookupClass lookups a classfile by ext. func (p *Module) LookupClass(ext string) (c *Project, ok bool) { - c, ok = p.projects[ext] + c, ok = p.projs[ext] return } @@ -79,9 +79,10 @@ func (p *Module) ImportClasses(importClass ...func(c *Project)) (err error) { if importClass != nil { impcls = importClass[0] } + p.projs = make(map[string]*Project) p.importClass(TestProject, impcls) p.importClass(SpxProject, impcls) - p.projects[".gmx"] = SpxProject // old style + p.projs[".gmx"] = SpxProject // old style opt := p.Opt for _, c := range opt.Projects { p.importClass(c, impcls) @@ -130,9 +131,9 @@ func (p *Module) importClassFrom(modVer module.Version, impcls func(c *Project)) } func (p *Module) importClass(c *Project, impcls func(c *Project)) { - p.projects[c.Ext] = c + p.projs[c.Ext] = c for _, w := range c.Works { - p.projects[w.Ext] = c + p.projs[w.Ext] = c } if impcls != nil { impcls(c) diff --git a/gopmod/gopmod_test.go b/gopmod/gopmod_test.go index 4319a7a..6bc83e4 100644 --- a/gopmod/gopmod_test.go +++ b/gopmod/gopmod_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/goplus/mod/modload/modtest" + "golang.org/x/mod/module" ) func TestLookup(t *testing.T) { @@ -68,3 +69,33 @@ func TestPkgType(t *testing.T) { t.Fatal("mod.PkgType github.com/goplus/community/foo:", pt) } } + +func TestClassfile(t *testing.T) { + modVer := module.Version{Path: "github.com/goplus/yap", Version: "v0.5.0"} + mod, err := LoadMod(modVer) + if err != nil { + t.Fatal("LoadMod:", err) + } + if err = mod.ImportClasses(); err != nil { + t.Fatal("mod.ImportClasses:", err) + } + if c, ok := mod.LookupClass("_yap.gox"); !ok || c.Class != "App" { + t.Fatal("mod.LookupClass _yap.gox:", c.Class) + } + if !mod.IsClass("_yap.gox") { + t.Fatal("mod.IsClass _yap.gox: not ok?") + } +} + +func TestClassfile2(t *testing.T) { + mod := New(modtest.GopCommunity(t)) + if err := mod.ImportClasses(func(c *Project) {}); err != nil { + t.Fatal("mod.ImportClasses:", err) + } + if isProj, ok := mod.ClassKind("foo_yap.gox"); !ok || !isProj { + t.Fatal("mod.ClassKind foo_yap.gox:", isProj, ok) + } + if _, ok := mod.ClassKind("foo.gox"); ok { + t.Fatal("mod.ClassKind foo.gox:", ok) + } +} diff --git a/gopmod/module.go b/gopmod/module.go index a298b87..4d05dc4 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -35,7 +35,7 @@ import ( type Module struct { modload.Module - projects map[string]*Project // ext -> project + projs map[string]*Project // ext -> project depmods_ map[string]module.Version } @@ -148,8 +148,7 @@ func (p *Module) LookupDepMod(modPath string) (modVer module.Version, ok bool) { // New creates a module from a modload.Module instance. func New(mod modload.Module) *Module { - projects := make(map[string]*Project) - return &Module{projects: projects, Module: mod} + return &Module{Module: mod} } // Load loads a module from a local directory. diff --git a/modload/modtest/modtest.go b/modload/modtest/modtest.go index ea8cde0..ca61684 100644 --- a/modload/modtest/modtest.go +++ b/modload/modtest/modtest.go @@ -44,6 +44,19 @@ func Load(t *testing.T, gomodText, gopmodText string, errMsg string) modload.Mod return mod } +func GopCommunity(t *testing.T) modload.Module { + const gomodText = ` +module github.com/goplus/community + +go 1.18 + +require ( + github.com/goplus/yap v0.5.0 //gop:class +) +` + return Load(t, gomodText, ``, ``) +} + func GopClass(t *testing.T) modload.Module { const gomodText = ` module github.com/goplus/community From 6517c5f9f3daddfdf1db2b9c6906334d073528e6 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 11 Feb 2024 04:58:40 +0800 Subject: [PATCH 2/4] TestClassfile2 --- gopmod/gopmod_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gopmod/gopmod_test.go b/gopmod/gopmod_test.go index 6bc83e4..e41670e 100644 --- a/gopmod/gopmod_test.go +++ b/gopmod/gopmod_test.go @@ -89,6 +89,9 @@ func TestClassfile(t *testing.T) { func TestClassfile2(t *testing.T) { mod := New(modtest.GopCommunity(t)) + if _, ok := mod.ClassKind("foo_yap.gox"); ok { + t.Fatal("mod.ClassKind foo_yap.gox: ok?") + } if err := mod.ImportClasses(func(c *Project) {}); err != nil { t.Fatal("mod.ImportClasses:", err) } @@ -96,6 +99,6 @@ func TestClassfile2(t *testing.T) { t.Fatal("mod.ClassKind foo_yap.gox:", isProj, ok) } if _, ok := mod.ClassKind("foo.gox"); ok { - t.Fatal("mod.ClassKind foo.gox:", ok) + t.Fatal("mod.ClassKind foo.gox: ok?") } } From 0181ef807db6bc9540af7371d7d988a21f17bfea Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 11 Feb 2024 05:07:13 +0800 Subject: [PATCH 3/4] x --- gopmod/classfile.go | 2 +- gopmod/module.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/gopmod/classfile.go b/gopmod/classfile.go index 62733e0..1300386 100644 --- a/gopmod/classfile.go +++ b/gopmod/classfile.go @@ -101,7 +101,7 @@ func (p *Module) importMod(modPath string, imcls func(c *Project)) (err error) { return syscall.ENOENT } err = p.importClassFrom(mod, imcls) - if err != syscall.ENOENT { + if errors.Unwrap(err) != syscall.ENOENT { return } mod, err = modfetch.Get(mod.String()) diff --git a/gopmod/module.go b/gopmod/module.go index 4d05dc4..b14bad8 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -173,7 +173,7 @@ func LoadFrom(gomod, gopmod string) (*Module, error) { // If we only want to load a Go modfile, pass env parameter as nil. func LoadMod(mod module.Version) (p *Module, err error) { p, err = loadModFrom(mod) - if err != syscall.ENOENT { + if errors.Unwrap(err) != syscall.ENOENT { return } mod, err = modfetch.Get(mod.String()) From 8d5c718bd8258bdeceeb98fdedde20a8e72854ed Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 11 Feb 2024 05:14:47 +0800 Subject: [PATCH 4/4] errors.Err --- gopmod/classfile.go | 4 ++-- gopmod/module.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gopmod/classfile.go b/gopmod/classfile.go index 1300386..0ffed46 100644 --- a/gopmod/classfile.go +++ b/gopmod/classfile.go @@ -17,13 +17,13 @@ package gopmod import ( - "errors" "syscall" "github.com/goplus/mod/modcache" "github.com/goplus/mod/modfetch" "github.com/goplus/mod/modfile" "github.com/goplus/mod/modload" + "github.com/qiniu/x/errors" "golang.org/x/mod/module" ) @@ -101,7 +101,7 @@ func (p *Module) importMod(modPath string, imcls func(c *Project)) (err error) { return syscall.ENOENT } err = p.importClassFrom(mod, imcls) - if errors.Unwrap(err) != syscall.ENOENT { + if errors.Err(err) != syscall.ENOENT { return } mod, err = modfetch.Get(mod.String()) diff --git a/gopmod/module.go b/gopmod/module.go index b14bad8..27f2b3a 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -173,7 +173,7 @@ func LoadFrom(gomod, gopmod string) (*Module, error) { // If we only want to load a Go modfile, pass env parameter as nil. func LoadMod(mod module.Version) (p *Module, err error) { p, err = loadModFrom(mod) - if errors.Unwrap(err) != syscall.ENOENT { + if errors.Err(err) != syscall.ENOENT { return } mod, err = modfetch.Get(mod.String())