-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers_test.go
More file actions
152 lines (117 loc) · 4.02 KB
/
Copy pathhelpers_test.go
File metadata and controls
152 lines (117 loc) · 4.02 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
142
143
144
145
146
147
148
149
150
151
152
package githosts
import (
"bytes"
"compress/gzip"
"context"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMaskSecretsReplacesSecretsWithAsterisks(t *testing.T) {
content := "Hello, my secret is secret123"
secrets := []string{"secret123"}
maskedContent := maskSecrets(content, secrets)
assert.Equal(t, "Hello, my secret is *****", maskedContent)
}
func TestMaskSecretsHandlesMultipleSecrets(t *testing.T) {
content := "Hello, my secrets are secret123 and secret456"
secrets := []string{"secret123", "secret456"}
maskedContent := maskSecrets(content, secrets)
assert.Equal(t, "Hello, my secrets are ***** and *****", maskedContent)
}
func TestMaskSecretsReturnsOriginalContentWhenNoSecrets(t *testing.T) {
content := "Hello, I have no secrets"
secrets := []string{}
maskedContent := maskSecrets(content, secrets)
assert.Equal(t, content, maskedContent)
}
func TestMaskSecretsDoesNotAlterContentWithoutSecrets(t *testing.T) {
content := "Hello, my secret is not here"
secrets := []string{"secret123"}
maskedContent := maskSecrets(content, secrets)
assert.Equal(t, content, maskedContent)
}
func TestCreateDirIfAbsent(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "new", "sub")
err := createDirIfAbsent(path)
assert.NoError(t, err)
info, statErr := os.Stat(path)
assert.NoError(t, statErr)
assert.True(t, info.IsDir())
}
func TestStripTrailing(t *testing.T) {
assert.Equal(t, "hello", stripTrailing("hello\n", "\n"))
assert.Equal(t, "world", stripTrailing("world", "\n"))
}
func TestURLHelpers(t *testing.T) {
u := urlWithToken("https://example.com/repo.git", "tok")
assert.Equal(t, "https://tok@example.com/repo.git", u)
u = urlWithToken("noscheme", "tok")
assert.Equal(t, "noscheme", u)
u = urlWithBasicAuthURL("https://example.com/repo.git", "u", "p")
assert.Equal(t, "https://u:p@example.com/repo.git", u)
u = urlWithBasicAuthURL("noscheme", "u", "p")
assert.Equal(t, "noscheme", u)
}
func TestTimeStampToTime(t *testing.T) {
ts := getTimestamp()
parsed, err := timeStampToTime(ts)
assert.NoError(t, err)
assert.NotZero(t, parsed)
_, err = timeStampToTime("bad")
assert.Error(t, err)
}
func TestParseCountObjectsOutput(t *testing.T) {
loose, packed, err := parseCountObjectsOutput("count: 0\nin-pack: 0\n")
assert.NoError(t, err)
assert.False(t, loose)
assert.False(t, packed)
_, _, err = parseCountObjectsOutput("count: 1\n")
assert.Error(t, err)
}
func TestIsEmpty(t *testing.T) {
repo := t.TempDir()
cmd := exec.Command("git", "init", repo)
assert.NoError(t, cmd.Run())
empty, err := isEmpty(context.Background(), repo)
assert.NoError(t, err)
assert.True(t, empty)
f := filepath.Join(repo, "file.txt")
assert.NoError(t, os.WriteFile(f, []byte("content"), 0o600))
assert.NoError(t, exec.Command("git", "-C", repo, "add", "file.txt").Run())
assert.NoError(t, exec.Command("git", "-C", repo, "-c", "user.email=a@b", "-c", "user.name=n", "commit", "-m", "c").Run())
empty, err = isEmpty(context.Background(), repo)
assert.NoError(t, err)
assert.False(t, empty)
}
func TestGetResponseBody(t *testing.T) {
b := bytes.NewBufferString("hello")
resp := &http.Response{Body: io.NopCloser(b), Header: http.Header{}}
out, err := getResponseBody(resp)
assert.NoError(t, err)
assert.Equal(t, "hello", string(out))
var gzBuf bytes.Buffer
gz := gzip.NewWriter(&gzBuf)
_, _ = gz.Write([]byte("hello"))
gz.Close()
resp = &http.Response{Body: io.NopCloser(&gzBuf), Header: http.Header{"Content-Encoding": []string{"gzip"}}}
out, err = getResponseBody(resp)
assert.NoError(t, err)
assert.Equal(t, "hello", string(out))
}
func TestGetBundleRefs(t *testing.T) {
refs, err := getBundleRefs(context.Background(), "testfiles/example-bundles/example.20221102202522.bundle")
assert.NoError(t, err)
assert.Equal(t, "2c84a508078d81eae0246ae3f3097befd0bcb7dc", refs["refs/heads/master"])
}
func TestRemoveNotFound(t *testing.T) {
s := []string{"a", "b", "c"}
out := remove(s, "z")
require.Equal(t, s, out)
}