github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/formatter_diff_test.go (about)

     1  package container
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/docker/cli/cli/command/formatter"
     8  	"github.com/docker/docker/api/types/container"
     9  	"github.com/docker/docker/pkg/archive"
    10  	"gotest.tools/v3/assert"
    11  )
    12  
    13  func TestDiffContextFormatWrite(t *testing.T) {
    14  	// Check default output format (verbose and non-verbose mode) for table headers
    15  	cases := []struct {
    16  		context  formatter.Context
    17  		expected string
    18  	}{
    19  		{
    20  			formatter.Context{Format: NewDiffFormat("table")},
    21  			`CHANGE TYPE   PATH
    22  C             /var/log/app.log
    23  A             /usr/app/app.js
    24  D             /usr/app/old_app.js
    25  `,
    26  		},
    27  		{
    28  			formatter.Context{Format: NewDiffFormat("table {{.Path}}")},
    29  			`PATH
    30  /var/log/app.log
    31  /usr/app/app.js
    32  /usr/app/old_app.js
    33  `,
    34  		},
    35  		{
    36  			formatter.Context{Format: NewDiffFormat("{{.Type}}: {{.Path}}")},
    37  			`C: /var/log/app.log
    38  A: /usr/app/app.js
    39  D: /usr/app/old_app.js
    40  `,
    41  		},
    42  	}
    43  
    44  	diffs := []container.ContainerChangeResponseItem{
    45  		{Kind: archive.ChangeModify, Path: "/var/log/app.log"},
    46  		{Kind: archive.ChangeAdd, Path: "/usr/app/app.js"},
    47  		{Kind: archive.ChangeDelete, Path: "/usr/app/old_app.js"},
    48  	}
    49  
    50  	for _, tc := range cases {
    51  		tc := tc
    52  		t.Run(string(tc.context.Format), func(t *testing.T) {
    53  			out := bytes.NewBufferString("")
    54  			tc.context.Output = out
    55  			err := DiffFormatWrite(tc.context, diffs)
    56  			if err != nil {
    57  				assert.Error(t, err, tc.expected)
    58  			} else {
    59  				assert.Equal(t, out.String(), tc.expected)
    60  			}
    61  		})
    62  	}
    63  }