-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathcompress_test.go
More file actions
141 lines (119 loc) · 3.73 KB
/
Copy pathcompress_test.go
File metadata and controls
141 lines (119 loc) · 3.73 KB
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
package compress
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/crc-org/crc/v2/pkg/extract"
"github.com/stretchr/testify/require"
)
const testArchiveName = "testdata.tar.zst"
var (
files fileMap = map[string]string{
"a": "a",
"c": "c",
filepath.Join("b", "d"): "d",
}
)
func testCompress(t *testing.T, baseDir string) {
// This is useful to check that the top-level directory of the archive
// was created with the correct permissions, and was not created by the
// `os.MkdirAll(0750)` call at the beginning of `untarFile`
require.NoError(t, os.Chmod(baseDir, 0755))
require.NoError(t, Compress(baseDir, testArchiveName))
defer os.Remove(testArchiveName)
destDir := t.TempDir()
fileList, err := extract.Uncompress(testArchiveName, destDir)
require.NoError(t, err)
_, d := filepath.Split(baseDir)
fi, err := os.Stat(filepath.Join(destDir, d))
require.NoError(t, err)
fMode := os.FileMode(0755)
if runtime.GOOS == "windows" {
// https://golang.org/pkg/os/#Chmod
fMode = os.FileMode(0777)
}
require.Equal(t, fi.Mode().Perm(), fMode)
require.NoError(t, checkFileList(filepath.Join(destDir, d), fileList, files))
require.NoError(t, checkFiles(filepath.Join(destDir, d), files))
}
func TestCompressRelative(t *testing.T) {
// Test with relative path
testCompress(t, "testdata")
// Test with absolute path
currentDir, err := os.Getwd()
require.NoError(t, err)
testCompress(t, filepath.Join(currentDir, "testdata"))
}
/* The code below is duplicated from pkg/extract/extract_test.go */
type fileMap map[string]string
func copyFileMap(orig fileMap) fileMap {
copiedMap := fileMap{}
for key, value := range orig {
copiedMap[key] = value
}
return copiedMap
}
// This checks that the list of files returned by Uncompress matches what we expect
func checkFileList(destDir string, extractedFiles []string, expectedFiles fileMap) error {
// We are going to remove elements from the map, but we don't want to modify the map used by the caller
expectedFiles = copyFileMap(expectedFiles)
for _, file := range extractedFiles {
rel, err := filepath.Rel(destDir, file)
if err != nil {
return err
}
_, found := expectedFiles[rel]
if !found {
return fmt.Errorf("Unexpected file '%s' in file list %v", rel, expectedFiles)
}
delete(expectedFiles, rel)
}
if len(expectedFiles) != 0 {
return fmt.Errorf("Some expected files were not in file list: %v", expectedFiles)
}
return nil
}
// This checks that the files in the destination directory matches what we expect
func checkFiles(destDir string, files fileMap) error {
// We are going to remove elements from the map, but we don't want to modify the map used by the caller
files = copyFileMap(files)
err := filepath.Walk(destDir, func(path string, info os.FileInfo, err error) error {
logging.Debugf("Walking %s", path)
if err != nil {
return err
}
if info.IsDir() {
logging.Debugf("Skipping directory %s", path)
return nil
}
logging.Debugf("Checking file %s", path)
archivePath, err := filepath.Rel(destDir, path)
if err != nil {
return err
}
expectedContent, found := files[archivePath]
if !found {
return fmt.Errorf("Unexpected extracted file '%s'", path)
}
delete(files, archivePath)
data, err := os.ReadFile(path) // #nosec G304
if err != nil {
return err
}
if string(data) != expectedContent {
return fmt.Errorf("Unexpected content for '%s': expected [%s], got [%s]", path, expectedContent, string(data))
}
logging.Debugf("'%s' successfully checked", path)
return nil
})
if err != nil {
return err
}
if len(files) != 0 {
return fmt.Errorf("Some expected files were not extracted: %v", files)
}
return nil
}