diff_test.go 1.3 KB

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