-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprobe_test.go
145 lines (129 loc) · 2.91 KB
/
probe_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
package boar
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/itchio/headway/state"
"github.com/itchio/httpkit/eos"
"github.com/stretchr/testify/assert"
)
type StrategyTest struct {
fileName string
result Strategy
}
var (
strategyTests = []StrategyTest{
{"foo_bar.zip", StrategyZip},
{"foo_bar.tar", StrategyTar},
{"foo_bar.tar.gz", StrategyTarGz},
{"foo_bar.tar.bz2", StrategyTarBz2},
{"foo_bar.7z", StrategySevenZip},
{"foo_bar.rar", StrategyRar},
{"foo_bar.dmg", StrategyDmg},
{"foo_bar.exe", StrategySevenZipUnsure},
{"foo_bar", StrategySevenZipUnsure},
}
)
func TestGetStrategy(t *testing.T) {
consumer := &state.Consumer{}
for _, cas := range strategyTests {
ff := fakeFile{
fileName: cas.fileName,
canStat: true,
}
strat := getStrategy(getExt(ff, consumer))
assert.Equal(t, cas.result, strat)
}
}
func TestGetStrategyNoStat(t *testing.T) {
// Only one test case here
ff := fakeFile{}
strat := getStrategy(getExt(ff, &state.Consumer{}))
assert.Equal(t, StrategySevenZipUnsure, strat)
}
type fakeFile struct {
fileName string
canStat bool
}
func (ff fakeFile) Read([]byte) (int, error) {
return 0, errors.New("Fake files can't read")
}
func (ff fakeFile) Close() error {
return errors.New("Fake files can't close")
}
func (ff fakeFile) ReadAt([]byte, int64) (int, error) {
return 0, errors.New("Fake files can't read")
}
func (ff fakeFile) Seek(int64, int) (int64, error) {
return 0, errors.New("Fake files can't seek")
}
func (ff fakeFile) Stat() (os.FileInfo, error) {
if ff.canStat {
return fakeFileInfo{name: ff.fileName}, nil
}
return fakeFileInfo{}, errors.New("This fake file can't Stat()")
}
type fakeFileInfo struct {
name string
}
func (ffi fakeFileInfo) Name() string {
return ffi.name
}
func (ffi fakeFileInfo) Size() int64 {
return 0
}
func (ffi fakeFileInfo) IsDir() bool {
return false
}
func (ffi fakeFileInfo) ModTime() time.Time {
return time.Time{}
}
func (ffi fakeFileInfo) Mode() os.FileMode {
return 0
}
func (ffi fakeFileInfo) Sys() interface{} {
return nil
}
func Test_RealFiles(t *testing.T) {
files, err := ioutil.ReadDir("testdata")
if err != nil {
panic(err)
}
consumer := &state.Consumer{}
for _, f := range files {
t.Run(f.Name(), func(t *testing.T) {
file, err := eos.Open(filepath.Join("testdata", f.Name()))
if err != nil {
panic(err)
}
defer file.Close()
ai, err := Probe(ProbeParams{
File: file,
Consumer: consumer,
})
if err != nil {
panic(err)
}
tokens := strings.Split(f.Name(), "-")
lastToken := tokens[len(tokens)-1]
expectedType := lastToken
switch lastToken {
case "dmg":
expectedType = "hfs"
case "rar4":
expectedType = "rar"
case "gz":
expectedType = "tar.gz"
case "bz2":
expectedType = "tar.bz2"
case "xz":
expectedType = "tar.xz"
}
assert.EqualValues(t, expectedType, ai.Format)
})
}
}