-
Notifications
You must be signed in to change notification settings - Fork 36
/
archivex_test.go
110 lines (92 loc) · 3.4 KB
/
archivex_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
//////////////////////////////////////////
// archivex_test.go
// Jhonathan Paulo Banczek - 2014
// jpbanczek@gmail.com - jhoonb.com
//////////////////////////////////////////
package archivex
import (
"fmt"
"os"
"path"
"reflect"
"strings"
"testing"
)
type archTest struct {
addPath string
include bool
name string
filePath string
addString string
addFileName string
}
type archTypeTest struct {
tests []archTest
arch Archivex
}
func Test_archivex(t *testing.T) {
dir, _ := os.Getwd()
// let's clean up the previous results, to be sure that we're not reading from an old result.
if err := os.RemoveAll(path.Join(dir, "/testresults")); err != nil && !os.IsNotExist(err) {
t.Fatalf("cannot clean up test results directory: %v", err)
}
if err := os.Mkdir(path.Join(dir, "/testresults"), 0777); err != nil && !os.IsExist(err) {
t.Fatalf("cannot make test results directory: %v", err)
}
// All the different tests we want to run with different combinations of input paths and the includeCurrentFolder flag
tests := []archTest{
// absolute path
archTest{dir + "/testfolder/", true, "absTrailInclude", dir + "/LICENSE", "string", "filename"},
archTest{dir + "/testfolder/", false, "absTrailExclude", dir + "/LICENSE", "string", "filename"},
// relative path
archTest{"testfolder/", true, "relTrailInclude", "LICENSE", "string", "filename"},
archTest{"testfolder/", false, "relTrailExclude", "LICENSE", "string", "filename"},
// without trailing slashes
archTest{dir + "/testfolder", true, "absInclude", dir + "/LICENSE", "string", "filename"},
archTest{dir + "/testfolder", false, "absExclude", dir + "/LICENSE", "string", "filename"},
archTest{"testfolder", true, "relInclude", "LICENSE", "string", "filename"},
archTest{"testfolder", false, "relExclude", "LICENSE", "string", "filename"},
}
// We want to execute the batch of tests on both Zip and Tar
typeTests := []archTypeTest{
archTypeTest{tests, &ZipFile{}},
archTypeTest{tests, &TarFile{}},
}
// Run all tests
for _, typeTest := range typeTests {
currentType := reflect.TypeOf(typeTest.arch)
t.Logf("Running tests for archive type: %s", currentType.Elem())
for i, test := range typeTest.tests {
t.Logf("Running %s...", test.name)
// Create the archive
filename := fmt.Sprintf("%d_%s_test", i+1, test.name)
arch := reflect.ValueOf(typeTest.arch).Interface().(Archivex)
if err := arch.Create(path.Join("testresults", filename)); err != nil {
t.Fatalf("Error creating '%s': %v", filename, err)
}
// Add the files to the archive
if err := arch.AddAll(test.addPath, test.include); err != nil {
t.Fatalf("Error doing AddAll with '%s' and includeCurrentFolder = %v: %v", test.addPath, test.include, err)
}
// Add a file to the archive
file, err := os.Open(test.filePath)
if err != nil {
t.Fatalf("Error opening file '%s': %v", test.filePath, err)
}
info, err := file.Stat()
if err != nil {
t.Fatalf("Error statting file '%s': %v", test.filePath, err)
}
if err := arch.Add(test.filePath, file, info); err != nil {
t.Fatalf("Error doing AddFile with '%s': %v", test.filePath, err)
}
//}
// Add a file to the archive
if err := arch.Add(test.addFileName, strings.NewReader(test.addString), nil); err != nil {
t.Fatalf("Error doing Add with '%s', '%s': %v", test.addString, test.addFileName, err)
}
// Close the archive
arch.Close()
}
}
}