Skip to content

Commit 342e889

Browse files
author
matm
committedOct 8, 2013
More UT
1 parent b7acded commit 342e889

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed
 

‎cloudinary/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (c *Config) handleEnvVars() error {
6363
if err != nil {
6464
return err
6565
}
66-
c.PrependPath = ensureTrailingSlash(ptag)
66+
c.PrependPath = cloudinary.EnsureTrailingSlash(ptag)
6767
}
6868
}
6969

@@ -103,7 +103,7 @@ func LoadConfig(path string) (*Config, error) {
103103

104104
// An optional remote prepend path
105105
if prepend, err := c.String("cloudinary", "prepend"); err == nil {
106-
settings.PrependPath = ensureTrailingSlash(prepend)
106+
settings.PrependPath = cloudinary.EnsureTrailingSlash(prepend)
107107
}
108108
settings.ProdTag, _ = c.String("global", "prodtag")
109109

‎cloudinary/vars.go

-9
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,3 @@ func handleQuery(uri *url.URL) (*url.URL, error) {
4242
}
4343
return wuri, nil
4444
}
45-
46-
// ensureTrailingSlash adds any missing trailing / at the end
47-
// of a directory.
48-
func ensureTrailingSlash(dirname string) string {
49-
if !strings.HasSuffix(dirname, "/") {
50-
dirname += "/"
51-
}
52-
return dirname
53-
}

‎service.go

+16
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (s *Service) DefaultUploadURI() *url.URL {
216216
// new/css/default
217217
func cleanAssetName(path, basePath, prependPath string) string {
218218
var name string
219+
path, basePath, prependPath = strings.TrimSpace(path), strings.TrimSpace(basePath), strings.TrimSpace(prependPath)
219220
basePath, err := filepath.Abs(basePath)
220221
if err != nil {
221222
basePath = ""
@@ -237,9 +238,24 @@ func cleanAssetName(path, basePath, prependPath string) string {
237238
name = name[1:]
238239
}
239240
}
241+
if prependPath != "" {
242+
if prependPath[0] == os.PathSeparator {
243+
prependPath = prependPath[1:]
244+
}
245+
prependPath = EnsureTrailingSlash(prependPath)
246+
}
240247
return prependPath + name[:len(name)-len(filepath.Ext(name))]
241248
}
242249

250+
// EnsureTrailingSlash adds a missing trailing / at the end
251+
// of a directory name.
252+
func EnsureTrailingSlash(dirname string) string {
253+
if !strings.HasSuffix(dirname, "/") {
254+
dirname += "/"
255+
}
256+
return dirname
257+
}
258+
243259
func (s *Service) walkIt(path string, info os.FileInfo, err error) error {
244260
if info.IsDir() {
245261
return nil

‎service_test.go

+26-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestDial(t *testing.T) {
1616

1717
// Not a cloudinary:// URL scheme
1818
if _, err := Dial("http://localhost"); err == nil {
19-
t.Error("should fail if URL scheme different of cloudinary://")
19+
t.Error("should fail if URL scheme different from cloudinary://")
2020
}
2121

2222
// Missing API secret (password)?
@@ -83,5 +83,30 @@ func TestUseDatabase(t *testing.T) {
8383
if err := s.UseDatabase("baduri::"); err == nil {
8484
t.Error("should fail on bad uri")
8585
}
86+
// Bad scheme
87+
if err := s.UseDatabase("http://localhost"); err == nil {
88+
t.Error("should fail if URL scheme different from mongodb://")
89+
}
90+
if err := s.UseDatabase("mongodb://localhost/cloudinary"); err != nil {
91+
t.Error("please ensure you have a running MongoDB server on localhost")
92+
}
93+
if s.dbSession == nil || s.col == nil {
94+
t.Error("service's dbSession and col should not be nil")
95+
}
96+
}
8697

98+
func TestCleanAssetName(t *testing.T) {
99+
assets := [][4]string{
100+
// order: path, basepath, prepend, expected result
101+
{"/tmp/css/default.css", "/tmp/", "new", "new/css/default"},
102+
{"/a/b/c.png", "/a", "", "b/c"},
103+
{"/a/b/c.png", "/a ", " ", "b/c"}, // With spaces
104+
{"/a/b/c.png", "", "/x", "x/a/b/c"},
105+
}
106+
for _, p := range assets {
107+
c := cleanAssetName(p[0], p[1], p[2])
108+
if c != p[3] {
109+
t.Errorf("wrong cleaned name. Expect '%s', got '%s'", p[3], c)
110+
}
111+
}
87112
}

0 commit comments

Comments
 (0)
Please sign in to comment.