github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/uzip/uzip_test.go (about) 1 // Copyright 2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package uzip 6 7 import ( 8 "github.com/stretchr/testify/require" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 ) 14 15 func TestFromZip(t *testing.T) { 16 tmpDir, err := ioutil.TempDir("", "ziptest") 17 if err != nil { 18 t.Fatal(err) 19 } 20 defer os.RemoveAll(tmpDir) 21 22 f := filepath.Join(tmpDir, "test.zip") 23 err = ToZip("testdata/testFolder", f) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 z, err := ioutil.ReadFile(f) 29 if err != nil { 30 t.Fatal(err) 31 } 32 require.NotEmpty(t, z) 33 34 out := filepath.Join(tmpDir, "unziped") 35 err = os.MkdirAll(out, os.ModePerm) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 err = FromZip(f, out) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 f1 := filepath.Join(out, "file1") 46 f2 := filepath.Join(out, "file2") 47 f3 := filepath.Join(out, "subFolder", "file3") 48 f4 := filepath.Join(out, "subFolder", "file4") 49 50 f1Expected, err := ioutil.ReadFile("testdata/testFolder/file1") 51 if err != nil { 52 t.Fatal(err) 53 } 54 f2Expected, err := ioutil.ReadFile("testdata/testFolder/file2") 55 if err != nil { 56 t.Fatal(err) 57 } 58 f3Expected, err := ioutil.ReadFile("testdata/testFolder/subFolder/file3") 59 if err != nil { 60 t.Fatal(err) 61 } 62 f4Expected, err := ioutil.ReadFile("testdata/testFolder/subFolder/file4") 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 require.FileExists(t, f1) 68 require.FileExists(t, f2) 69 require.FileExists(t, f3) 70 require.FileExists(t, f4) 71 72 var x []byte 73 74 x, err = ioutil.ReadFile(f1) 75 require.NoError(t, err) 76 require.Equal(t, x, f1Expected) 77 78 x, err = ioutil.ReadFile(f2) 79 require.NoError(t, err) 80 require.Equal(t, x, f2Expected) 81 82 x, err = ioutil.ReadFile(f3) 83 require.NoError(t, err) 84 require.Equal(t, x, f3Expected) 85 86 x, err = ioutil.ReadFile(f4) 87 require.NoError(t, err) 88 require.Equal(t, x, f4Expected) 89 }