Skip to content

Commit 57be9d7

Browse files
Merge pull request #354 from dropbox/feature/dry-run-put
Migrate put dry-run to renderOperation and unit-test the helper
2 parents 4201d38 + b2d07d1 commit 57be9d7

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

cmd/dry_run_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package cmd
1616

1717
import (
1818
"bytes"
19+
"encoding/json"
20+
"io"
1921
"testing"
2022

2123
"github.com/spf13/cobra"
@@ -99,6 +101,61 @@ func TestDryRunDisplayPath(t *testing.T) {
99101
}
100102
}
101103

104+
func TestRenderOperationTextUsesClosure(t *testing.T) {
105+
var stdout bytes.Buffer
106+
cmd := renderOperationTestCommand(&stdout, "text")
107+
108+
results := []jsonOperationResult{newJSONOperationResult(jsonStatusPlanned, "folder", nil, plannedMetadata("folder", "/Projects"))}
109+
err := renderOperation(cmd, nil, results, nil, func(w io.Writer) error {
110+
return writeDryRunLine(w, "create directory", "/Projects")
111+
})
112+
if err != nil {
113+
t.Fatalf("renderOperation error: %v", err)
114+
}
115+
116+
if got, want := stdout.String(), "Would create directory /Projects\n"; got != want {
117+
t.Fatalf("text output = %q, want %q", got, want)
118+
}
119+
}
120+
121+
func TestRenderOperationJSONRendersEnvelopeAndSkipsClosure(t *testing.T) {
122+
var stdout bytes.Buffer
123+
cmd := renderOperationTestCommand(&stdout, "json")
124+
125+
input := mkdirInput{Path: "/Projects", DryRun: true}
126+
results := []jsonOperationResult{newJSONOperationResult(jsonStatusPlanned, "folder", input, plannedMetadata("folder", "/Projects"))}
127+
warnings := []jsonWarning{{Code: jsonWarningCodeSkippedSymlink, Message: "skipped symlink", Path: "/link"}}
128+
129+
err := renderOperation(cmd, input, results, warnings, func(w io.Writer) error {
130+
t.Fatalf("text closure called in JSON mode")
131+
return nil
132+
})
133+
if err != nil {
134+
t.Fatalf("renderOperation error: %v", err)
135+
}
136+
137+
var got jsonOperationOutput
138+
if err := json.Unmarshal(stdout.Bytes(), &got); err != nil {
139+
t.Fatalf("decode output: %v (raw %s)", err, stdout.String())
140+
}
141+
if !got.OK || got.SchemaVersion != jsonSchemaVersion || got.Command != "render-op" {
142+
t.Fatalf("envelope = %+v, want ok/schema_version/command populated", got)
143+
}
144+
if len(got.Results) != 1 || got.Results[0].Status != jsonStatusPlanned || got.Results[0].Kind != "folder" {
145+
t.Fatalf("results = %+v, want one planned folder result", got.Results)
146+
}
147+
if len(got.Warnings) != 1 || got.Warnings[0].Code != jsonWarningCodeSkippedSymlink {
148+
t.Fatalf("warnings = %+v, want skipped_symlink passthrough", got.Warnings)
149+
}
150+
}
151+
152+
func renderOperationTestCommand(stdout *bytes.Buffer, format string) *cobra.Command {
153+
cmd := &cobra.Command{Use: "render-op"}
154+
cmd.SetOut(stdout)
155+
cmd.Flags().String(outputFlag, format, "")
156+
return cmd
157+
}
158+
102159
func TestPlannedMetadata(t *testing.T) {
103160
got := plannedMetadata("file", "/Reports/Old.PDF")
104161
if got.Type != "file" || got.PathDisplay != "/Reports/Old.PDF" || got.PathLower != "/reports/old.pdf" {

cmd/put.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ func plannedPutFolderResult(src, dst string) putResult {
823823
}
824824

825825
func renderPlannedPutResults(cmd *cobra.Command, input putCommandInput, results []putResult, warnings []jsonWarning) error {
826-
return commandOutput(cmd).Render(func(w io.Writer) error {
826+
return renderOperation(cmd, input, putOperationResults(results), warnings, func(w io.Writer) error {
827827
for _, result := range results {
828828
if result.Kind == putKindFolder {
829829
if err := writeDryRunLine(w, "create directory", result.Input.Target); err != nil {
@@ -836,7 +836,7 @@ func renderPlannedPutResults(cmd *cobra.Command, input putCommandInput, results
836836
}
837837
}
838838
return nil
839-
}, newJSONCommandOperationOutput(cmd, input, putOperationResults(results), warnings))
839+
})
840840
}
841841

842842
// Keep traversal semantics aligned with putRecursiveInternal. Dry-run walks the

0 commit comments

Comments
 (0)