github.com/jenkins-x/jx/v2@v2.1.155/pkg/util/unzip_test.go (about)

     1  // +build unit
     2  
     3  package util
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"strings"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestUnzip(t *testing.T) {
    17  	t.Parallel()
    18  	dest, err := ioutil.TempDir("", "testUnzip")
    19  	require.NoError(t, err, "[TEST SETUP] failed to create temporary directory for tests")
    20  	defer os.RemoveAll(dest)
    21  
    22  	zipFile, err := filepath.Abs("")
    23  	require.NoError(t, err, "[TEST SETUP] could not obtain CWD")
    24  	zipFile = filepath.Join(zipFile, "test_data", "2_files.zip")
    25  
    26  	err = Unzip(zipFile, dest)
    27  	require.NoError(t, err, "failed to unzip archive")
    28  	files, err := ioutil.ReadDir(dest)
    29  	require.NoError(t, err, "failed to list files")
    30  
    31  	assert.Len(t, files, 2)
    32  	// files are sorted by ReadDir
    33  	assert.Equal(t, "file1.txt", files[0].Name())
    34  	assert.Equal(t, "file2.txt", files[1].Name())
    35  	for _, f := range files {
    36  		assertFileContents(t, filepath.Join(dest, f.Name()), strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
    37  	}
    38  }
    39  
    40  func TestUnzipSpecificFilesHappyPath(t *testing.T) {
    41  	t.Parallel()
    42  	dest, err := ioutil.TempDir("", "testUnzip")
    43  	require.NoError(t, err, "[TEST SETUP] failed to create temporary directory for tests")
    44  	defer os.RemoveAll(dest)
    45  
    46  	zipFile, err := filepath.Abs("")
    47  	require.NoError(t, err, "[TEST SETUP] could not obtain CWD")
    48  	zipFile = filepath.Join(zipFile, "test_data", "2_files.zip")
    49  
    50  	err = UnzipSpecificFiles(zipFile, dest, "file2.txt")
    51  	require.NoError(t, err, "failed to unzip archive")
    52  	files, err := ioutil.ReadDir(dest)
    53  	require.NoError(t, err, "failed to list files")
    54  
    55  	assert.Len(t, files, 1)
    56  	// files are sorted by ReadDir
    57  	require.Equal(t, "file2.txt", files[0].Name())
    58  	assertFileContents(t, filepath.Join(dest, "file2.txt"), "file2")
    59  }
    60  
    61  func TestUnzipSpecificFilesMIssingFile(t *testing.T) {
    62  	t.Parallel()
    63  	dest, err := ioutil.TempDir("", "testUnzip")
    64  	require.NoError(t, err, "[TEST SETUP] failed to create temporary directory for tests")
    65  	defer os.RemoveAll(dest)
    66  
    67  	zipFile, err := filepath.Abs("")
    68  	require.NoError(t, err, "[TEST SETUP] could not obtain CWD")
    69  	zipFile = filepath.Join(zipFile, "test_data", "2_files.zip")
    70  
    71  	err = UnzipSpecificFiles(zipFile, dest, "file2.txt", "file3.txt")
    72  	assert.Error(t, err, "unzip should fail as file3.txt is not in the zip")
    73  }
    74  
    75  func assertFileContents(t *testing.T, file, expected string) {
    76  	bytes, err := ioutil.ReadFile(file)
    77  	if assert.NoError(t, err, "could not read contents of %s", file) {
    78  		assert.Equal(t, expected, string(bytes), "file contents for %s did not match", file)
    79  	}
    80  }