-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
77 lines (63 loc) · 2.01 KB
/
util_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
package storage
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type UtilTestSuite struct {
suite.Suite
}
func (s *UtilTestSuite) SetupTest() {
omitArg := func(argName string, args []string) []string {
for i := range args {
splitArgs := strings.Split(args[i], "=")
if len(splitArgs) != 2 {
continue
}
name := splitArgs[0]
if name == argName {
return append(args[:i], args[i+1:]...)
}
}
return args
}
os.Args = omitArg("--gcs_bucket", os.Args)
}
func (s *UtilTestSuite) TestParsePathWithGSPathSyntax() {
bucket, key, err := parsePath("gs://bucket-34ff28a4-b823-4c05-8367-8d760927938b/test_file.pdf")
if assert.NoError(s.T(), err) {
assert.Equal(s.T(), bucket, "bucket-34ff28a4-b823-4c05-8367-8d760927938b")
assert.Equal(s.T(), key, "test_file.pdf")
}
}
func (s *UtilTestSuite) TestParsePathWithBucketAndKey() {
bucket, key, err := parsePath("bucket-34ff28a4-b823-4c05-8367-8d760927938b/test_file.pdf")
if assert.NoError(s.T(), err) {
assert.Equal(s.T(), bucket, "bucket-34ff28a4-b823-4c05-8367-8d760927938b")
assert.Equal(s.T(), key, "test_file.pdf")
}
}
func (s *UtilTestSuite) TestParsePathWithNestedBucketAndKey() {
bucket, key, err := parsePath("bucket-34ff28a4-b823-4c05-8367-8d760927938b/certificates/2024/test_file.pdf")
if assert.NoError(s.T(), err) {
assert.Equal(s.T(), bucket, "bucket-34ff28a4-b823-4c05-8367-8d760927938b")
assert.Equal(s.T(), key, "certificates/2024/test_file.pdf")
}
}
func (s *UtilTestSuite) TestParsePathThrowsErrorForInvalidPaths() {
_, _, err := parsePath("test_file.pdf")
assert.EqualError(s.T(), err, ErrInvalidPath.Error())
}
func (s *UtilTestSuite) TestParsePathAppendsGCSBucketIfSet() {
os.Args = append(os.Args, "--gcs_bucket=my-bucket")
bucket, path, err := parsePath("test_file.pdf")
if assert.NoError(s.T(), err) {
assert.Equal(s.T(), bucket, "my-bucket")
assert.Equal(s.T(), path, "test_file.pdf")
}
}
func TestUtils(t *testing.T) {
suite.Run(t, new(UtilTestSuite))
}