You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
3 years ago
|
package pathx
|
||
3 years ago
|
|
||
|
import (
|
||
3 years ago
|
"io/ioutil"
|
||
3 years ago
|
"os"
|
||
|
"path/filepath"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
3 years ago
|
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
|
||
3 years ago
|
)
|
||
|
|
||
3 years ago
|
func TestGetTemplateDir(t *testing.T) {
|
||
|
category := "foo"
|
||
|
t.Run("before_have_templates", func(t *testing.T) {
|
||
|
home := t.TempDir()
|
||
|
RegisterGoctlHome("")
|
||
|
RegisterGoctlHome(home)
|
||
|
v := version.GetGoctlVersion()
|
||
|
dir := filepath.Join(home, v, category)
|
||
|
err := MkdirIfNotExist(dir)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
tempFile := filepath.Join(dir, "bar.txt")
|
||
|
err = ioutil.WriteFile(tempFile, []byte("foo"), os.ModePerm)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
templateDir, err := GetTemplateDir(category)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
assert.Equal(t, dir, templateDir)
|
||
|
RegisterGoctlHome("")
|
||
|
})
|
||
|
|
||
|
t.Run("before_has_no_template", func(t *testing.T) {
|
||
|
home := t.TempDir()
|
||
|
RegisterGoctlHome("")
|
||
|
RegisterGoctlHome(home)
|
||
|
dir := filepath.Join(home, category)
|
||
|
err := MkdirIfNotExist(dir)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
templateDir, err := GetTemplateDir(category)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
assert.Equal(t, dir, templateDir)
|
||
|
})
|
||
|
|
||
|
t.Run("default", func(t *testing.T) {
|
||
|
RegisterGoctlHome("")
|
||
|
dir, err := GetTemplateDir(category)
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
assert.Contains(t, dir, version.BuildVersion)
|
||
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
func TestGetGitHome(t *testing.T) {
|
||
|
homeDir, err := os.UserHomeDir()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
actual, err := GetGitHome()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
expected := filepath.Join(homeDir, goctlDir, gitDir)
|
||
|
assert.Equal(t, expected, actual)
|
||
|
}
|