@@ -16,6 +16,8 @@ package cmd
1616
1717import (
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+
102159func 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" {
0 commit comments