2019-08-08 21:50:32 +08:00
|
|
|
package unzip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2019-09-02 00:31:53 +08:00
|
|
|
"runtime"
|
2019-08-08 21:50:32 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func currentDir() string {
|
|
|
|
dir, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
func testNullContent(t *testing.T) {
|
2019-08-09 17:08:37 +08:00
|
|
|
aFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/a.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer aFile.Close()
|
|
|
|
|
|
|
|
aFileContent, err := ioutil.ReadAll(aFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
aOutFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/a.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer aOutFile.Close()
|
|
|
|
|
|
|
|
aOutFileContent, err := ioutil.ReadAll(aOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(aFileContent) != string(aOutFileContent) {
|
|
|
|
t.Fatal("Unzip file content error.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(aFileContent) != "" {
|
|
|
|
t.Fatal("Unzip file content error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testContentEqual(t *testing.T) {
|
2019-08-09 17:08:37 +08:00
|
|
|
bFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/b.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer bFile.Close()
|
|
|
|
|
|
|
|
bFileContent, err := ioutil.ReadAll(bFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
bOutFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/b.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer bOutFile.Close()
|
|
|
|
|
|
|
|
bOutFileContent, err := ioutil.ReadAll(bOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(bFileContent) != string(bOutFileContent) {
|
|
|
|
t.Log(string(bFileContent))
|
|
|
|
t.Log(string(bOutFileContent))
|
|
|
|
t.Fatal("Unzip file content error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLink(t *testing.T) {
|
2019-08-09 17:08:37 +08:00
|
|
|
cOutFile, err := os.OpenFile(filepath.FromSlash(path.Join(currentDir(), "./test/out/c.txt")), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cOutFile.Close()
|
|
|
|
|
|
|
|
cOutFileContent, err := ioutil.ReadAll(cOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
cOutLinkFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/c.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cOutLinkFile.Close()
|
|
|
|
|
|
|
|
cOutLinkFileContent, err := ioutil.ReadAll(cOutLinkFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(cOutFileContent) != string(cOutLinkFileContent) {
|
|
|
|
t.Fatal("Unzip file content error or link file content error.")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Old c.txt content:", string(cOutFileContent))
|
|
|
|
t.Log("Change c.txt content.")
|
|
|
|
_, err = cOutFile.WriteString("123")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
cOutFileNew, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/c.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cOutFileNew.Close()
|
|
|
|
|
|
|
|
cOutFileContentNew, err := ioutil.ReadAll(cOutFileNew)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log("New c.txt content:", string(cOutFileContentNew))
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
cOutLinkFileNew, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/c.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cOutLinkFileNew.Close()
|
|
|
|
|
|
|
|
cOutLinkFileContentNew, err := ioutil.ReadAll(cOutLinkFileNew)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(cOutFileContentNew) != string(cOutLinkFileContentNew) {
|
|
|
|
t.Fatal("Unzip file link error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSymlink(t *testing.T) {
|
2019-09-02 00:31:53 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Log("Windows zip no symlink")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
targetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/d.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("targetURL:", targetURL)
|
2019-08-09 17:08:37 +08:00
|
|
|
if targetURL != filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/d.txt")) {
|
2019-08-08 21:50:32 +08:00
|
|
|
t.Fatal("Unzip file Symlink error.")
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
dOutSymlinkFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/d.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dOutSymlinkFile.Close()
|
|
|
|
|
|
|
|
dOutSymlinkFileContent, err := ioutil.ReadAll(dOutSymlinkFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
dOutFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/d.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer dOutFile.Close()
|
|
|
|
|
|
|
|
dOutFileContent, err := ioutil.ReadAll(dOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(dOutSymlinkFileContent) != string(dOutFileContent) {
|
|
|
|
t.Fatal("The content of symlink file is not equal to that of target file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLintInDir(t *testing.T) {
|
2019-08-09 17:08:37 +08:00
|
|
|
eOutFile, err := os.OpenFile(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/e.txt")), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer eOutFile.Close()
|
|
|
|
|
|
|
|
eOutFileContent, err := ioutil.ReadAll(eOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
eOutLinkFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/e.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer eOutLinkFile.Close()
|
|
|
|
|
|
|
|
eOutLinkFileContent, err := ioutil.ReadAll(eOutLinkFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(eOutFileContent) != string(eOutLinkFileContent) {
|
|
|
|
t.Fatal("Unzip file content error or link file content error.")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Old dir/dir/e.txt content:", string(eOutFileContent))
|
|
|
|
t.Log("Change dir/dir/e.txt content.")
|
|
|
|
_, err = eOutFile.WriteString("abcde")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
eOutFileNew, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/e.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer eOutFileNew.Close()
|
|
|
|
|
|
|
|
eOutFileContentNew, err := ioutil.ReadAll(eOutFileNew)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log("New dir/dir/e.txt content:", string(eOutFileContentNew))
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
eOutLinkFileNew, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/e.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer eOutLinkFileNew.Close()
|
|
|
|
|
|
|
|
eOutLinkFileContentNew, err := ioutil.ReadAll(eOutLinkFileNew)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(eOutFileContentNew) != string(eOutLinkFileContentNew) {
|
|
|
|
t.Fatal("Unzip file link error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSymlinkInDir(t *testing.T) {
|
2019-09-02 00:31:53 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Log("Windows zip no symlink")
|
|
|
|
return
|
|
|
|
}
|
2019-08-09 17:08:37 +08:00
|
|
|
inDirTargetURL, err := filepath.EvalSymlinks(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/f.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log("F outSymlinkFile link:", inDirTargetURL)
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
if inDirTargetURL != filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/f.txt")) {
|
2019-08-08 21:50:32 +08:00
|
|
|
t.Fatal("Unzip file Symlink error.")
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
fOutSymlinkFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/dir/f.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fOutSymlinkFile.Close()
|
|
|
|
|
|
|
|
fOutSymlinkFileContent, err := ioutil.ReadAll(fOutSymlinkFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-09 17:08:37 +08:00
|
|
|
fOutFile, err := os.Open(filepath.FromSlash(path.Join(currentDir(), "./test/out/dir/f.txt")))
|
2019-08-08 21:50:32 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer fOutFile.Close()
|
|
|
|
|
|
|
|
fOutFileContent, err := ioutil.ReadAll(fOutFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(fOutSymlinkFileContent) != string(fOutFileContent) {
|
|
|
|
t.Fatal("The content of symlink file is not equal to that of target file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnzip(t *testing.T) {
|
|
|
|
filePath := filepath.FromSlash(path.Join(currentDir(), "./test/t.zip"))
|
|
|
|
outDir := filepath.FromSlash(path.Join(currentDir(), "./test/out") + "/")
|
|
|
|
|
|
|
|
os.RemoveAll(outDir)
|
|
|
|
|
|
|
|
unzip := New(filePath, outDir)
|
|
|
|
err := unzip.Extract()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Content is null", testNullContent)
|
|
|
|
t.Run("Content is equal", testContentEqual)
|
|
|
|
// t.Run("Link", testLink)
|
|
|
|
t.Run("Symlink", testSymlink)
|
|
|
|
// t.Run("Link in dir", testLintInDir)
|
|
|
|
t.Run("Symlink in dir", testSymlinkInDir)
|
|
|
|
}
|