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")
}
func TestFileChangeCacheOneDir(t *testing.T) {
	callLog := make([]string, 32)
	//递归可用
	kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache"))
	kmgFile.MustMkdirAll("testFile/d1/d2")
	kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("1"))
	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[3] = "f3"
	})
	kmgTest.Equal(callLog[3], "f3")

	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[4] = "f3"
	})
	kmgTest.Equal(callLog[4], "")

	time.Sleep(time.Second * 1)
	kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("2"))
	MustFileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[5] = "f3"
	})
	kmgTest.Equal(callLog[5], "f3")
}
Exemple #3
0
func installGolangWithUrlMap(urlMap map[string]string) {
	p := kmgPlatform.GetCompiledPlatform()
	if p.Compatible(kmgPlatform.WindowsAmd64) {
		contentB, err := kmgHttp.UrlGetContent(urlMap["windows_amd64"])
		kmgConsole.ExitOnErr(err)
		kmgFile.MustDelete(`c:\go`)
		err = kmgCompress.ZipUncompressFromBytesToDir(contentB, `c:\go`, "go")
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\go.exe`, `c:\windows\system32\go.exe`)
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\godoc.exe`, `c:\windows\system32\godoc.exe`)
		kmgConsole.ExitOnErr(err)
		err = kmgFile.CopyFile(`c:\go\bin\gofmt.exe`, `c:\windows\system32\gofmt.exe`)
		kmgConsole.ExitOnErr(err)
		return
	}
	tmpPath := kmgFile.MustChangeToTmpPath()
	defer kmgFile.MustDelete(tmpPath)
	if !kmgSys.MustIsRoot() {
		fmt.Println("you need to be root to install golang")
		return
	}

	url, ok := urlMap[p.String()]
	if !ok {
		kmgConsole.ExitOnErr(fmt.Errorf("not support platform [%s]", p))
	}
	packageName := path.Base(url)
	contentB := kmgHttp.MustUrlGetContentProcess(url)

	kmgFile.MustWriteFile(packageName, contentB)
	kmgCmd.ProxyRun("tar -xf " + packageName)
	if kmgFile.MustFileExist("/usr/local/go") {
		kmgCmd.ProxyRun("mv /usr/local/go /usr/local/go.bak." + time.Now().Format(kmgTime.FormatFileNameV2))
	}
	kmgCmd.ProxyRun("cp -rf go /usr/local")
	kmgFile.MustDeleteFile("/bin/go")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/go /bin/go")
	kmgFile.MustDeleteFile("/bin/godoc")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/godoc /bin/godoc")
	kmgFile.MustDeleteFile("/bin/gofmt")
	kmgCmd.ProxyRun("ln -s /usr/local/go/bin/gofmt /bin/gofmt")
	kmgFile.MustEnsureBinPath("/bin/go")
	kmgFile.MustEnsureBinPath("/bin/godoc")
	kmgFile.MustEnsureBinPath("/bin/gofmt")
}
Exemple #4
0
func selfUpdate() {
	baseFileContent, err := kmgHttp.UrlGetContent("http://kmgtools.qiniudn.com/v1/installKmg.bash?v=" + kmgRand.MustCryptoRandToAlphaNum(16))
	kmgConsole.ExitOnErr(err)

	baseFilePath := "/tmp/installKmg.bash"
	kmgFile.MustDeleteFile(baseFilePath)
	kmgFile.MustAppendFile(baseFilePath, baseFileContent)
	kmgCmd.MustRunInBash(baseFilePath)
}
Exemple #5
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()
}
Exemple #6
0
func TestContextWithFile(ot *testing.T) {
	if testContext == nil {
		ot.Skip("need config testContext todo the real upload download test")
		return
	}
	var err error
	err = testContext.RemovePrefix("/kmgTest/")
	kmgTest.Equal(err, nil)

	err = testContext.UploadFromFile("testFile", "/kmgTest/")
	kmgTest.Equal(err, nil)

	kmgFile.MustDeleteFile("testFile/downloaded.txt")
	err = testContext.DownloadToFile("/kmgTest/1.txt", "testFile/downloaded.txt")
	kmgTest.Equal(err, nil)

	kmgTest.Equal(kmgFile.MustReadFileAll("testFile/downloaded.txt"), []byte("abc"))
}
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], "")
}
func TestFileMd5ChangeCacheOneDir(t *testing.T) {
	callLog := make([]string, 32)
	//递归可用
	kmgFile.MustDeleteFile(getFileChangeCachePath("test_file_change_cache"))
	kmgFile.MustDelete("testFile/d1")

	kmgFile.MustMkdirAll("testFile/d1/d2")
	kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("1"))
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[3] = "f3"
	})
	kmgTest.Equal(callLog[3], "f3")

	//没有碰过任何东西,缓存有效
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[4] = "f3"
	})
	kmgTest.Equal(callLog[4], "")

	//修改文件内容,缓存应该无效
	kmgFile.MustWriteFile("testFile/d1/d2/f3", []byte("2"))
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[5] = "f3"
	})
	kmgTest.Equal(callLog[5], "f3")

	//删除文件,缓存应该无效
	kmgFile.MustDelete("testFile/d1/d2/f3")
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[6] = "f4"
	})
	kmgTest.Equal(callLog[6], "f4")

	//添加文件,缓存应该无效
	kmgFile.MustWriteFile("testFile/d1/d2/f4", []byte("3"))
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[7] = "f4"
	})
	kmgTest.Equal(callLog[7], "f4")

	//读取文件,缓存有效
	kmgFile.MustReadFile("testFile/d1/d2/f4")
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[8] = "f4"
	})
	kmgTest.Equal(callLog[8], "")

	//创建目录,缓存有效
	kmgFile.MustMkdir("testFile/d1/d2/f5")
	MustMd5FileChangeCache("test_file_change_cache", []string{
		"testFile/d1",
	}, func() {
		callLog[9] = "f4"
	})
	kmgTest.Equal(callLog[9], "")
}