Skip to content

Commit

Permalink
Add tests for YAMLStringify
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Aug 7, 2024
1 parent e4ffc6b commit cb0f397
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
4 changes: 2 additions & 2 deletions cfg/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestOctalTypeInConfigStringify(t *testing.T) {
},
}

str, err := util.Stringify(&c)
str, err := util.JSONStringify(&c)

Check failure on line 31 in cfg/types_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)

if assert.NoError(t, err) {
assert.Equal(t, "file-system:\n dir-mode: \"755\"\n", str)
Expand All @@ -37,7 +37,7 @@ func TestOctalTypeInConfigStringify(t *testing.T) {
func TestOctalStringify(t *testing.T) {
o := Octal(0765)

str, err := util.Stringify(&o)
str, err := util.JSONStringify(&o)

Check failure on line 40 in cfg/types_test.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)

if assert.NoError(t, err) {
assert.Equal(t, "\"765\"\n", str)
Expand Down
6 changes: 3 additions & 3 deletions cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,22 @@ func getUserAgent(appName string, config string) string {
}

func logNewConfiguration(newConfig *cfg.Config) {
cfgStr, err := util.Stringify(newConfig)
cfgStr, err := util.YAMLStringify(newConfig)
if err != nil {
logger.Warnf("failed to stringify configuration: %v", err)
return
}
logger.Infof("GCSFuse mount config: %s", cfgStr)
}
func logLegacyConfiguration(flags *flagStorage, mountConfig *config.MountConfig) {
flagsStringified, err := util.Stringify(*flags)
flagsStringified, err := util.JSONStringify(*flags)

Check failure on line 101 in cmd/legacy_main.go

View workflow job for this annotation

GitHub Actions / Lint

SA1019: util.JSONStringify is deprecated: use YAMLStringify (staticcheck)
if err != nil {
logger.Warnf("failed to stringify cli flags: %v", err)
} else {
logger.Infof("GCSFuse mount command flags: %s", flagsStringified)
}

mountConfigStringified, err := util.Stringify(*mountConfig)
mountConfigStringified, err := util.JSONStringify(*mountConfig)
if err != nil {
logger.Warnf("failed to stringify config-file: %v", err)
} else {
Expand Down
20 changes: 17 additions & 3 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"path/filepath"
"strings"
"time"

"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -75,12 +77,24 @@ func GetResolvedPath(filePath string) (resolvedPath string, err error) {
}
}

// Stringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
func Stringify(input any) (string, error) {
// JSONStringify marshals an object (only exported attribute) to a JSON string. If marshalling fails, it returns an empty string.
//
// Deprecated: use YAMLStringify
func JSONStringify(input any) (string, error) {
inputBytes, err := json.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in Stringify %w", err)
return "", fmt.Errorf("error in JSONStringify %w", err)
}
return string(inputBytes), nil
}

// YAMLStringify marshals an object to a YAML string. If marshalling fails, it returns an error.
func YAMLStringify(input any) (string, error) {
inputBytes, err := yaml.Marshal(input)

if err != nil {
return "", fmt.Errorf("error in YAMLStringify %w", err)
}
return string(inputBytes), nil
}
Expand Down
41 changes: 39 additions & 2 deletions internal/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,43 @@ func (ts *UtilTest) ResolveWhenParentProcDirEnvSetAndAbsoluteFilePath() {
assert.Equal(ts.T(), "/var/dir/test.txt", resolvedPath)
}

func TestYAMLStringify(t *testing.T) {
tests := []struct {
name string
obj any
expected string
}{
{
name: "Test Map",
obj: map[string]int{
"1": 1,
"2": 2,
"3": 3,
},
expected: "\"1\": 1\n\"2\": 2\n\"3\": 3\n",
},
{
name: "Test Nested Map",
obj: map[string]any{
"1": 1,
"2": map[string]int{
"3": 1,
},
},
expected: "\"1\": 1\n\"2\":\n \"3\": 1\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
s, err := YAMLStringify(tc.obj)

Check failure on line 181 in internal/util/util_test.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
if assert.NoError(t, err) {
assert.Equal(t, tc.expected, s)
}
})
}
}
func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMarshalledString() {
sampleMap := map[string]int{
"1": 1,
Expand All @@ -163,7 +200,7 @@ func (ts *UtilTest) TestStringifyShouldReturnAllFieldsPassedInCustomObjectAsMars
NestedValue: sampleNestedValue,
}

actual, _ := Stringify(customObject)
actual, _ := JSONStringify(customObject)

expected := "{\"Value\":\"test_value\",\"NestedValue\":{\"SomeField\":10,\"SomeOther\":{\"1\":1,\"2\":2,\"3\":3}}}"
assert.Equal(ts.T(), expected, actual)
Expand All @@ -174,7 +211,7 @@ func (ts *UtilTest) TestStringifyShouldReturnEmptyStringWhenMarshalErrorsOut() {
value: "example",
}

actual, _ := Stringify(customInstance)
actual, _ := JSONStringify(customInstance)

expected := ""
assert.Equal(ts.T(), expected, actual)
Expand Down

0 comments on commit cb0f397

Please sign in to comment.