2018-10-23 11:05:44 -04:00
|
|
|
package container
|
2017-03-06 15:45:12 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-03-06 15:45:12 -05:00
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2017-03-06 15:45:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDiffContextFormatWrite(t *testing.T) {
|
|
|
|
// Check default output format (verbose and non-verbose mode) for table headers
|
|
|
|
cases := []struct {
|
2018-10-23 11:05:44 -04:00
|
|
|
context formatter.Context
|
2017-03-06 15:45:12 -05:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewDiffFormat("table")},
|
2017-03-06 15:45:12 -05:00
|
|
|
`CHANGE TYPE PATH
|
|
|
|
C /var/log/app.log
|
|
|
|
A /usr/app/app.js
|
|
|
|
D /usr/app/old_app.js
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewDiffFormat("table {{.Path}}")},
|
2017-03-06 15:45:12 -05:00
|
|
|
`PATH
|
|
|
|
/var/log/app.log
|
|
|
|
/usr/app/app.js
|
|
|
|
/usr/app/old_app.js
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2018-10-23 11:05:44 -04:00
|
|
|
formatter.Context{Format: NewDiffFormat("{{.Type}}: {{.Path}}")},
|
2017-03-06 15:45:12 -05:00
|
|
|
`C: /var/log/app.log
|
|
|
|
A: /usr/app/app.js
|
|
|
|
D: /usr/app/old_app.js
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
diffs := []container.ContainerChangeResponseItem{
|
2018-04-10 09:55:00 -04:00
|
|
|
{Kind: archive.ChangeModify, Path: "/var/log/app.log"},
|
|
|
|
{Kind: archive.ChangeAdd, Path: "/usr/app/app.js"},
|
|
|
|
{Kind: archive.ChangeDelete, Path: "/usr/app/old_app.js"},
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
testcase.context.Output = out
|
2018-10-23 11:05:44 -04:00
|
|
|
err := DiffFormatWrite(testcase.context, diffs)
|
2017-03-06 15:45:12 -05:00
|
|
|
if err != nil {
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expected)
|
2017-03-06 15:45:12 -05:00
|
|
|
} else {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(testcase.expected, out.String()))
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|