Skip to content

Commit

Permalink
add a couple of tests for toml utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofel committed Jan 11, 2024
1 parent a4123a0 commit 9a4f0f2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/toml/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ func SaveStructAsToml(v any, dirName, name string) (string, error) {
}
}
filePath := fmt.Sprintf("%s/%s.toml", fileDir, name)
f, _ := toml.Marshal(v)
err = os.WriteFile(filePath, f, 0600)
f, err := toml.Marshal(v)
if err != nil {
return "", err
}

return filePath, err
return filePath, os.WriteFile(filePath, f, 0600)
}
89 changes: 89 additions & 0 deletions utils/toml/toml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package toml

import (
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-testing-framework/utils/ptr"
)

func TestItSavesStructToTomlAndReadsIt(t *testing.T) {
type someStruct struct {
Hello string `toml:"hello"`
Yolo map[string]string `toml:"yolo"`
Laika *string `toml:"laika"`
Mir *string `toml:"mir"`
}
testStruct := someStruct{
Hello: "World",
Yolo: map[string]string{
"yolo": "swag",
},
Mir: ptr.Ptr("Mir"),
}

t.Cleanup(func() {
os.Remove("test/test.toml")
os.Remove("test")
os.Remove("test.toml")
})

filePath, err := SaveStructAsToml(testStruct, "test", "test")
require.NoError(t, err, "failed to save struct as toml")

var readStruct someStruct
err = OpenTomlFileAsStruct(filePath, &readStruct)
require.NoError(t, err, "failed to read toml file")

require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct")

//save to current folder
filePath, err = SaveStructAsToml(testStruct, "", "test")
require.NoError(t, err, "failed to save struct as toml")

err = OpenTomlFileAsStruct(filePath, &readStruct)
require.NoError(t, err, "failed to read toml file")

require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct")
}

func TestItOverridesExistingFile(t *testing.T) {
type someStruct struct {
Hello string `toml:"hello"`
Yolo map[string]string `toml:"yolo"`
Laika *string `toml:"laika"`
Mir *string `toml:"mir"`
}
testStruct := someStruct{
Hello: "World",
Yolo: map[string]string{
"yolo": "swag",
},
Mir: ptr.Ptr("Mir"),
}

t.Cleanup(func() {
os.Remove("test.toml")
})

err := os.WriteFile("test.toml", []byte("mietek"), 0600)
require.NoError(t, err, "failed to create test file")

filePath, err := SaveStructAsToml(testStruct, "", "test")
require.NoError(t, err, "failed to save struct as toml")

var readStruct someStruct
err = OpenTomlFileAsStruct(filePath, &readStruct)
require.NoError(t, err, "failed to read toml file")

require.Equal(t, testStruct, readStruct, "read struct is not equal to original struct")
}

func TestItReturnsErrorWhenOpeningNonExistent(t *testing.T) {
var readStruct struct{}
err := OpenTomlFileAsStruct("nonexistent.toml", &readStruct)
require.Error(t, err, "should return error when opening nonexistent file")
require.Contains(t, err.Error(), "no such file or directory", "should return correct error when opening nonexistent file")
}

0 comments on commit 9a4f0f2

Please sign in to comment.