Beispiel #1
0
func TestMustIsFileInIndex(t *testing.T) {
	GitTestCb(func() {
		kmgCmd.MustRun("git init")
		kmgFile.MustWriteFile("1.txt", []byte("1"))
		kmgFile.MustWriteFile("2.txt", []byte("1"))
		kmgCmd.MustRun("git add 1.txt")
		repo := MustGetRepositoryFromPath(".")
		kmgTest.Equal(repo.MustIsFileInIndex("1.txt"), true)
		kmgTest.Equal(repo.MustIsFileInIndex("2.txt"), false)
	})
}
Beispiel #2
0
func TestDemo(ot *testing.T) {
	importPath := "github.com/bronze1man/kmg/kmgGoSource"
	kmgCmd.MustRun("kmg go test -i " + importPath)
	pkgDir := kmgConfig.DefaultEnv().MustGetPathFromImportPath(importPath)
	fset := token.NewFileSet()
	astPkgMap, err := parser.ParseDir(fset, pkgDir, nil, 0)
	if err != nil {
		panic(err)
	}
	astPkg := astPkgMap["kmgGoSource_test"]
	astFileList := []*ast.File{}
	for _, file := range astPkg.Files {
		astFileList = append(astFileList, file)
	}
	//os.Chdir(kmgConfig.DefaultEnv().ProjectPath)
	pkg, err := types.Check(pkgDir, fset, astFileList)
	if err != nil {
		panic(err)
	}
	funcA := pkg.Scope().Lookup("FuncA")
	recvPkg := types.NewPackage("github.com/bronze1man/kmg/kmgGoSource", "kmgGoSource")
	kmgDebug.Println(types.TypeString(recvPkg, funcA.Type()))
	funTypParams := funcA.Type().(*types.Signature).Params()
	for i := 0; i < funTypParams.Len(); i++ {
		kmgDebug.Println(funTypParams.At(i).Name())
		kmgDebug.Println(funTypParams.At(i).Type().String())
	}
	//for _,p:=range funcA.Type().(*types.Signature).Params().
	//kmgDebug.Println(funcA.Type().(*types.Signature).Params().String())
}
Beispiel #3
0
func MustSetIptableRule(rule IptableRule) {
	for _, thisRule := range MustGetIptableRuleList() {
		if thisRule.Table == rule.Table && thisRule.Rule == rule.Rule {
			return
		}
	}
	// Another app is currently holding the xtables lock. Perhaps you want to use the -w option?
	kmgCmd.MustRun("iptables -w -t " + rule.Table + " " + rule.Rule)
}
Beispiel #4
0
func main() {
	kmgCmd.MustRun(`kmg GoCrossCompile -platform linux_amd64 github.com/bronze1man/kmg/kmg`)
	kmgCmd.MustRun(`kmg GoCrossCompile -platform linux_amd64 github.com/bronze1man/kmg/kmg/SubCommand/serviceCmd/testBin`)
	kmgFile.MustCopyFile(filepath.Join(kmgConfig.DefaultEnv().ProjectPath, "bin/kmg_linux_amd64"), filepath.Join(dockerPath, "kmg"))
	kmgFile.MustCopyFile(filepath.Join(kmgConfig.DefaultEnv().ProjectPath, "bin/testBin_linux_amd64"), filepath.Join(dockerPath, "testBin"))
	kmgFile.MustWriteFile(filepath.Join(dockerPath, "Dockerfile"), []byte(`FROM ubuntu
WORKDIR /
COPY kmg /bin/
COPY testBin /bin/
RUN chmod +x /bin/kmg
RUN chmod +x /bin/testBin
CMD testBin
`))
	//CMD kmg service setandrestart t testBin && kmg service stop t && kmg service start t && kmg service restart t
	kmgCmd.MustRunAndReturnOutput("docker build -t kmgtest " + dockerPath)
	for i := 0; i < 20; i++ {
		time.Sleep(time.Second)
		kmgCmd.MustRunAndReturnOutput("docker run kmgtest")
	}
}
Beispiel #5
0
func TestGitSubmoduleAddRealSubmodule(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()

		kmgCmd.MustRun("git add -A")
		kmgCmd.MustRun("git commit -am'save'")

		repo := MustGetRepositoryFromPath(".")
		kmgTest.Equal(repo.MustIsFileInIndex("sub/1.txt"), false)
		kmgTest.Equal(repo.MustIsFileInIndex("sub"), true)

		repo.MustFakeSubmoduleAdd("sub")
		kmgTest.Equal(repo.MustIsFileInIndex("sub"), false)
		kmgTest.Equal(repo.MustIsFileInIndex("sub/1.txt"), true)
	})
}
Beispiel #6
0
func TestMustIsFileIgnore(t *testing.T) {
	GitTestCb(func() {
		kmgCmd.MustRun("git init")
		kmgFile.MustWriteFile(".gitignore", []byte("/1.txt"))
		kmgFile.MustWriteFile("1.txt", []byte("1"))
		kmgFile.MustWriteFile("2.txt", []byte("1"))

		repo := MustGetRepositoryFromPath(".")
		kmgTest.Equal(repo.MustIsFileIgnore("1.txt"), true)
		kmgTest.Equal(repo.MustIsFileIgnore("notExist.txt"), false)
		kmgTest.Equal(repo.MustIsFileIgnore("2.txt"), false)
	})
}
Beispiel #7
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)
	})
}
Beispiel #8
0
func MustEnsureBinPath(finalPath string) {
	basePath := filepath.Base(finalPath)
	path, err := exec.LookPath(basePath)
	if err != nil {
		if os.IsNotExist(err) {
			return
		}
		panic(err)
	}
	if path != finalPath {
		backPathDir := "/var/backup/bin/" + basePath + time.Now().Format(kmgTime.FormatFileName)
		MustMkdirAll(backPathDir)
		kmgCmd.MustRun("mv " + path + " " + backPathDir)
	}
}
Beispiel #9
0
func TestJava(ot *testing.T) {
	closer := testPackage.ListenAndServe_Demo(":34895", &testPackage.Demo{}, kmgCrypto.Get32PskFromString("abc psk"))
	defer closer()
	os.Chdir(filepath.Join("java", "src"))
	MustGenerateCode(&GenerateRequest{
		ObjectPkgPath:   "github.com/bronze1man/kmg/kmgRpc/testPackage",
		ObjectName:      "Demo",
		ObjectIsPointer: true,
		OutFilePath:     "testPackage/RpcDemo.java",
		OutPackageName:  "testPackage",
		OutClassName:    "RpcDemo",
	})
	kmgCmd.MustRun("javac -sourcepath . ./testPackage/Main.java")
	out := kmgCmd.MustRunAndReturnOutput("java -classpath . testPackage.Main")
	kmgTest.Equal(out, []byte("Success\n"))
}
Beispiel #10
0
func TestRpcDemo(ot *testing.T) {
	importPath := "github.com/bronze1man/kmg/kmgGoSource"
	kmgCmd.MustRun("kmg go install " + importPath)
	typ := MustGetGoTypesFromReflect(reflect.TypeOf(&RpcDemo{}))

	typ1, ok := typ.(*types.Pointer)
	kmgTest.Equal(ok, true)

	typ2, ok := typ1.Elem().(*types.Named)
	kmgTest.Equal(ok, true)
	kmgTest.Equal(typ2.NumMethods(), 1)

	obj3 := typ2.Method(0)
	kmgTest.Equal(obj3.Name(), "PostScoreInt")

	typ4, ok := obj3.Type().(*types.Signature)
	kmgTest.Equal(ok, true)
	kmgTest.Equal(typ4.Params().Len(), 3)
	kmgTest.Equal(typ4.Results().Len(), 2)

	for _, testCase := range []struct {
		Type   types.Type
		Expect string
	}{
		{typ4.Params().At(0).Type(), "string"},
		{typ4.Params().At(1).Type(), "int"},
		{typ4.Params().At(2).Type(), "*RpcDemo"},
		{typ4.Results().At(0).Type(), "string"},
		{typ4.Results().At(1).Type(), "error"},
	} {
		typS, importPathList := MustWriteGoTypes("github.com/bronze1man/kmg/kmgGoSource", testCase.Type)
		kmgTest.Equal(typS, testCase.Expect)
		kmgTest.Equal(len(importPathList), 0)
	}

	typS, importPathList := MustWriteGoTypes("github.com/bronze1man/kmg/kmgTest", typ4.Params().At(2).Type())
	kmgTest.Equal(typS, "*kmgGoSource.RpcDemo")
	kmgTest.Equal(importPathList, []string{"github.com/bronze1man/kmg/kmgGoSource"})

	methodSet := types.NewMethodSet(typ)
	kmgTest.Equal(methodSet.Len(), 1)
	kmgTest.Equal(methodSet.At(0).Obj().Name(), "PostScoreInt")

}
Beispiel #11
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], "")
}
Beispiel #12
0
func main() {
	//var exitSignalProcessor func(signal os.Signal) // 只杀掉一层子进程不管用,bash -c 不会传递信号.
	go func() {
		kmgCmd.MustRun("kmg go install github.com/bronze1man/kmg/kmgCmd/testPackage/testCmdChildren")
		cmd := kmgCmd.CmdBash("./bin/testCmdChildren cmd | tee -i /tmp/1.log")
		//exitSignalProcessor = func(signal os.Signal){
		//	err := cmd.GetExecCmd().Process.Signal(signal)
		//	if err!=nil{
		//		panic(err)
		//	}
		//}
		cmd.MustRun()
		fmt.Println("parent Must Run return")
	}()
	ch := make(chan os.Signal, 10)
	signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)
	thisSignal := <-ch
	fmt.Println("parent", thisSignal)
	//if exitSignalProcessor!=nil {
	//	exitSignalProcessor(thisSignal)
	//}
}
Beispiel #13
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'")
}
Beispiel #14
0
func SetDnsServerAddr(ip string) {
	if !kmgPlatform.IsDarwin() {
		panic("not support platform")
	}
	kmgCmd.MustRun("networksetup -setdnsservers Wi-Fi " + ip)
}