tty_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package progress
  14. import (
  15. "fmt"
  16. "sync"
  17. "testing"
  18. "time"
  19. "gotest.tools/v3/assert"
  20. )
  21. func TestLineText(t *testing.T) {
  22. now := time.Now()
  23. ev := Event{
  24. ID: "id",
  25. Text: "Text",
  26. Status: Working,
  27. StatusText: "Status",
  28. endTime: now,
  29. startTime: now,
  30. spinner: &spinner{
  31. chars: []string{"."},
  32. },
  33. }
  34. lineWidth := len(fmt.Sprintf("%s %s", ev.ID, ev.Text))
  35. out := lineText(ev, "", 50, lineWidth, true)
  36. assert.Equal(t, out, "\x1b[37m . id Text Status 0.0s\n\x1b[0m")
  37. out = lineText(ev, "", 50, lineWidth, false)
  38. assert.Equal(t, out, " . id Text Status 0.0s\n")
  39. ev.Status = Done
  40. out = lineText(ev, "", 50, lineWidth, true)
  41. assert.Equal(t, out, "\x1b[34m . id Text Status 0.0s\n\x1b[0m")
  42. ev.Status = Error
  43. out = lineText(ev, "", 50, lineWidth, true)
  44. assert.Equal(t, out, "\x1b[31m . id Text Status 0.0s\n\x1b[0m")
  45. ev.Status = Warning
  46. out = lineText(ev, "", 50, lineWidth, true)
  47. assert.Equal(t, out, "\x1b[33m . id Text Status 0.0s\n\x1b[0m")
  48. }
  49. func TestLineTextSingleEvent(t *testing.T) {
  50. now := time.Now()
  51. ev := Event{
  52. ID: "id",
  53. Text: "Text",
  54. Status: Done,
  55. StatusText: "Status",
  56. startTime: now,
  57. spinner: &spinner{
  58. chars: []string{"."},
  59. },
  60. }
  61. lineWidth := len(fmt.Sprintf("%s %s", ev.ID, ev.Text))
  62. out := lineText(ev, "", 50, lineWidth, true)
  63. assert.Equal(t, out, "\x1b[34m . id Text Status 0.0s\n\x1b[0m")
  64. }
  65. func TestErrorEvent(t *testing.T) {
  66. w := &ttyWriter{
  67. events: map[string]Event{},
  68. mtx: &sync.Mutex{},
  69. }
  70. e := Event{
  71. ID: "id",
  72. Text: "Text",
  73. Status: Working,
  74. StatusText: "Working",
  75. startTime: time.Now(),
  76. spinner: &spinner{
  77. chars: []string{"."},
  78. },
  79. }
  80. // Fire "Working" event and check end time isn't touched
  81. w.Event(e)
  82. event, ok := w.events[e.ID]
  83. assert.Assert(t, ok)
  84. assert.Assert(t, event.endTime.Equal(time.Time{}))
  85. // Fire "Error" event and check end time is set
  86. e.Status = Error
  87. w.Event(e)
  88. event, ok = w.events[e.ID]
  89. assert.Assert(t, ok)
  90. assert.Assert(t, event.endTime.After(time.Now().Add(-10*time.Second)))
  91. }
  92. func TestWarningEvent(t *testing.T) {
  93. w := &ttyWriter{
  94. events: map[string]Event{},
  95. mtx: &sync.Mutex{},
  96. }
  97. e := Event{
  98. ID: "id",
  99. Text: "Text",
  100. Status: Working,
  101. StatusText: "Working",
  102. startTime: time.Now(),
  103. spinner: &spinner{
  104. chars: []string{"."},
  105. },
  106. }
  107. // Fire "Working" event and check end time isn't touched
  108. w.Event(e)
  109. event, ok := w.events[e.ID]
  110. assert.Assert(t, ok)
  111. assert.Assert(t, event.endTime.Equal(time.Time{}))
  112. // Fire "Warning" event and check end time is set
  113. e.Status = Warning
  114. w.Event(e)
  115. event, ok = w.events[e.ID]
  116. assert.Assert(t, ok)
  117. assert.Assert(t, event.endTime.After(time.Now().Add(-10*time.Second)))
  118. }