github.com/cdoern/storage@v1.12.13/pkg/archive/wrap_test.go (about)

     1  package archive
     2  
     3  import (
     4  	"archive/tar"
     5  	"bytes"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestGenerateEmptyFile(t *testing.T) {
    13  	archive, err := Generate("emptyFile")
    14  	require.NoError(t, err)
    15  	if archive == nil {
    16  		t.Fatal("The generated archive should not be nil.")
    17  	}
    18  
    19  	expectedFiles := [][]string{
    20  		{"emptyFile", ""},
    21  	}
    22  
    23  	tr := tar.NewReader(archive)
    24  	actualFiles := make([][]string, 0, 10)
    25  	i := 0
    26  	for {
    27  		hdr, err := tr.Next()
    28  		if err == io.EOF {
    29  			break
    30  		}
    31  		require.NoError(t, err)
    32  		buf := new(bytes.Buffer)
    33  		buf.ReadFrom(tr)
    34  		content := buf.String()
    35  		actualFiles = append(actualFiles, []string{hdr.Name, content})
    36  		i++
    37  	}
    38  	if len(actualFiles) != len(expectedFiles) {
    39  		t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles))
    40  	}
    41  	for i := 0; i < len(expectedFiles); i++ {
    42  		actual := actualFiles[i]
    43  		expected := expectedFiles[i]
    44  		if actual[0] != expected[0] {
    45  			t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0])
    46  		}
    47  		if actual[1] != expected[1] {
    48  			t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1])
    49  		}
    50  	}
    51  }
    52  
    53  func TestGenerateWithContent(t *testing.T) {
    54  	archive, err := Generate("file", "content")
    55  	require.NoError(t, err)
    56  	if archive == nil {
    57  		t.Fatal("The generated archive should not be nil.")
    58  	}
    59  
    60  	expectedFiles := [][]string{
    61  		{"file", "content"},
    62  	}
    63  
    64  	tr := tar.NewReader(archive)
    65  	actualFiles := make([][]string, 0, 10)
    66  	i := 0
    67  	for {
    68  		hdr, err := tr.Next()
    69  		if err == io.EOF {
    70  			break
    71  		}
    72  		require.NoError(t, err)
    73  		buf := new(bytes.Buffer)
    74  		buf.ReadFrom(tr)
    75  		content := buf.String()
    76  		actualFiles = append(actualFiles, []string{hdr.Name, content})
    77  		i++
    78  	}
    79  	if len(actualFiles) != len(expectedFiles) {
    80  		t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles))
    81  	}
    82  	for i := 0; i < len(expectedFiles); i++ {
    83  		actual := actualFiles[i]
    84  		expected := expectedFiles[i]
    85  		if actual[0] != expected[0] {
    86  			t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0])
    87  		}
    88  		if actual[1] != expected[1] {
    89  			t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1])
    90  		}
    91  	}
    92  }