File: uncompress_test.go

package info (click to toggle)
golang-github-rhysd-go-github-selfupdate 1.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: sh: 21; makefile: 6
file content (133 lines) | stat: -rw-r--r-- 3,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package selfupdate

import (
	"bytes"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestCompressionNotRequired(t *testing.T) {
	buf := []byte{'a', 'b', 'c'}
	want := bytes.NewReader(buf)
	r, err := UncompressCommand(want, "https://github.com/foo/bar/releases/download/v1.2.3/foo", "foo")
	if err != nil {
		t.Fatal(err)
	}
	have, err := ioutil.ReadAll(r)
	if err != nil {
		t.Fatal(err)
	}
	for i, b := range have {
		if buf[i] != b {
			t.Error(i, "th elem is not the same as wanted. want", buf[i], "but got", b)
		}
	}
}

func getArchiveFileExt(file string) string {
	if strings.HasSuffix(file, ".tar.gz") {
		return ".tar.gz"
	}
	if strings.HasSuffix(file, ".tar.xz") {
		return ".tar.xz"
	}
	return filepath.Ext(file)
}

func TestUncompress(t *testing.T) {
	for _, n := range []string{
		"testdata/foo.zip",
		"testdata/single-file.zip",
		"testdata/single-file.gz",
		"testdata/single-file.gzip",
		"testdata/foo.tar.gz",
		"testdata/foo.tgz",
		"testdata/foo.tar.xz",
		"testdata/single-file.xz",
	} {
		t.Run(n, func(t *testing.T) {
			f, err := os.Open(n)
			if err != nil {
				t.Fatal(err)
			}

			ext := getArchiveFileExt(n)
			url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
			r, err := UncompressCommand(f, url, "bar")
			if err != nil {
				t.Fatal(err)
			}

			bytes, err := ioutil.ReadAll(r)
			if err != nil {
				t.Fatal(err)
			}
			s := string(bytes)
			if s != "this is test\n" {
				t.Fatal("Uncompressing zip failed into unexpected content", s)
			}
		})
	}
}

func TestUncompressInvalidArchive(t *testing.T) {
	for _, a := range []struct {
		name string
		msg  string
	}{
		{"testdata/invalid.zip", "not a valid zip file"},
		{"testdata/invalid.gz", "Failed to uncompress gzip file"},
		{"testdata/invalid-tar.tar.gz", "Failed to unarchive .tar file"},
		{"testdata/invalid-gzip.tar.gz", "Failed to uncompress .tar.gz file"},
		{"testdata/invalid.xz", "Failed to uncompress xzip file"},
		{"testdata/invalid-tar.tar.xz", "Failed to unarchive .tar file"},
		{"testdata/invalid-xz.tar.xz", "Failed to uncompress .tar.xz file"},
	} {
		f, err := os.Open(a.name)
		if err != nil {
			t.Fatal(err)
		}

		ext := getArchiveFileExt(a.name)
		url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
		_, err = UncompressCommand(f, url, "bar")
		if err == nil {
			t.Fatal("Error should be raised")
		}
		if !strings.Contains(err.Error(), a.msg) {
			t.Fatal("Unexpected error:", err)
		}
	}
}

func TestTargetNotFound(t *testing.T) {
	for _, tc := range []struct {
		name string
		msg  string
	}{
		{"testdata/empty.zip", "command is not found"},
		{"testdata/bar-not-found.zip", "command is not found"},
		{"testdata/bar-not-found.gzip", "does not match to command"},
		{"testdata/empty.tar.gz", "command is not found"},
		{"testdata/bar-not-found.tar.gz", "command is not found"},
	} {
		t.Run(tc.name, func(t *testing.T) {
			f, err := os.Open(tc.name)
			if err != nil {
				t.Fatal(err)
			}
			ext := getArchiveFileExt(tc.name)
			url := "https://github.com/foo/bar/releases/download/v1.2.3/bar" + ext
			_, err = UncompressCommand(f, url, "bar")
			if err == nil {
				t.Fatal("Error should be raised for")
			}
			if !strings.Contains(err.Error(), tc.msg) {
				t.Fatal("Unexpected error:", err)
			}
		})
	}
}