|
17 | 17 | package test
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "encoding/json" |
| 21 | + "fmt" |
20 | 22 | "slices"
|
21 | 23 | "testing"
|
22 | 24 |
|
23 | 25 | "github.com/containerd/nerdctl/mod/tigron/internal/assertive"
|
| 26 | + "github.com/containerd/nerdctl/mod/tigron/internal/formatter" |
24 | 27 | )
|
25 | 28 |
|
26 | 29 | // Case describes an entire test-case, including data, setup and cleanup routines, command and
|
@@ -62,27 +65,33 @@ type Case struct {
|
62 | 65 | parent *Case
|
63 | 66 | }
|
64 | 67 |
|
| 68 | +const ( |
| 69 | + startDecorator = "🚀" |
| 70 | + cleanDecorator = "🧽" |
| 71 | + setupDecorator = "🏗" |
| 72 | + subinDecorator = "⤵️" |
| 73 | + suboutDecorator = "↩️" |
| 74 | +) |
| 75 | + |
65 | 76 | // Run prepares and executes the test, and any possible subtests.
|
66 |
| -// |
67 |
| -//nolint:gocognit |
68 | 77 | func (test *Case) Run(t *testing.T) {
|
69 | 78 | t.Helper()
|
70 | 79 | // Run the test
|
71 | 80 | //nolint:thelper
|
72 | 81 | testRun := func(subT *testing.T) {
|
73 | 82 | subT.Helper()
|
74 | 83 |
|
75 |
| - assertive.True(subT, test.t == nil, "You cannot run a test multiple times") |
| 84 | + silentT := assertive.WithSilentSuccess(subT) |
| 85 | + |
| 86 | + assertive.True(silentT, test.t == nil, "You cannot run a test multiple times") |
| 87 | + assertive.True(silentT, test.Description != "" || test.parent == nil, |
| 88 | + "A subtest description cannot be empty") |
| 89 | + assertive.True(silentT, test.Command == nil || test.Expected != nil, |
| 90 | + "Expectations for a test command cannot be nil. You may want to use `Setup` instead"+ |
| 91 | + "of `Command`.") |
76 | 92 |
|
77 | 93 | // Attach testing.T
|
78 | 94 | test.t = subT
|
79 |
| - assertive.True( |
80 |
| - test.t, |
81 |
| - test.Description != "" || test.parent == nil, |
82 |
| - "A test description cannot be empty", |
83 |
| - ) |
84 |
| - assertive.True(test.t, test.Command == nil || test.Expected != nil, |
85 |
| - "Expectations for a test command cannot be nil. You may want to use Setup instead.") |
86 | 95 |
|
87 | 96 | // Ensure we have env
|
88 | 97 | if test.Env == nil {
|
@@ -168,49 +177,83 @@ func (test *Case) Run(t *testing.T) {
|
168 | 177 | }
|
169 | 178 |
|
170 | 179 | // Execute cleanups now
|
171 |
| - test.t.Log("") |
172 |
| - test.t.Log("======================== Pre-test cleanup ========================") |
173 |
| - |
174 |
| - for _, cleanup := range cleanups { |
175 |
| - cleanup(test.Data, test.helpers) |
176 |
| - } |
177 |
| - |
178 |
| - // Register the cleanups, in reverse |
179 |
| - test.t.Cleanup(func() { |
180 |
| - test.t.Log("") |
181 |
| - test.t.Log("======================== Post-test cleanup ========================") |
182 |
| - |
183 |
| - slices.Reverse(cleanups) |
| 180 | + if len(cleanups) > 0 { |
| 181 | + test.t.Log( |
| 182 | + "\n\n" + formatter.Table( |
| 183 | + [][]any{{cleanDecorator, fmt.Sprintf("%q: initial cleanup", test.t.Name())}}, |
| 184 | + "=", |
| 185 | + ) + "\n", |
| 186 | + ) |
184 | 187 |
|
185 | 188 | for _, cleanup := range cleanups {
|
186 | 189 | cleanup(test.Data, test.helpers)
|
187 | 190 | }
|
188 |
| - }) |
189 | 191 |
|
190 |
| - // Run the setups |
191 |
| - test.t.Log("") |
192 |
| - test.t.Log("======================== Test setup ========================") |
| 192 | + // Register the cleanups, in reverse |
| 193 | + test.t.Cleanup(func() { |
| 194 | + test.t.Helper() |
| 195 | + test.t.Log( |
| 196 | + "\n\n" + formatter.Table( |
| 197 | + [][]any{{cleanDecorator, fmt.Sprintf("%q: post-cleanup", test.t.Name())}}, |
| 198 | + "=", |
| 199 | + ) + "\n", |
| 200 | + ) |
| 201 | + |
| 202 | + slices.Reverse(cleanups) |
193 | 203 |
|
194 |
| - for _, setup := range setups { |
195 |
| - setup(test.Data, test.helpers) |
| 204 | + for _, cleanup := range cleanups { |
| 205 | + cleanup(test.Data, test.helpers) |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | + |
| 210 | + // Run the setups |
| 211 | + if len(setups) > 0 { |
| 212 | + test.t.Log( |
| 213 | + "\n\n" + formatter.Table( |
| 214 | + [][]any{{setupDecorator, fmt.Sprintf("%q: setup", test.t.Name())}}, |
| 215 | + "=", |
| 216 | + ) + "\n", |
| 217 | + ) |
| 218 | + |
| 219 | + for _, setup := range setups { |
| 220 | + setup(test.Data, test.helpers) |
| 221 | + } |
196 | 222 | }
|
197 | 223 |
|
198 | 224 | // Run the command if any, with expectations
|
199 | 225 | // Note: if we have a command, we already know we DO have Expected
|
200 |
| - test.t.Log("") |
201 |
| - test.t.Log("======================== Test Run ========================") |
202 |
| - |
203 | 226 | if test.Command != nil {
|
204 |
| - test.Command(test.Data, test.helpers).Run(test.Expected(test.Data, test.helpers)) |
| 227 | + cmd := test.Command(test.Data, test.helpers) |
| 228 | + |
| 229 | + debugConfig, _ := json.MarshalIndent(test.Config.(*config).config, "", " ") |
| 230 | + debugData, _ := json.MarshalIndent(test.Data.(*data).labels, "", " ") |
| 231 | + |
| 232 | + test.t.Log( |
| 233 | + "\n\n" + formatter.Table( |
| 234 | + [][]any{ |
| 235 | + {startDecorator, fmt.Sprintf("%q: starting test!", test.t.Name())}, |
| 236 | + {"cwd", test.Data.TempDir()}, |
| 237 | + {"config", string(debugConfig)}, |
| 238 | + {"data", string(debugData)}, |
| 239 | + }, |
| 240 | + "=", |
| 241 | + ) + "\n", |
| 242 | + ) |
| 243 | + |
| 244 | + cmd.Run(test.Expected(test.Data, test.helpers)) |
205 | 245 | }
|
206 | 246 |
|
207 |
| - // Now go for the subtests |
208 |
| - test.t.Log("") |
209 |
| - test.t.Log("======================== Processing subtests ========================") |
| 247 | + if len(test.SubTests) > 0 { |
| 248 | + // Now go for the subtests |
| 249 | + test.t.Logf("\n%s️ %q: into subtests prep", subinDecorator, test.t.Name()) |
| 250 | + |
| 251 | + for _, subTest := range test.SubTests { |
| 252 | + subTest.parent = test |
| 253 | + subTest.Run(test.t) |
| 254 | + } |
210 | 255 |
|
211 |
| - for _, subTest := range test.SubTests { |
212 |
| - subTest.parent = test |
213 |
| - subTest.Run(test.t) |
| 256 | + test.t.Logf("\n%s️ %q: done with subtests prep", suboutDecorator, test.t.Name()) |
214 | 257 | }
|
215 | 258 | }
|
216 | 259 |
|
|
0 commit comments