Skip to content

fix: create file before put content to it #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/content.en/docs/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Information about release notes of INFINI Framework is provided here.
- Add new stats api to quickly find the top N keys from a Badger DB (#67)
- Proactively restore dead node's availability (#72)

### Bug fix
- Fixed client sync config panic when config folder not exits (#71)

### Improvements

- Add util to http handler, support to parse bool parameter
Expand Down
6 changes: 5 additions & 1 deletion modules/configs/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func SaveConfigStr(name, content string) error {
if err != nil {
return err
}

fileToSave := path.Join(cfgDir, name)

log.Info("write config file: ", fileToSave)
Expand All @@ -138,6 +137,11 @@ func SaveConfigStr(name, content string) error {
}
}

err = os.MkdirAll(cfgDir, 0755)
if err != nil {
return err
}

_, err = util.FilePutContent(fileToSave, content)
if err != nil {
return err
Expand Down
37 changes: 37 additions & 0 deletions modules/configs/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ package config
import (
"fmt"
"github.com/magiconair/properties/assert"
"infini.sh/framework/core/global"
"infini.sh/framework/core/util"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -40,3 +44,36 @@ func TestVersion(t *testing.T) {
ver = parseConfigVersion("what's the version, i think is 1234")
assert.Equal(t, ver, int64(-1))
}

func TestSaveConfigStr(t *testing.T) {
cfgDir, err := filepath.Abs(global.Env().GetConfigDir())
if err != nil {
t.Fatalf("Failed to get config directory: %v", err)
}

testFileName := "test_config.yaml"
testContent := "test content"

err = SaveConfigStr(testFileName, testContent)
if err != nil {
t.Fatalf("SaveConfigStr failed: %v", err)
}

savedFilePath := filepath.Join(cfgDir, testFileName)
if !util.FileExists(savedFilePath) {
t.Fatalf("Expected file %s to exist", savedFilePath)
}

savedContent, err := util.FileGetContent(savedFilePath)
if err != nil {
t.Fatalf("Failed to read saved file content: %v", err)
}

assert.Equal(t, string(savedContent), testContent)

// Clean up
err = os.Remove(savedFilePath)
if err != nil {
t.Fatalf("Failed to remove test file: %v", err)
}
}
Loading