Skip to content

Commit

Permalink
单测fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lvyahui8 committed Jan 6, 2024
1 parent bee0839 commit be7dd03
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ellyn_ast/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *Program) _init() {
}
p.mainPkg.Name = p.pkgMap[p.mainPkg.Dir].Name
p.modFile = utils.Go.GetModFile(p.mainPkg.Dir)
rootPkgPath := utils.Go.GetRootPkgPath(p.modFile)
rootPkgPath := utils.Go.GetProjectRootPkgPath(p.modFile)
p.rootPkg = NewPackage(path.Dir(p.modFile), rootPkgPath)
p.progCtx = &ProgramContext{rootPkgPath: rootPkgPath}
})
Expand Down
14 changes: 10 additions & 4 deletions ellyn_common/utils/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func (g *goUtils) AllPackages(mainPkgPath string) map[string]string {
}

// GetGoEnv 获取指定的go env
func (g *goUtils) GetGoEnv(mainPkgPath, envKey string) string {
out := Shell.Exec(mainPkgPath, "go", "env", envKey)
func (g *goUtils) GetGoEnv(workDir, envKey string) string {
out := Shell.Exec(workDir, "go", "env", envKey)
return strings.TrimSpace(out)
}

Expand All @@ -41,15 +41,21 @@ func (g *goUtils) GetModFile(mainPkgPath string) string {
return g.GetGoEnv(mainPkgPath, "GOMOD")
}

// GetRootPkgPath 获取项目go.mod文件所在的package name
func (g *goUtils) GetRootPkgPath(modFilePath string) string {
// GetProjectRootPkgPath 获取项目go.mod文件所在的package name
func (g *goUtils) GetProjectRootPkgPath(modFilePath string) string {
content, err := os.ReadFile(modFilePath)
asserts.IsNil(err)
modFile, err := modfile.Parse("go.mod", content, nil)
asserts.IsNil(err)
return modFile.Module.Mod.Path
}

func (g *goUtils) GetGoRootDir() string {
wd, err := os.Getwd()
asserts.IsNil(err)
return g.GetGoEnv(wd, "GOROOT")
}

func (g *goUtils) IsTestFile(file string) bool {
return strings.HasSuffix(file, "_test.go")
}
Expand Down
7 changes: 4 additions & 3 deletions ellyn_common/utils/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func TestGoUtils_GetModFile(t *testing.T) {
}

func TestGoUtils_GetRootPkg(t *testing.T) {
rootPkg := Go.GetRootPkgPath(Go.GetModFile(ellyn_testing.GetTestProjPath()))
rootPkg := Go.GetProjectRootPkgPath(Go.GetModFile(ellyn_testing.GetTestProjPath()))
require.Equal(t, "test_proj", rootPkg)
}

func TestGoUtils_IsAutoGenFile(t *testing.T) {
require.True(t, Go.IsAutoGenFile("D:\\software\\go\\go1.18\\src\\cmd\\go\\internal\\test\\flagdefs.go"))
require.False(t, Go.IsAutoGenFile("D:\\software\\go\\go1.18\\src\\cmd\\go\\internal\\test\\cover.go"))
goRootDir := Go.GetGoRootDir()
require.True(t, Go.IsAutoGenFile(goRootDir+"\\src\\cmd\\go\\internal\\test\\flagdefs.go"))
require.False(t, Go.IsAutoGenFile(goRootDir+"\\src\\cmd\\go\\internal\\test\\cover.go"))
}

0 comments on commit be7dd03

Please sign in to comment.