Skip to content

Commit

Permalink
Merge pull request #1745 from xushiwei/t
Browse files Browse the repository at this point in the history
NewDefaultConf: add (noTestFile bool) param
  • Loading branch information
xushiwei committed Feb 14, 2024
2 parents bd54356 + 1aa3eaa commit c84d8bd
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/internal/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func runCmd(cmd *base.Command, args []string) {
log.Panicln("too many arguments:", args)
}

conf, err := gop.NewDefaultConf(".")
conf, err := gop.NewDefaultConf(".", true)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/gengo/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".")
conf, err := gop.NewDefaultConf(".", false)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".")
conf, err := gop.NewDefaultConf(".", true)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func runCmd(cmd *base.Command, args []string) {
}

noChdir := *flagNoChdir
conf, err := gop.NewDefaultConf(".")
conf, err := gop.NewDefaultConf(".", true)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runCmd(cmd *base.Command, args []string) {
cl.SetDisableRecover(true)
}

conf, err := gop.NewDefaultConf(".")
conf, err := gop.NewDefaultConf(".", false)
if err != nil {
log.Panicln("gop.NewDefaultConf:", err)
}
Expand Down
32 changes: 29 additions & 3 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"go/types"
"io/fs"
"os"
"path"
"strings"

"github.com/goplus/gop/ast"
Expand Down Expand Up @@ -126,20 +127,26 @@ type Config struct {
DontUpdateGoMod bool
}

func NewDefaultConf(dir string) (conf *Config, err error) {
// NewDefaultConf creates a dfault configuration for common cases.
func NewDefaultConf(dir string, noTestFile bool) (conf *Config, err error) {
mod, err := LoadMod(dir)
if err != nil {
return
}
gop := gopenv.Get()
fset := token.NewFileSet()
imp := NewImporter(mod, gop, fset)
return &Config{Gop: gop, Fset: fset, Mod: mod, Importer: imp}, nil
conf = &Config{Gop: gop, Fset: fset, Mod: mod, Importer: imp}
if noTestFile {
conf.Filter = FilterNoTestFiles
}
return
}

// LoadMod loads a Go+ module from a specified directory.
func LoadMod(dir string) (mod *gopmod.Module, err error) {
mod, err = gopmod.Load(dir)
if err != nil && !NotFound(err) {
if err != nil && !gopmod.IsNotFound(err) {
err = errors.NewWith(err, `gopmod.Load(dir, 0)`, -2, "gopmod.Load", dir, 0)
return
}
Expand All @@ -153,13 +160,31 @@ func LoadMod(dir string) (mod *gopmod.Module, err error) {
return gopmod.Default, nil
}

// FilterNoTestFiles filters to skip all testing files.
func FilterNoTestFiles(fi fs.FileInfo) bool {
fname := fi.Name()
suffix := ""
switch path.Ext(fname) {
case ".gox":
suffix = "test.gox"
case ".gop":
suffix = "_test.gop"
case ".go":
suffix = "_test.go"
default:
return true
}
return !strings.HasSuffix(fname, suffix)
}

func hasModfile(mod *gopmod.Module) bool {
f := mod.File
return f != nil && f.Syntax != nil
}

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

// LoadDir loads Go+ packages from a specified directory.
func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (out, test *gox.Package, err error) {
if conf == nil {
conf = new(Config)
Expand Down Expand Up @@ -271,6 +296,7 @@ func relativeBaseOf(mod *gopmod.Module) string {

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

// LoadDir loads a Go+ package from specified files.
func LoadFiles(dir string, files []string, conf *Config) (out *gox.Package, err error) {
if conf == nil {
conf = new(Config)
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_gop.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func ParseFSEntry(fset *token.FileSet, fs FileSystem, filename string, src inter

func filter(d fs.DirEntry, fn func(fs.FileInfo) bool) bool {
fi, err := d.Info()
return err != nil || fn(fi)
return err == nil && fn(fi)
}

func reqPkg(pkgs map[string]*ast.Package, name string) *ast.Package {
Expand Down
21 changes: 20 additions & 1 deletion parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ import (

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

type fileInfo struct {
*fsx.FileInfo
}

func (p fileInfo) Info() (fs.FileInfo, error) {
return nil, fs.ErrNotExist
}

func TestFilter(t *testing.T) {
d := fsx.NewFileInfo("foo.go", 10)
if filter(d, func(fi fs.FileInfo) bool {
return false
}) {
t.Fatal("TestFilter failed")
t.Fatal("TestFilter: true?")
}
if !filter(d, func(fi fs.FileInfo) bool {
return true
}) {
t.Fatal("TestFilter: false?")
}
d2 := fileInfo{d}
if filter(d2, func(fi fs.FileInfo) bool {
return true
}) {
t.Fatal("TestFilter: true?")
}
}

Expand Down
4 changes: 3 additions & 1 deletion x/typesutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) (err error)
LookupPub: c2go.LookupPub(mod),
LookupClass: mod.LookupClass,
Importer: conf.Importer,
Recorder: gopRecorder{p.gopInfo},
Recorder: NewRecorder(p.gopInfo),
NoFileLine: true,
NoAutoGenMain: true,
NoSkipConstant: true,
Expand All @@ -169,12 +169,14 @@ func (p *Checker) Files(goFiles []*goast.File, gopFiles []*ast.File) (err error)
log.Println("typesutil.Check err:", err)
log.SingleStack()
}
// don't return even if err != nil
}
if len(files) > 0 {
scope := pkgTypes.Scope()
objMap := DeleteObjects(scope, files)
checker := types.NewChecker(conf, fset, pkgTypes, p.goInfo)
err = checker.Files(files)
// TODO: how to process error?
CorrectTypesInfo(scope, objMap, p.gopInfo.Uses)
}
return
Expand Down
6 changes: 6 additions & 0 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go/types"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/cl"
"github.com/goplus/gox"
"github.com/qiniu/x/log"
)
Expand Down Expand Up @@ -168,6 +169,11 @@ type gopRecorder struct {
*Info
}

// NewRecorder creates a new recorder for cl.NewPackage.
func NewRecorder(info *Info) cl.Recorder {
return gopRecorder{info}
}

// Type maps expressions to their types, and for constant
// expressions, also their values. Invalid expressions are
// omitted.
Expand Down

0 comments on commit c84d8bd

Please sign in to comment.