Ejemplo n.º 1
0
func TestFileChangeCacheOneFile(t *testing.T) {
	//可以递归 遍历文件
	//缓存数据文件不存在,没有问题
	//指定的文件不存在,也没有问题
	callLog := make([]string, 32)
	//指定的文件不存在
	kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache"))
	kmgFile.MustDeleteFile("testFile/notExist")

	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/notExist",
	}, func() {
		callLog[1] = "notExist"
		kmgFile.MustWriteFileWithMkdir("testFile/notExist", []byte("1"))
	})
	kmgTest.Equal(callLog[1], "notExist")

	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/notExist",
	}, func() {
		callLog[2] = "notExist"
	})
	kmgTest.Equal(callLog[2], "")

	time.Sleep(time.Second * 1)

	kmgFile.MustWriteFile("testFile/notExist", []byte("2"))
	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/notExist",
	}, func() {
		callLog[3] = "notExist"
	})
	kmgTest.Equal(callLog[3], "notExist")
}
Ejemplo n.º 2
0
//生成代码
// 会把Object上面所有的公开函数都拿去生成一遍
func MustGenerateCode(req *GenerateRequest) {
	config := reflectToTplConfig(req)
	outBs := tplGenerateCode(config)
	outB := []byte(outBs)
	outB1, err := kmgFormat.Source(outB)
	if err == nil {
		outB = outB1
	}
	kmgFile.MustWriteFileWithMkdir(req.OutFilePath, outB)
	return
}
Ejemplo n.º 3
0
//生成代码
// 此处只生成swift代码,不生成golang代码.
// 限制: 输出只能有一个参数,
func MustGenerateCode(req *GenerateRequest) {
	config := reflectToTplConfig(req)
	outBs := tplGenerateCode(config)
	outB := kmgFormat.RemoteEmptyLine([]byte(outBs))
	kmgFile.MustWriteFileWithMkdir(req.OutFilePath, outB)
	if req.NeedSource {
		//生成xxx-Bridging-Header.h NSData+Compression.h NSData+Compression.m
		BridgingHeaderContent := `
//  请将该文件放到根目录的项目名文件下
#import "NSData+Compression.h"
	`
		path := strings.Split(req.OutFilePath, "/")
		parPath := strings.Join(path[:(len(path)-1)], "/") + "/"
		BridgingHeaderPath := parPath + req.OutProjectName + "-Bridging-Header.h"
		NSDataCompressionMethodPath := parPath + "NSData+Compression.m"
		NSDataCompressionHeadPath := parPath + "NSData+Compression.h"
		InfoListPath := parPath + "Info.plist"
		path = strings.Split(req.XcodeprojPath, "/")
		projectPath := strings.Join(path[:(len(path)-1)], "/") + "/"
		podFilePath := projectPath + "Podfile"
		xcodeprojPath := req.XcodeprojPath
		kmgFile.MustWriteFileWithMkdir(BridgingHeaderPath, []byte(BridgingHeaderContent))
		kmgFile.MustWriteFileWithMkdir(NSDataCompressionMethodPath, []byte(NSDataCompressionMethod()))
		kmgFile.MustWriteFileWithMkdir(NSDataCompressionHeadPath, []byte(NSDataCompressionHead()))
		kmgFile.MustWriteFileWithMkdir(InfoListPath, []byte(InfoList()))
		kmgFile.MustWriteFileWithMkdir(podFilePath, []byte(Podfile(req.OutProjectName)))
		kmgFileToXcode.AddFilesToXcode([]string{req.OutFilePath, BridgingHeaderPath, NSDataCompressionMethodPath, NSDataCompressionHeadPath}, xcodeprojPath)
		cmd := kmgCmd.CmdBash("export LANG=UTF-8;pod install")
		cmd.SetDir(projectPath)
		cmd.MustRun()
	}
}
Ejemplo n.º 4
0
func TestGitSubmoduleAddNotInIndex(ot *testing.T) {
	GitTestCb(func() {
		kmgCmd.MustRun("git init")
		kmgFile.MustWriteFileWithMkdir("sub/1.txt", []byte("1"))
		kmgCmd.CmdString("git init").SetDir("sub").MustRun()
		kmgCmd.CmdString("git add -A").SetDir("sub").MustRun()
		kmgCmd.CmdString("git commit -am'save'").SetDir("sub").MustRun()

		repo := MustGetRepositoryFromPath(".")
		repo.MustFakeSubmoduleAdd("sub")
		kmgTest.Equal(repo.MustIsFileInIndex("sub/1.txt"), true)
	})
}
Ejemplo n.º 5
0
func TestGitSubmoduleInitIgnore(ot *testing.T) {
	kmgGit.GitTestCb(func() {
		kmgCmd.MustRun("git init")
		kmgFile.MustWriteFile(".gitignore", []byte("/subIgnored"))
		kmgFile.MustWriteFileWithMkdir("subIgnored/1.txt", []byte("1"))
		kmgCmd.CmdString("git init").SetDir("subIgnored").MustRun()
		kmgCmd.CmdString("git add -A").SetDir("subIgnored").MustRun()
		kmgCmd.CmdString("git commit -am'save'").SetDir("subIgnored").MustRun()

		repo := kmgGit.MustGetRepositoryFromPath(".")
		GitSubmoduleInit(repo)
		kmgTest.Equal(repo.MustIsFileInIndex("subIgnored"), false)
	})
}
Ejemplo n.º 6
0
func TestTplGenerateCode(t *testing.T) {
	out := tplGenerateCode(&tplConfig{
		OutPackageName: "tplTestPackage",
		ObjectName:     "Demo",
		ObjectTypeStr:  "*Demo",
		ApiList: []Api{
			{
				Name: "PostScoreInt",
				InArgsList: []ArgumentNameTypePair{
					{
						Name:          "LbId",
						ObjectTypeStr: "string",
					},
					{
						Name:          "Score",
						ObjectTypeStr: "int",
					},
				},
				OutArgsList: []ArgumentNameTypePair{
					{
						Name:          "Info",
						ObjectTypeStr: "string",
					},
					{
						Name:          "Err",
						ObjectTypeStr: "error",
					},
				},
			},
		},
		ImportPathMap: map[string]bool{
			"encoding/json": true,
			"errors":        true,
			"fmt":           true,
			"github.com/bronze1man/kmg/kmgCrypto":      true,
			"github.com/bronze1man/kmg/kmgLog":         true,
			"github.com/bronze1man/kmg/kmgNet/kmgHttp": true,
			"net/http": true,
			"bytes":    true,
		},
	})
	kmgFile.MustDeleteFile("tplTestPackage/generated.go")
	kmgFile.MustWriteFileWithMkdir("tplTestPackage/generated.go", []byte(out))
	kmgCmd.CmdString("kmg go test").SetDir("tplTestPackage").Run()
}
Ejemplo n.º 7
0
func TestGitFixNameCaseWithDirectory(ot *testing.T) {
	oldWd, err := os.Getwd()
	kmgTest.Equal(err, nil)
	kmgFile.MustDelete("testfile")
	kmgFile.MustWriteFileWithMkdir("testfile/a/a.txt", []byte("abc"))
	os.Chdir("testfile")
	defer os.Chdir(oldWd)
	kmgCmd.MustRun("git init")
	kmgCmd.MustRun("git add -A")
	kmgCmd.MustRun("git commit -am'save'")
	err = os.Rename("a", "A")
	kmgTest.Equal(err, nil)

	err = GitFixNameCase(filepath.Join(oldWd, "testfile"))
	kmgTest.Equal(err, nil)

	kmgCmd.MustRun("git status")
	kmgCmd.MustRun("git add -A")
	kmgCmd.MustRun("git commit -am'save'")
}
Ejemplo n.º 8
0
func TestFileMd5ChangeCacheSymlink(t *testing.T) {
	callLog := make([]string, 32)
	//递归可用
	kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache"))
	kmgFile.MustDelete("testFile")
	kmgFile.MustWriteFileWithMkdir("testFile/d1/d2", []byte("1"))
	kmgCmd.MustRun("ln -s d1 testFile/d3")
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile",
	}, func() {
		callLog[0] = "f3"
	})
	kmgTest.Equal(callLog[0], "f3")
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile",
	}, func() {
		callLog[1] = "f3"
	})
	kmgTest.Equal(callLog[1], "")
}
Ejemplo n.º 9
0
func MustKvdbSetBytes(s string, b []byte) {
	key := kmgCrypto.Md5Hex([]byte(s))
	kmgFile.MustWriteFileWithMkdir(kmgConfig.DefaultEnv().PathInTmp("kvdb/"+key), b)
}
Ejemplo n.º 10
0
//生成代码
// 此处只生成java代码,不生成golang代码.
// 限制: 输出只能有一个参数,
func MustGenerateCode(req *GenerateRequest) {
	config := reflectToTplConfig(req)
	outBs := tplGenerateCode(config)
	outB := kmgFormat.RemoteEmptyLine([]byte(outBs))
	kmgFile.MustWriteFileWithMkdir(req.OutFilePath, outB)
}