Skip to content

Commit

Permalink
Make the output of diff and push the same when no changes
Browse files Browse the repository at this point in the history
  • Loading branch information
VMitov committed Feb 1, 2017
1 parent 21f66fd commit cdc430f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
9 changes: 6 additions & 3 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ func diffRun(tmpl, format, key string, sourcesList []map[string]interface{}, sto
return err
}

fmt.Println(strChanges(changes, s, pretty))
fmt.Println(strChanges(changes, key, s, pretty))
return nil
}

func strChanges(cs changes, s storage, pretty bool) string {
func strChanges(cs changes, key string, s storage, pretty bool) string {
if cs.Len() == 0 {
return fmt.Sprintf("There are no changes")
if key != "" {
return fmt.Sprintf("No changes for key %v", key)
}
return fmt.Sprintf("No changes")
}
return s.Diff(cs, pretty)
}
39 changes: 36 additions & 3 deletions cmd/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ func TestDiffRun(t *testing.T) {
testCases := []struct {
storage string
tmpl string
key string
sources []map[string]interface{}
output string
}{
{
`key: oldval`,
`key: {{.placeholder}}`,
"",
[]map[string]interface{}{
{
"type": "config",
Expand All @@ -34,6 +36,7 @@ func TestDiffRun(t *testing.T) {
{
`key: val`,
`key: {{.placeholder}}`,
"",
[]map[string]interface{}{
{
"type": "config",
Expand All @@ -42,7 +45,37 @@ func TestDiffRun(t *testing.T) {
},
},
},
"There are no changes\n",
"No changes\n",
},
{
`key: oldval`,
`key: {{.placeholder}}`,
"key",
[]map[string]interface{}{
{
"type": "config",
"vals": map[string]string{
"placeholder": "val",
},
},
},
"" +
"-key: oldval\n" +
"+key: val\n",
},
{
`key: val`,
`key: {{.placeholder}}`,
"key",
[]map[string]interface{}{
{
"type": "config",
"vals": map[string]string{
"placeholder": "val",
},
},
},
"No changes for key key\n",
},
}

Expand All @@ -63,7 +96,7 @@ func TestDiffRun(t *testing.T) {
defer os.Remove(configf.Name())

out := caspertest.GetStdout(t, func() {
err = diffRun(tmpf.Name(), "yaml", "", tc.sources, "file", map[string]interface{}{"path": configf.Name()}, false)
err = diffRun(tmpf.Name(), "yaml", tc.key, tc.sources, "file", map[string]interface{}{"path": configf.Name()}, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -109,7 +142,7 @@ func TestDiff(t *testing.T) {
},
},
},
"There are no changes\n",
"No changes\n",
},
}

Expand Down
5 changes: 1 addition & 4 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,11 @@ func pushRun(tmpl, format, key string, sourcesList []map[string]interface{}, sto
return err
}

fmt.Println(strChanges(changes, key, s, pretty))
if changes.Len() == 0 {
fmt.Println("No changes")
return nil
}

fmt.Println("Changes:")
fmt.Println(strChanges(changes, s, pretty))

if !force {
// prompt for agreement
fmt.Print("Continue[y/N]: ")
Expand Down
3 changes: 0 additions & 3 deletions cmd/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func TestPushRun(t *testing.T) {
},
true,
"" +
"Changes:\n" +
"-\n" +
"+key: val\n" +
"Applying changes...\n",
Expand All @@ -49,7 +48,6 @@ func TestPushRun(t *testing.T) {
},
false,
"" +
"Changes:\n" +
"-\n" +
"+key: val\n" +
"Continue[y/N]: Canceled\n",
Expand Down Expand Up @@ -120,7 +118,6 @@ func TestPush(t *testing.T) {
},
},
"" +
"Changes:\n" +
"-\n" +
"+key: val\n" +
"Applying changes...\n",
Expand Down

0 comments on commit cdc430f

Please sign in to comment.