|
| 1 | +package formatter |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/bcmi-labs/arduino-cli/task" |
| 13 | +) |
| 14 | + |
| 15 | +func captureStdout(toCapture func()) []byte { |
| 16 | + rescueStdout := os.Stdout |
| 17 | + r, w, _ := os.Pipe() |
| 18 | + os.Stdout = w |
| 19 | + |
| 20 | + toCapture() |
| 21 | + |
| 22 | + w.Close() |
| 23 | + out, _ := ioutil.ReadAll(r) |
| 24 | + os.Stdout = rescueStdout |
| 25 | + |
| 26 | + fmt.Printf("Captured >>>\n%s<<<\n", out) |
| 27 | + return out |
| 28 | +} |
| 29 | +func TestCreateSequence(t *testing.T) { |
| 30 | + task1 := WrapTask( |
| 31 | + func() task.Result { |
| 32 | + fmt.Println("First task") |
| 33 | + return task.Result{} |
| 34 | + }, |
| 35 | + &TaskWrapperMessages{ |
| 36 | + BeforeMessage: "starting first task", |
| 37 | + AfterMessage: "first task over", |
| 38 | + }) |
| 39 | + task2 := WrapTask( |
| 40 | + func() task.Result { |
| 41 | + fmt.Println("second task") |
| 42 | + return task.Result{} |
| 43 | + }, |
| 44 | + &TaskWrapperMessages{ |
| 45 | + BeforeMessage: "starting second task", |
| 46 | + AfterMessage: "second task over", |
| 47 | + }) |
| 48 | + task3 := WrapTask( |
| 49 | + func() task.Result { |
| 50 | + fmt.Println("third task") |
| 51 | + return task.Result{} |
| 52 | + }, |
| 53 | + &TaskWrapperMessages{ |
| 54 | + BeforeMessage: "starting third task", |
| 55 | + AfterMessage: "third task over", |
| 56 | + }) |
| 57 | + tasks := []task.Task{task1, task2, task3} |
| 58 | + |
| 59 | + sequence := WrapTask(task.CreateSequence(tasks, []bool{true, true, true}).Task(), |
| 60 | + &TaskWrapperMessages{ |
| 61 | + BeforeMessage: "Starting sequence", |
| 62 | + AfterMessage: "Sequence over", |
| 63 | + }) |
| 64 | + |
| 65 | + out := captureStdout(func() { sequence() }) |
| 66 | + require.Equal(t, `Starting sequence |
| 67 | +starting first task |
| 68 | +First task |
| 69 | +first task over |
| 70 | +starting second task |
| 71 | +second task |
| 72 | +second task over |
| 73 | +starting third task |
| 74 | +third task |
| 75 | +third task over |
| 76 | +Sequence over |
| 77 | +`, string(out), "Sequence output") |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +func TestCreateSequenceWithErrorsIgnored(t *testing.T) { |
| 82 | + task1 := WrapTask( |
| 83 | + func() task.Result { |
| 84 | + fmt.Println("First task") |
| 85 | + return task.Result{} |
| 86 | + }, |
| 87 | + &TaskWrapperMessages{ |
| 88 | + BeforeMessage: "starting first task", |
| 89 | + AfterMessage: "first task over", |
| 90 | + ErrorMessage: "first task with error", |
| 91 | + }) |
| 92 | + task2 := WrapTask( |
| 93 | + func() task.Result { |
| 94 | + fmt.Println("second task (with error)") |
| 95 | + return task.Result{Error: errors.New("Error Triggered")} |
| 96 | + }, |
| 97 | + &TaskWrapperMessages{ |
| 98 | + BeforeMessage: "starting second task", |
| 99 | + AfterMessage: "second task over", |
| 100 | + ErrorMessage: "second task with error", |
| 101 | + }) |
| 102 | + task3 := WrapTask( |
| 103 | + func() task.Result { |
| 104 | + fmt.Println("third task (with error)") |
| 105 | + return task.Result{Error: errors.New("Second Error Triggered")} |
| 106 | + }, |
| 107 | + &TaskWrapperMessages{ |
| 108 | + BeforeMessage: "starting third task", |
| 109 | + AfterMessage: "third task over", |
| 110 | + ErrorMessage: "third task with error", |
| 111 | + }) |
| 112 | + task4 := WrapTask( |
| 113 | + func() task.Result { |
| 114 | + fmt.Println("fourth task") |
| 115 | + return task.Result{} |
| 116 | + }, |
| 117 | + &TaskWrapperMessages{ |
| 118 | + BeforeMessage: "starting fourth task", |
| 119 | + AfterMessage: "fourth task over", |
| 120 | + ErrorMessage: "fourth task with error", |
| 121 | + }) |
| 122 | + tasks := []task.Task{task1, task2, task3, task4} |
| 123 | + |
| 124 | + // Do not ignore errors for every step. |
| 125 | + sequence := WrapTask( |
| 126 | + task.CreateSequence(tasks, []bool{true, true, true, true}).Task(), |
| 127 | + &TaskWrapperMessages{ |
| 128 | + BeforeMessage: "Starting sequence", |
| 129 | + AfterMessage: "Sequence over", |
| 130 | + ErrorMessage: "Sequence with errors", |
| 131 | + }) |
| 132 | + |
| 133 | + out := captureStdout(func() { sequence() }) |
| 134 | + require.Equal(t, `Starting sequence |
| 135 | +starting first task |
| 136 | +First task |
| 137 | +first task over |
| 138 | +starting second task |
| 139 | +second task (with error) |
| 140 | +second task with error |
| 141 | +starting third task |
| 142 | +third task (with error) |
| 143 | +third task with error |
| 144 | +starting fourth task |
| 145 | +fourth task |
| 146 | +fourth task over |
| 147 | +Sequence over |
| 148 | +`, string(out), "Sequence output") |
| 149 | +} |
| 150 | + |
| 151 | +func TestCreateSequenceWithErrorsDisplayed(t *testing.T) { |
| 152 | + task1 := WrapTask( |
| 153 | + func() task.Result { |
| 154 | + fmt.Println("First task") |
| 155 | + return task.Result{} |
| 156 | + }, |
| 157 | + &TaskWrapperMessages{ |
| 158 | + BeforeMessage: "starting first task", |
| 159 | + AfterMessage: "first task over", |
| 160 | + ErrorMessage: "first task with error", |
| 161 | + }) |
| 162 | + task2 := WrapTask( |
| 163 | + func() task.Result { |
| 164 | + fmt.Println("second task (with error)") |
| 165 | + return task.Result{Error: errors.New("Error Triggered")} |
| 166 | + }, |
| 167 | + &TaskWrapperMessages{ |
| 168 | + BeforeMessage: "starting second task", |
| 169 | + AfterMessage: "second task over", |
| 170 | + ErrorMessage: "second task with error", |
| 171 | + }) |
| 172 | + task3 := WrapTask( |
| 173 | + func() task.Result { |
| 174 | + fmt.Println("third task (with error)") |
| 175 | + return task.Result{Error: errors.New("Second Error Triggered")} |
| 176 | + }, |
| 177 | + &TaskWrapperMessages{ |
| 178 | + BeforeMessage: "starting third task", |
| 179 | + AfterMessage: "third task over", |
| 180 | + ErrorMessage: "third task with error", |
| 181 | + }) |
| 182 | + task4 := WrapTask( |
| 183 | + func() task.Result { |
| 184 | + fmt.Println("fourth task") |
| 185 | + return task.Result{} |
| 186 | + }, |
| 187 | + &TaskWrapperMessages{ |
| 188 | + BeforeMessage: "starting fourth task", |
| 189 | + AfterMessage: "fourth task over", |
| 190 | + ErrorMessage: "fourth task with error", |
| 191 | + }) |
| 192 | + tasks := []task.Task{task1, task2, task3, task4} |
| 193 | + |
| 194 | + sequence := WrapTask( |
| 195 | + // Do not ignore errors for every step. |
| 196 | + task.CreateSequence(tasks, []bool{false, false, false, false}).Task(), |
| 197 | + &TaskWrapperMessages{ |
| 198 | + BeforeMessage: "Starting sequence", |
| 199 | + AfterMessage: "Sequence over", |
| 200 | + ErrorMessage: "Sequence with errors", |
| 201 | + }) |
| 202 | + |
| 203 | + out := captureStdout(func() { sequence() }) |
| 204 | + require.Equal(t, `Starting sequence |
| 205 | +starting first task |
| 206 | +First task |
| 207 | +first task over |
| 208 | +starting second task |
| 209 | +second task (with error) |
| 210 | +second task with error |
| 211 | +Warning from task 1: Error Triggered |
| 212 | +starting third task |
| 213 | +third task (with error) |
| 214 | +third task with error |
| 215 | +Warning from task 2: Second Error Triggered |
| 216 | +starting fourth task |
| 217 | +fourth task |
| 218 | +fourth task over |
| 219 | +Sequence over |
| 220 | +`, string(out), "Sequence output") |
| 221 | + |
| 222 | +} |
| 223 | + |
| 224 | +func TestCreateSequenceWithPartialErrorsIgnored(t *testing.T) { |
| 225 | + task1 := WrapTask( |
| 226 | + func() task.Result { |
| 227 | + fmt.Println("First task") |
| 228 | + return task.Result{} |
| 229 | + }, |
| 230 | + &TaskWrapperMessages{ |
| 231 | + BeforeMessage: "starting first task", |
| 232 | + AfterMessage: "first task over", |
| 233 | + ErrorMessage: "first task with error", |
| 234 | + }) |
| 235 | + task2 := WrapTask( |
| 236 | + func() task.Result { |
| 237 | + fmt.Println("second task (with error)") |
| 238 | + return task.Result{Error: errors.New("Error Triggered")} |
| 239 | + }, |
| 240 | + &TaskWrapperMessages{ |
| 241 | + BeforeMessage: "starting second task", |
| 242 | + AfterMessage: "second task over", |
| 243 | + ErrorMessage: "second task with error", |
| 244 | + }) |
| 245 | + task3 := WrapTask( |
| 246 | + func() task.Result { |
| 247 | + fmt.Println("third task (with error)") |
| 248 | + return task.Result{Error: errors.New("Second Error Triggered")} |
| 249 | + }, |
| 250 | + &TaskWrapperMessages{ |
| 251 | + BeforeMessage: "starting third task", |
| 252 | + AfterMessage: "third task over", |
| 253 | + ErrorMessage: "third task with error", |
| 254 | + }) |
| 255 | + task4 := WrapTask( |
| 256 | + func() task.Result { |
| 257 | + fmt.Println("fourth task") |
| 258 | + return task.Result{} |
| 259 | + }, |
| 260 | + &TaskWrapperMessages{ |
| 261 | + BeforeMessage: "starting fourth task", |
| 262 | + AfterMessage: "fourth task over", |
| 263 | + ErrorMessage: "fourth task with error", |
| 264 | + }) |
| 265 | + tasks := []task.Task{task1, task2, task3, task4} |
| 266 | + |
| 267 | + sequence := WrapTask( |
| 268 | + // Do not ignore errors for every step. |
| 269 | + task.CreateSequence(tasks, []bool{true, false, true, false}).Task(), |
| 270 | + &TaskWrapperMessages{ |
| 271 | + BeforeMessage: "Starting sequence", |
| 272 | + AfterMessage: "Sequence over", |
| 273 | + ErrorMessage: "Sequence with errors", |
| 274 | + }) |
| 275 | + |
| 276 | + out := captureStdout(func() { sequence() }) |
| 277 | + require.Equal(t, `Starting sequence |
| 278 | +starting first task |
| 279 | +First task |
| 280 | +first task over |
| 281 | +starting second task |
| 282 | +second task (with error) |
| 283 | +second task with error |
| 284 | +Warning from task 1: Error Triggered |
| 285 | +starting third task |
| 286 | +third task (with error) |
| 287 | +third task with error |
| 288 | +starting fourth task |
| 289 | +fourth task |
| 290 | +fourth task over |
| 291 | +Sequence over |
| 292 | +`, string(out), "Sequence output") |
| 293 | + |
| 294 | +} |
0 commit comments