Skip to content

Commit

Permalink
fix: test (#45)
Browse files Browse the repository at this point in the history
* fix: test

* fix: test
  • Loading branch information
hwbrzzl authored Dec 31, 2024
1 parent 0d2216c commit 0475734
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
18 changes: 15 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ on:
branches:
- master
pull_request:
env:
TENCENT_ACCESS_KEY_ID: ${{ secrets.TENCENT_ACCESS_KEY_ID }}
TENCENT_ACCESS_KEY_SECRET: ${{ secrets.TENCENT_ACCESS_KEY_SECRET }}
TENCENT_URL: ${{ secrets.TENCENT_URL }}
jobs:
test:
uses: goravel/.github/.github/workflows/test.yml@master
secrets: inherit
ubuntu:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: 'stable'
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test -timeout 1h ./...
34 changes: 32 additions & 2 deletions cos.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ func (r *Cos) Directories(path string) ([]string, error) {
return nil, err
}
for _, commonPrefix := range v.CommonPrefixes {
directories = append(directories, strings.ReplaceAll(commonPrefix, validPath, ""))
directory := strings.ReplaceAll(commonPrefix, validPath, "")
if directory != "" {
directories = append(directories, directory)
}
}
isTruncated = v.IsTruncated
marker = v.NextMarker
Expand Down Expand Up @@ -240,7 +243,10 @@ func (r *Cos) Files(path string) ([]string, error) {
return nil, err
}
for _, content := range v.Contents {
files = append(files, strings.ReplaceAll(content.Key, validPath, ""))
file := strings.ReplaceAll(content.Key, validPath, "")
if file != "" {
files = append(files, file)
}
}
isTruncated = v.IsTruncated
marker = v.NextMarker
Expand Down Expand Up @@ -332,6 +338,18 @@ func (r *Cos) Path(file string) string {
}

func (r *Cos) Put(file string, content string) error {
// If the file is created in a folder directly, we can't check if the folder exists.
// So we need to create the top folder first.
if !strings.HasSuffix(file, "/") {
index := strings.Index(file, "/")
if index != -1 {
folder := file[:index+1]
if err := r.MakeDirectory(folder); err != nil {
return err
}
}
}

tempFile, err := r.tempFile(content)
defer os.Remove(tempFile.Name())
if err != nil {
Expand All @@ -355,6 +373,18 @@ func (r *Cos) PutFileAs(filePath string, source filesystem.File, name string) (s
return "", err
}

// If the file is created in a folder directly, we can't check if the folder exists.
// So we need to create the top folder first.
if !strings.HasSuffix(fullPath, "/") {
index := strings.Index(fullPath, "/")
if index != -1 {
folder := fullPath[:index+1]
if err := r.MakeDirectory(folder); err != nil {
return "", err
}
}
}

if _, _, err := r.instance.Object.Upload(
r.ctx, fullPath, source.File(), nil,
); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

func TestStorage(t *testing.T) {
if os.Getenv("TENCENT_ACCESS_KEY_ID") == "" {
color.Redln("No filesystem tests run, please add cos configuration: TENCENT_ACCESS_KEY_ID= TENCENT_ACCESS_KEY_SECRET= TENCENT_BUCKET= TENCENT_URL= go test ./...")
color.Redln("No filesystem tests run, please add cos configuration: TENCENT_ACCESS_KEY_ID= TENCENT_ACCESS_KEY_SECRET= TENCENT_URL= go test ./...")
return
}

Expand Down Expand Up @@ -269,7 +269,7 @@ func TestStorage(t *testing.T) {
setup: func() {
assert.Nil(t, driver.Put("Put/1.txt", "Goravel"))
assert.True(t, driver.Exists("Put/1.txt"))
assert.True(t, driver.Exists("Put"))
assert.True(t, driver.Exists("Put/"))
assert.True(t, driver.Missing("Put/2.txt"))
assert.Nil(t, driver.DeleteDirectory("Put"))
},
Expand All @@ -290,7 +290,7 @@ func TestStorage(t *testing.T) {
fileInfo := &File{path: "test.txt"}
path, err := driver.PutFile("PutFile", fileInfo)
assert.Nil(t, err)
assert.True(t, driver.Exists("PutFile"))
assert.True(t, driver.Exists("PutFile/"))
assert.True(t, driver.Exists(path))
data, err := driver.Get(path)
assert.Nil(t, err)
Expand Down

0 comments on commit 0475734

Please sign in to comment.