Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
Signed-off-by: Faeka Ansari <[email protected]>
  • Loading branch information
fykaa committed Dec 19, 2024
1 parent 1e99c62 commit 8b92db0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
12 changes: 8 additions & 4 deletions internal/directives/json_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ func (j *jsonUpdater) updateFile(workDir string, path string, changes map[string
}

for key, value := range changes {
switch value.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64,
string, bool:
default:
return fmt.Errorf("value for key %q is not a scalar type", key)
}
updatedContent, setErr := sjson.Set(string(fileContent), key, value)
if setErr != nil {
return fmt.Errorf("error setting key %q in JSON file: %w", key, setErr)
Expand Down Expand Up @@ -137,10 +145,6 @@ func (j *jsonUpdater) generateCommitMessage(path string, updates map[string]any)
switch v := value.(type) {
case string:
_, _ = commitMsg.WriteString(fmt.Sprintf("\n- %s: %q", key, v))
case bool:
_, _ = commitMsg.WriteString(fmt.Sprintf("\n- %s: \"%v\"", key, v))
case int, float64:
_, _ = commitMsg.WriteString(fmt.Sprintf("\n- %s: %v", key, v))
default:
_, _ = commitMsg.WriteString(fmt.Sprintf("\n- %s: %v", key, v))
}
Expand Down
35 changes: 34 additions & 1 deletion internal/directives/json_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ func Test_jsonUpdater_updateValuesFile(t *testing.T) {
assert.JSONEq(t, `{"key": "value"}`, string(content))
},
},
{
name: "preserve formatting after update",
valuesContent: `{
"key": "value",
"nested": {
"key1": "value1",
"key2": "value2"
}
}`,
changes: map[string]any{"key": "newvalue"},
assertions: func(t *testing.T, valuesFilePath string, err error) {
require.NoError(t, err)

require.FileExists(t, valuesFilePath)
content, err := os.ReadFile(valuesFilePath)
require.NoError(t, err)

updatedContent := `{
"key": "newvalue",
"nested": {
"key1": "value1",
"key2": "value2"
}
}`

assert.JSONEq(t, updatedContent, string(content))

var result map[string]any
err = json.Unmarshal(content, &result)
require.NoError(t, err)
assert.Equal(t, "newvalue", result["key"])
},
},
}

runner := &jsonUpdater{}
Expand Down Expand Up @@ -271,7 +304,7 @@ func Test_jsonUpdater_runPromotionStep(t *testing.T) {
Output: map[string]any{
"commitMessage": "Updated config.json\n\n" +
"- app.version: \"1.0.1\"\n" +
"- features.newFeature: \"true\"\n" +
"- features.newFeature: true\n" +
"- threshold: 100",
},
}, result)
Expand Down

0 comments on commit 8b92db0

Please sign in to comment.