123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package generator
- import (
- "go/build"
- "os"
- "path/filepath"
- "strings"
- "testing"
- "git.i2edu.net/i2/go-zero/core/logx"
- "git.i2edu.net/i2/go-zero/core/stringx"
- conf "git.i2edu.net/i2/go-zero/tools/goctl/config"
- "git.i2edu.net/i2/go-zero/tools/goctl/rpc/execx"
- "github.com/stretchr/testify/assert"
- )
- var cfg = &conf.Config{
- NamingFormat: "gozero",
- }
- func TestRpcGenerate(t *testing.T) {
- _ = Clean()
- dispatcher := NewDefaultGenerator()
- err := dispatcher.Prepare()
- if err != nil {
- logx.Error(err)
- return
- }
- projectName := stringx.Rand()
- g := NewRPCGenerator(dispatcher, cfg)
- // case go path
- src := filepath.Join(build.Default.GOPATH, "src")
- _, err = os.Stat(src)
- if err != nil {
- return
- }
- projectDir := filepath.Join(src, projectName)
- srcDir := projectDir
- defer func() {
- _ = os.RemoveAll(srcDir)
- }()
- err = g.Generate("./test.proto", projectDir, []string{src})
- assert.Nil(t, err)
- _, err = execx.Run("go test "+projectName, projectDir)
- if err != nil {
- assert.True(t, func() bool {
- return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
- }())
- }
- // case go mod
- workDir := t.TempDir()
- name := filepath.Base(workDir)
- _, err = execx.Run("go mod init "+name, workDir)
- if err != nil {
- logx.Error(err)
- return
- }
- projectDir = filepath.Join(workDir, projectName)
- err = g.Generate("./test.proto", projectDir, []string{src})
- assert.Nil(t, err)
- _, err = execx.Run("go test "+projectName, projectDir)
- if err != nil {
- assert.True(t, func() bool {
- return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
- }())
- }
- // case not in go mod and go path
- err = g.Generate("./test.proto", projectDir, []string{src})
- assert.Nil(t, err)
- _, err = execx.Run("go test "+projectName, projectDir)
- if err != nil {
- assert.True(t, func() bool {
- return strings.Contains(err.Error(), "not in GOROOT") || strings.Contains(err.Error(), "cannot find package")
- }())
- }
- }
|