Skip to content

Commit

Permalink
Merge pull request #169 from multiversx/save-toml-file
Browse files Browse the repository at this point in the history
Added save toml file func
  • Loading branch information
ssd04 authored Apr 28, 2023
2 parents 9c03b1f + 30177d5 commit 79fc6ec
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
14 changes: 14 additions & 0 deletions core/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func LoadTomlFile(dest interface{}, relativePath string) error {
return toml.NewDecoder(f).Decode(dest)
}

// SaveTomlFile will open and save data to toml file
func SaveTomlFile(src interface{}, relativePath string) error {
f, err := os.Create(relativePath)
if err != nil {
return err
}

defer func() {
_ = f.Close()
}()

return toml.NewEncoder(f).Encode(src)
}

// LoadTomlFileToMap opens and decodes a toml file as a map[string]interface{}
func LoadTomlFileToMap(relativePath string) (map[string]interface{}, error) {
f, err := OpenFile(relativePath)
Expand Down
61 changes: 58 additions & 3 deletions core/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type TestStruct struct {
a, b int
A, B int
}

func TestOpenFile_NoExistingFileShouldErr(t *testing.T) {
Expand Down Expand Up @@ -69,6 +70,61 @@ func TestLoadTomlFile_FileExitsShouldPass(t *testing.T) {
assert.Nil(t, err)
}

func TestSaveTomlFile(t *testing.T) {
t.Parallel()

t.Run("empty filename, should fail", func(t *testing.T) {
t.Parallel()

cfg := &TestStruct{}

fileName := ""

err := core.SaveTomlFile(cfg, fileName)
require.Error(t, err)
})

t.Run("invalid path, should fail", func(t *testing.T) {
t.Parallel()

cfg := &TestStruct{}

fileName := "invalid_path" + "/testFile1"

err := core.SaveTomlFile(cfg, fileName)
require.Error(t, err)
})

t.Run("should work with valid dir", func(t *testing.T) {
t.Parallel()

cfg := &TestStruct{}

dir := t.TempDir()
fileName := dir + "/testFile1"

err := core.SaveTomlFile(cfg, fileName)
require.Nil(t, err)
})

t.Run("should work", func(t *testing.T) {
t.Parallel()

cfg := &TestStruct{A: 10, B: 20}
dir := t.TempDir()
fileName := dir + "/testFile1"

err := core.SaveTomlFile(cfg, fileName)
require.Nil(t, err)

newCfg := &TestStruct{}
err = core.LoadTomlFile(newCfg, fileName)
require.Nil(t, err)

require.Equal(t, cfg, newCfg)
})
}

func TestLoadJSonFile_NoExistingFileShouldErr(t *testing.T) {
t.Parallel()

Expand All @@ -77,7 +133,6 @@ func TestLoadJSonFile_NoExistingFileShouldErr(t *testing.T) {
err := core.LoadJsonFile(cfg, "file")

assert.Error(t, err)

}

func TestLoadJSonFile_FileExitsShouldPass(t *testing.T) {
Expand All @@ -89,7 +144,7 @@ func TestLoadJSonFile_FileExitsShouldPass(t *testing.T) {
file, err := os.Create(fileName)
assert.Nil(t, err)

data, _ := json.MarshalIndent(TestStruct{a: 0, b: 0}, "", " ")
data, _ := json.MarshalIndent(TestStruct{A: 0, B: 0}, "", " ")

_ = ioutil.WriteFile(fileName, data, 0644)

Expand Down

0 comments on commit 79fc6ec

Please sign in to comment.