Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix diff output compared with nil #635

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading