Skip to content

Commit

Permalink
Merge pull request #635 from kayac/fix/diff-output-with-null
Browse files Browse the repository at this point in the history
fix diff output compared with nil
  • Loading branch information
fujiwara authored Dec 8, 2023
2 parents b2c2982 + 019c334 commit 21305e1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
18 changes: 14 additions & 4 deletions diff.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ecspresso

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -30,6 +31,7 @@ func (d *App) Diff(ctx context.Context, opt DiffOption) error {
var remoteTaskDefArn string
// diff for services only when service defined
if d.config.Service != "" {
d.Log("[DEGUG] diff service compare with %s", d.config.Service)
newSv, err := d.LoadServiceDefinition(d.config.ServiceDefinitionPath)
if err != nil {
return fmt.Errorf("failed to load service definition: %w", err)
Expand Down Expand Up @@ -70,6 +72,7 @@ func (d *App) Diff(ctx context.Context, opt DiffOption) error {
}
var remoteTd *ecs.RegisterTaskDefinitionInput
if remoteTaskDefArn != "" {
d.Log("[DEGUG] diff task definition compare with %s", remoteTaskDefArn)
remoteTd, err = d.DescribeTaskDefinition(ctx, remoteTaskDefArn)
if err != nil {
return err
Expand Down Expand Up @@ -112,8 +115,8 @@ func diffServices(local, remote *Service, localPath string, unified bool) (strin
return "", fmt.Errorf("failed to marshal remote service definition: %w", err)
}

remoteSv := string(remoteSvBytes)
newSv := string(newSvBytes)
remoteSv := toDiffString(remoteSvBytes)
newSv := toDiffString(newSvBytes)

if unified {
edits := myers.ComputeEdits(span.URIFromPath(remoteArn), remoteSv, newSv)
Expand Down Expand Up @@ -141,8 +144,8 @@ func diffTaskDefs(local, remote *TaskDefinitionInput, localPath, remoteArn strin
return "", fmt.Errorf("failed to marshal remote task definition: %w", err)
}

remoteTd := string(remoteTdBytes)
newTd := string(newTdBytes)
remoteTd := toDiffString(remoteTdBytes)
newTd := toDiffString(newTdBytes)

if unified {
edits := myers.ComputeEdits(span.URIFromPath(remoteArn), remoteTd, newTd)
Expand Down Expand Up @@ -341,3 +344,10 @@ func toNumberMemory(memory string) *string {
}
return &memory
}

func toDiffString(b []byte) string {
if bytes.Equal(b, []byte("null")) || bytes.Equal(b, []byte("null\n")) {
return ""
}
return string(b)
}
19 changes: 19 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ecspresso_test

import (
"strings"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -267,6 +268,15 @@ func TestDiffServices(t *testing.T) {
if diff == "" {
t.Errorf("unexpected diff: %s", diff)
}
minusDiffs := 0
for _, line := range strings.Split(diff, "\n") {
if strings.HasPrefix(line, "-") {
minusDiffs++
}
}
if minusDiffs != 1 { // The first line is "---"
t.Errorf("unexpected diff. has many minus diffs: %s", diff)
}
})
}

Expand Down Expand Up @@ -297,5 +307,14 @@ func TestDiffTaskDefs(t *testing.T) {
if diff == "" {
t.Errorf("unexpected diff: %s", diff)
}
minusDiffs := 0
for _, line := range strings.Split(diff, "\n") {
if strings.HasPrefix(line, "-") {
minusDiffs++
}
}
if minusDiffs != 1 { // The first line is "---"
t.Errorf("unexpected diff. has many minus diffs: %s", diff)
}
})
}
2 changes: 1 addition & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func (d *App) OutputJSONForAPI(w io.Writer, v interface{}) error {
b, err := MarshalJSONForAPI(v)
if err != nil {
fmt.Errorf("failed to marshal json: %w", err)
return fmt.Errorf("failed to marshal json: %w", err)
}
_, err = w.Write(b)
return err
Expand Down

0 comments on commit 21305e1

Please sign in to comment.