github.com/abemedia/appcast@v0.4.0/integrations/apt/compress_test.go (about)

     1  package apt_test
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  
     8  	"github.com/abemedia/appcast/integrations/apt"
     9  	"github.com/google/go-cmp/cmp"
    10  )
    11  
    12  func TestCompress(t *testing.T) {
    13  	want := []byte("test")
    14  	exts := []string{".gz", ".bz2", ".xz", ".lzma", ".lz4", ".zst", ""}
    15  
    16  	for _, ext := range exts {
    17  		buf := &bytes.Buffer{}
    18  
    19  		w, err := apt.Compress(ext)(buf)
    20  		if err != nil {
    21  			t.Errorf("compress %s: %s", ext, err)
    22  			continue
    23  		}
    24  		if _, err = w.Write(want); err != nil {
    25  			t.Errorf("write %s: %s", ext, err)
    26  			continue
    27  		}
    28  		if err = w.Close(); err != nil {
    29  			t.Errorf("close %s: %s", ext, err)
    30  			continue
    31  		}
    32  
    33  		isCompressed := ext != ""
    34  		if isCompressed == bytes.Equal(want, buf.Bytes()) {
    35  			t.Errorf("%s: should be compressed: %t", ext, isCompressed)
    36  			continue
    37  		}
    38  
    39  		r, err := apt.Decompress(ext)(buf)
    40  		if err != nil {
    41  			t.Errorf("decompress %s: %s", ext, err)
    42  			continue
    43  		}
    44  		got, err := io.ReadAll(r)
    45  		if err != nil {
    46  			t.Errorf("read %s: %s", ext, err)
    47  			continue
    48  		}
    49  
    50  		if err = r.Close(); err != nil {
    51  			t.Errorf("close %s: %s", ext, err)
    52  			continue
    53  		}
    54  
    55  		if !bytes.Equal(want, got) {
    56  			t.Errorf("%s: should be decompressed", ext)
    57  		}
    58  	}
    59  }
    60  
    61  func TestCompessionExtensions(t *testing.T) {
    62  	tests := []struct {
    63  		in   apt.CompressionAlgo
    64  		want []string
    65  	}{
    66  		{0, []string{"", ".xz", ".gz"}}, // defaults
    67  		{apt.NoCompression, []string{""}},
    68  		{apt.GZIP, []string{"", ".gz"}},
    69  		{apt.BZIP2, []string{"", ".bz2"}},
    70  		{apt.XZ, []string{"", ".xz"}},
    71  		{apt.LZMA, []string{"", ".lzma"}},
    72  		{apt.LZ4, []string{"", ".lz4"}},
    73  		{apt.ZSTD, []string{"", ".zst"}},
    74  		{apt.BZIP2 | apt.ZSTD | apt.XZ, []string{"", ".bz2", ".xz", ".zst"}},
    75  	}
    76  
    77  	for _, test := range tests {
    78  		got := apt.CompressionExtensions(test.in)
    79  		if diff := cmp.Diff(test.want, got); diff != "" {
    80  			t.Error(diff)
    81  		}
    82  	}
    83  }