Skip to content

Commit f704e7c

Browse files
committed
Fix uploaded file URL bug
Add tests, travis file
1 parent 645962e commit f704e7c

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: go
2+
3+
go:
4+
- "1.10"
5+
6+
install:
7+
- curl https://glide.sh/get | sh
8+
- glide install
9+
10+
notifications:
11+
email: false

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ func HasPreviousUpload(svc *s3.S3, bucket string, filename string) bool {
118118
}
119119

120120
// GetFileURL returns the final url of the file
121-
func GetFileURL(filename string, bucket string) string {
121+
func GetFileURL(bucket string, bucketFilename string) string {
122122
// the static.buffer.com bucket has a domain alias
123123
if bucket == defaultS3Bucket {
124-
return "https://" + path.Join(bucket, filename)
124+
return "https://" + path.Join(bucket, bucketFilename)
125125
}
126-
return "https://s3.amazonaws.com" + path.Join("/", filename)
126+
return "https://s3.amazonaws.com" + path.Join("/", bucket, "/", bucketFilename)
127127
}
128128

129129
// UploadFile uploads a given file to the s3 bucket
@@ -153,6 +153,8 @@ func VersionAndUploadFiles(
153153
) (map[string]string, error) {
154154
fileVersions := map[string]string{}
155155

156+
fmt.Printf("Uploading to %s/%s\n", bucket, directory)
157+
156158
for _, filename := range filenames {
157159
file, err := os.Open(filename)
158160
if err != nil {
@@ -170,7 +172,7 @@ func VersionAndUploadFiles(
170172
uploadFilename = GetVersionedFilename(filename, checksum)
171173
}
172174
bucketFilename := path.Join(directory, uploadFilename)
173-
fileURL := GetFileURL(filename, bucket)
175+
fileURL := GetFileURL(bucket, bucketFilename)
174176

175177
shouldUpload := !HasPreviousUpload(svc, bucket, bucketFilename)
176178
if shouldUpload && !dryRun {
@@ -181,9 +183,9 @@ func VersionAndUploadFiles(
181183
}
182184

183185
if shouldUpload {
184-
fmt.Printf("%-10s %s\n", "Uploaded", fileURL)
186+
fmt.Printf("%-10s %s\n", "Uploaded", filename)
185187
} else {
186-
fmt.Printf("%-10s %s\n", "Skipped", fileURL)
188+
fmt.Printf("%-10s %s\n", "Skipped", filename)
187189
}
188190

189191
fileVersions[filename] = fileURL

main_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetFileURL(t *testing.T) {
8+
tests := []struct {
9+
bucket string
10+
bucketFilename string
11+
expected string
12+
}{
13+
{"some-bucket", "js/app.1234.js", "https://s3.amazonaws.com/some-bucket/js/app.1234.js"},
14+
{"static.buffer.com", "dir/js/app.1234.js", "https://static.buffer.com/dir/js/app.1234.js"},
15+
}
16+
17+
for _, test := range tests {
18+
actual := GetFileURL(test.bucket, test.bucketFilename)
19+
if actual != test.expected {
20+
t.Errorf("File URL was incorrect, got: %s, expected %s", actual, test.expected)
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)