Skip to content

Commit

Permalink
Merge pull request #58 from xushiwei/req
Browse files Browse the repository at this point in the history
TestClassfile
  • Loading branch information
xushiwei committed Feb 10, 2024
2 parents 3cc7ed4 + 8d5c718 commit 2f03835
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 9 additions & 8 deletions gopmod/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -55,21 +55,21 @@ 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
}

// 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
}

Expand All @@ -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)
Expand All @@ -100,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.Err(err) != syscall.ENOENT {
return
}
mod, err = modfetch.Get(mod.String())
Expand Down Expand Up @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions gopmod/gopmod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/goplus/mod/modload/modtest"
"golang.org/x/mod/module"
)

func TestLookup(t *testing.T) {
Expand Down Expand Up @@ -68,3 +69,36 @@ 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 _, 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)
}
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?")
}
}
7 changes: 3 additions & 4 deletions gopmod/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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.
Expand All @@ -174,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.Err(err) != syscall.ENOENT {
return
}
mod, err = modfetch.Get(mod.String())
Expand Down
13 changes: 13 additions & 0 deletions modload/modtest/modtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f03835

Please sign in to comment.