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"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
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")},
|
2020-08-28 17:00:21 -04:00
|
|
|
`CHANGE TYPE PATH
|
|
|
|
C /var/log/app.log
|
|
|
|
A /usr/app/app.js
|
|
|
|
D /usr/app/old_app.js
|
2017-03-06 15:45:12 -05:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
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
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-28 21:51:11 -04:00
|
|
|
diffs := []container.FilesystemChange{
|
|
|
|
{Kind: container.ChangeModify, Path: "/var/log/app.log"},
|
|
|
|
{Kind: container.ChangeAdd, Path: "/usr/app/app.js"},
|
|
|
|
{Kind: container.ChangeDelete, Path: "/usr/app/old_app.js"},
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
|
2020-08-28 17:00:21 -04:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(string(tc.context.Format), func(t *testing.T) {
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
tc.context.Output = out
|
|
|
|
err := DiffFormatWrite(tc.context, diffs)
|
|
|
|
if err != nil {
|
|
|
|
assert.Error(t, err, tc.expected)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, out.String(), tc.expected)
|
|
|
|
}
|
|
|
|
})
|
2017-03-06 15:45:12 -05:00
|
|
|
}
|
|
|
|
}
|