github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/cae/tz/tz_test.go (about)

     1  // Copyright 2014 Unknown
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package tz
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path"
    21  	"strings"
    22  	"testing"
    23  
    24  	. "github.com/insionng/yougam/libraries/smartystreets/goconvey/convey"
    25  	"github.com/insionng/yougam/libraries/Unknwon/com"
    26  )
    27  
    28  func TestCreate(t *testing.T) {
    29  	Convey("Create a tar.gz file", t, func() {
    30  		_, err := Create(path.Join(os.TempDir(), "testdata/TestCreate.tar.gz"))
    31  		So(err, ShouldBeNil)
    32  	})
    33  }
    34  
    35  func TestOpen(t *testing.T) {
    36  	Convey("Open a tar.gz file normally with read-only flag", t, func() {
    37  		z, err := Open("testdata/test.tar.gz")
    38  		So(err, ShouldBeNil)
    39  		So(z.FileName, ShouldEqual, "testdata/test.tar.gz")
    40  		So(z.NumFiles, ShouldEqual, 5)
    41  		So(z.Flag, ShouldEqual, os.O_RDONLY)
    42  		So(z.Permission, ShouldEqual, 0)
    43  	})
    44  
    45  	Convey("Open a tar.gz file that does not exist", t, func() {
    46  		_, err := Open("testdata/404.tar.gz")
    47  		So(err, ShouldNotBeNil)
    48  	})
    49  
    50  	Convey("Open a file that is not a tar.gz file", t, func() {
    51  		_, err := Open("testdata/readme.notzip")
    52  		So(err, ShouldNotBeNil)
    53  	})
    54  }
    55  
    56  func TestList(t *testing.T) {
    57  	Convey("Open a tar.gz file and get list of file/dir name", t, func() {
    58  		z, err := Open("testdata/test.tar.gz")
    59  		So(err, ShouldBeNil)
    60  
    61  		Convey("List without prefix", func() {
    62  			So(com.CompareSliceStrU(z.List(),
    63  				strings.Split("dir/ dir/bar dir/empty/ hello readonly", " ")), ShouldBeTrue)
    64  		})
    65  
    66  		Convey("List with prefix", func() {
    67  			So(strings.Join(z.List("h"), " "), ShouldEqual, "hello")
    68  		})
    69  	})
    70  }
    71  
    72  func TestAddEmptyDir(t *testing.T) {
    73  	Convey("Open a tar.gz file and add empty dirs", t, func() {
    74  		z, err := Create(path.Join(os.TempDir(), "testdata/TestAddEmptyDir.tar.gz"))
    75  		So(err, ShouldBeNil)
    76  
    77  		Convey("Add dir that does not exist and then add again", func() {
    78  			So(z.AddEmptyDir("level1"), ShouldBeTrue)
    79  			So(!z.AddEmptyDir("level1"), ShouldBeTrue)
    80  		})
    81  
    82  		Convey("Add multiple-level dir", func() {
    83  			z.AddEmptyDir("level1/level2/level3/level4")
    84  			So(strings.Join(z.List(), " "), ShouldEqual,
    85  				"level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/")
    86  		})
    87  	})
    88  }
    89  
    90  func TestAddDir(t *testing.T) {
    91  	Convey("Open a tar.gz file and add dir with files", t, func() {
    92  		z, err := Create(path.Join(os.TempDir(), "testdata/TestAddDir.tar.gz"))
    93  		So(err, ShouldBeNil)
    94  
    95  		Convey("Add a dir that does exist", func() {
    96  			So(z.AddDir("testdata/testdir", "testdata/testdir"), ShouldBeNil)
    97  			So(strings.Join(z.List(), " "), ShouldEqual,
    98  				"testdata/ testdata/testdir/ testdata/testdir/gophercolor16x16.png"+
    99  					" testdata/testdir/level1/ testdata/testdir/level1/README.txt")
   100  		})
   101  
   102  		Convey("Add a dir that does not exist", func() {
   103  			So(z.AddDir("testdata/testdir", "testdata/testdir404"), ShouldNotBeNil)
   104  		})
   105  
   106  		Convey("Add a dir that is a file", func() {
   107  			So(z.AddDir("testdata/testdir", "testdata/README.txt"), ShouldNotBeNil)
   108  		})
   109  	})
   110  }
   111  
   112  func TestAddFile(t *testing.T) {
   113  	Convey("Open a tar.gz file and add files", t, func() {
   114  		z, err := Create(path.Join(os.TempDir(), "testdata/TestAddFile.tar.gz"))
   115  		So(err, ShouldBeNil)
   116  
   117  		Convey("Add a file that does exist", func() {
   118  			So(z.AddFile("testdata/README.txt", "testdata/gophercolor16x16.png"), ShouldBeNil)
   119  			So(strings.Join(z.List(), " "), ShouldEqual, "testdata/ testdata/README.txt")
   120  		})
   121  
   122  		Convey("Add a file that does not exist", func() {
   123  			So(z.AddFile("testdata/README.txt", "testdata/README_404.txt"), ShouldNotBeNil)
   124  		})
   125  
   126  		Convey("Add a file that is exist in list", func() {
   127  			So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil)
   128  			So(strings.Join(z.List(), " "), ShouldEqual, "testdata/ testdata/README.txt")
   129  		})
   130  	})
   131  }
   132  
   133  func TestExtractTo(t *testing.T) {
   134  	Convey("Extract a tar.gz file to given path", t, func() {
   135  		z, err := Open("testdata/test.tar.gz")
   136  		So(err, ShouldBeNil)
   137  
   138  		Convey("Extract the tar.gz file without entries", func() {
   139  			os.RemoveAll(path.Join(os.TempDir(), "testdata/test1"))
   140  			So(z.ExtractTo(path.Join(os.TempDir(), "testdata/test1")), ShouldBeNil)
   141  			list, err := com.StatDir(path.Join(os.TempDir(), "testdata/test1"), true)
   142  			So(err, ShouldBeNil)
   143  			So(com.CompareSliceStrU(list,
   144  				strings.Split("dir/ dir/bar dir/empty/ hello readonly", " ")), ShouldBeTrue)
   145  		})
   146  
   147  		Convey("Extract the tar.gz file with entries", func() {
   148  			os.RemoveAll(path.Join(os.TempDir(), "testdata/test2"))
   149  			So(z.ExtractTo(
   150  				path.Join(os.TempDir(), "testdata/test2"),
   151  				"dir/", "dir/bar", "readonly"), ShouldBeNil)
   152  			list, err := com.StatDir(path.Join(os.TempDir(), "testdata/test2"), true)
   153  			So(err, ShouldBeNil)
   154  			So(com.CompareSliceStrU(list,
   155  				strings.Split("dir/ dir/bar readonly", " ")), ShouldBeTrue)
   156  		})
   157  	})
   158  }
   159  
   160  func TestFlush(t *testing.T) {
   161  	Convey("Do some operations and flush to file system", t, func() {
   162  		z, err := Create(path.Join(os.TempDir(), "testdata/TestFlush.tar.gz"))
   163  		So(err, ShouldBeNil)
   164  
   165  		z.AddEmptyDir("level1/level2/level3/level4")
   166  		So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil)
   167  
   168  		// Add symbolic links.
   169  		So(z.AddFile("testdata/test.lnk", "testdata/test.lnk"), ShouldBeNil)
   170  		So(z.AddFile("testdata/testdir.lnk", "testdata/testdir.lnk"), ShouldBeNil)
   171  
   172  		fmt.Println("Flushing to local file system...")
   173  		So(z.Flush(), ShouldBeNil)
   174  	})
   175  
   176  	Convey("Do some operation and flush to io.Writer", t, func() {
   177  		f, err := os.Create(path.Join(os.TempDir(), "testdata/TestFlush2.tar.gz"))
   178  		So(err, ShouldBeNil)
   179  		So(f, ShouldNotBeNil)
   180  
   181  		z := New(f)
   182  		z.AddEmptyDir("level1/level2/level3/level4")
   183  		So(z.AddFile("testdata/README.txt", "testdata/README.txt"), ShouldBeNil)
   184  
   185  		fmt.Println("Flushing to local io.Writer...")
   186  		So(z.Flush(), ShouldBeNil)
   187  		So(z.Flush(), ShouldBeNil)
   188  	})
   189  }
   190  
   191  func TestPackTo(t *testing.T) {
   192  	Convey("Pack a dir or file to tar.gz file", t, func() {
   193  		Convey("Pack a dir that does exist and includir root dir", func() {
   194  			So(PackTo("testdata/testdir",
   195  				path.Join(os.TempDir(), "testdata/testdir1.tar.gz"), true), ShouldBeNil)
   196  		})
   197  
   198  		Convey("Pack a dir that does exist and does not includir root dir", func() {
   199  			So(PackTo("testdata/testdir",
   200  				path.Join(os.TempDir(), "testdata/testdir2.tar.gz")), ShouldBeNil)
   201  		})
   202  
   203  		Convey("Pack a dir that does not exist and does not includir root dir", func() {
   204  			So(PackTo("testdata/testdir404",
   205  				path.Join(os.TempDir(), "testdata/testdir3.tar.gz")), ShouldNotBeNil)
   206  		})
   207  
   208  		Convey("Pack a file that does exist", func() {
   209  			So(PackTo("testdata/README.txt",
   210  				path.Join(os.TempDir(), "testdata/testdir4.tar.gz")), ShouldBeNil)
   211  		})
   212  
   213  		Convey("Pack a file that does not exist", func() {
   214  			So(PackTo("testdata/README404.txt",
   215  				path.Join(os.TempDir(), "testdata/testdir5.tar.gz")), ShouldNotBeNil)
   216  		})
   217  	})
   218  }
   219  
   220  func TestClose(t *testing.T) {
   221  	Convey("Close the tar.gz file currently operating", t, func() {
   222  		z, err := Open("testdata/test.tar.gz")
   223  		So(err, ShouldBeNil)
   224  		So(z.Close(), ShouldBeNil)
   225  	})
   226  }
   227  
   228  func TestDeleteIndex(t *testing.T) {
   229  	Convey("Delete an entry with given index", t, func() {
   230  		z, err := Create(path.Join(os.TempDir(), "testdata/TestDeleteIndex.tar.gz"))
   231  		So(err, ShouldBeNil)
   232  
   233  		z.AddEmptyDir("level1/level2/level3/level4")
   234  
   235  		Convey("Delete an entry with valid index", func() {
   236  			So(z.DeleteIndex(3), ShouldBeNil)
   237  			So(strings.Join(z.List(), " "), ShouldEqual,
   238  				"level1/ level1/level2/ level1/level2/level3/")
   239  		})
   240  
   241  		Convey("Delete an entry with invalid index", func() {
   242  			So(z.DeleteIndex(5), ShouldNotBeNil)
   243  		})
   244  
   245  		Convey("Test after flush", func() {
   246  			So(z.Flush(), ShouldBeNil)
   247  			So(strings.Join(z.List(), " "), ShouldEqual,
   248  				"level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/")
   249  		})
   250  	})
   251  }
   252  
   253  func TestDeleteName(t *testing.T) {
   254  	Convey("Delete an entry with given name", t, func() {
   255  		z, err := Create(path.Join(os.TempDir(), "testdata/TestDeleteName.tar.gz"))
   256  		So(err, ShouldBeNil)
   257  
   258  		z.AddEmptyDir("level1/level2/level3/level4")
   259  
   260  		Convey("Delete an entry with valid name", func() {
   261  			So(z.DeleteName("level1/level2/level3/level4/"), ShouldBeNil)
   262  			So(strings.Join(z.List(), " "), ShouldEqual,
   263  				"level1/ level1/level2/ level1/level2/level3/")
   264  		})
   265  
   266  		Convey("Delete an entry with invalid name", func() {
   267  			So(z.DeleteName("level1/level2/level3/level"), ShouldNotBeNil)
   268  		})
   269  
   270  		Convey("Test after flush", func() {
   271  			So(z.Flush(), ShouldBeNil)
   272  			So(strings.Join(z.List(), " "), ShouldEqual,
   273  				"level1/ level1/level2/ level1/level2/level3/ level1/level2/level3/level4/")
   274  		})
   275  	})
   276  }