-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
111 lines (92 loc) · 2.89 KB
/
utils_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
package main
import (
"testing"
"strings"
"os"
"fmt"
"path/filepath"
"encoding/json"
)
func dumpRequest(request_type, request_string string) (string, error) {
handle, err := os.CreateTemp("", "request-" + request_type + "-")
if err != nil {
return "", fmt.Errorf("failed to create temp file; %w", err)
}
_, err = handle.WriteString(request_string)
if err != nil {
return "", fmt.Errorf("failed to write string; %w", err)
}
reqname := handle.Name()
err = handle.Close()
if err != nil {
return "", fmt.Errorf("failed to close file; %w", err)
}
return reqname, nil
}
func verifyFileContents(path, contents string) error {
observed, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read %q; %w", path, err)
}
if string(observed) != contents {
return fmt.Errorf("unexpected contents of %q; %w", path, err)
}
return nil
}
func constructMockRegistry() (string, error) {
reg, err := os.MkdirTemp("", "")
if err != nil {
return "", fmt.Errorf("failed to create the registry; %w", err)
}
err = os.Mkdir(filepath.Join(reg, logDirName), 0755)
if err != nil {
return "", fmt.Errorf("failed to create log subdirectory; %w", err)
}
return reg, nil
}
type logEntry struct {
Type string `json:"type"`
Project *string `json:"project"`
Asset *string `json:"asset"`
Version *string `json:"version"`
Latest *bool `json:"latest"`
}
func readAllLogs(registry string) ([]logEntry, error) {
logdir := filepath.Join(registry, logDirName)
logs, err := os.ReadDir(logdir)
if err != nil {
return nil, fmt.Errorf("failed to list the log directory contents; %w", err)
}
output := make([]logEntry, len(logs))
for i, l := range logs {
logpath := filepath.Join(logdir, l.Name())
content_raw, err := os.ReadFile(logpath)
if err != nil {
return nil, fmt.Errorf("failed to read the log at %q; %w", logpath, err)
}
err = json.Unmarshal(content_raw, &(output[i]))
if err != nil {
return nil, fmt.Errorf("failed to parse the log at %q; %w", logpath, err)
}
}
return output, nil
}
func TestIsBadName(t *testing.T) {
var err error
err = isBadName("..foo")
if err == nil || !strings.Contains(err.Error(), "..") {
t.Fatal("failed to stop on '..'")
}
err = isBadName("")
if err == nil || !strings.Contains(err.Error(), "empty") {
t.Fatal("failed to stop on an empty name")
}
err = isBadName("asda/a")
if err == nil || !strings.Contains(err.Error(), "/") {
t.Fatal("failed to stop in the presence of a forward slash")
}
err = isBadName("asda\\asdasd")
if err == nil || !strings.Contains(err.Error(), "\\") {
t.Fatal("failed to stop in the presence of a backslash")
}
}