Skip to content

Commit eb227ab

Browse files
Merge pull request #100 from dreamer-coding/add_timeout_flag
2 parents c85fafc + 81d1034 commit eb227ab

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The Pizza Test CLI provides an efficient way to run and manage tests directly fr
7878
| `color=<mode>` | Set color mode (enable, disable, auto). | Enhances readability in supported terminals. |
7979
| `theme=<name>` | Set the theme (pizza, catch, doctest, etc.). | Customizes the appearance of test output. |
8080
| `verbose=<level>` | Set verbosity level (plain, ci, doge). | Adjusts the level of detail in test output. |
81+
| `timeout=<seconds>` | Set a timeout for test execution. | Ensures tests do not exceed the specified duration, helping to identify long-running tests. |
8182

8283
### Run Command Options
8384
| Option | Description | Notes |

code/logic/common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
// exported flags
2828
// *****************************************************************************
2929

30+
uint64_t G_PIZZA_TIMEOUT = 60; // Default timeout in seconds
3031
int G_PIZZA_DRY_RUN = 0;
3132
int G_PIZZA_FAIL_FAST = 0;
3233
int G_PIZZA_SKIP = 0;
@@ -77,6 +78,7 @@ static void _show_help(void) {
7778
pizza_io_printf("{cyan} color=<mode> Set color mode (enable, disable, auto){reset}\n");
7879
pizza_io_printf("{cyan} theme=<name> Set the theme (fossil, catch, doctest, etc.){reset}\n");
7980
pizza_io_printf("{cyan} verbose=<level> Set verbosity level (plain, ci, doge){reset}\n");
81+
pizza_io_printf("{cyan} timeout=<seconds> Set the timeout for commands (default: 60 seconds){reset}\n");
8082
exit(EXIT_SUCCESS);
8183
}
8284

@@ -365,6 +367,16 @@ fossil_pizza_pallet_t fossil_pizza_pallet_create(int argc, char** argv) {
365367
if (i + 1 < argc && pizza_io_cstr_compare(argv[i + 1], "--help") == 0) {
366368
_show_subhelp_verbose();
367369
}
370+
} else if (strncmp(argv[i], "timeout=", 8) == 0) {
371+
G_PIZZA_TIMEOUT = atoi(argv[i] + 8);
372+
} else if (pizza_io_cstr_compare(argv[i], "timeout") == 0) {
373+
if (i + 1 < argc && pizza_io_cstr_compare(argv[i + 1], "--help") == 0) {
374+
_show_help();
375+
exit(EXIT_SUCCESS);
376+
}
377+
} else {
378+
pizza_io_printf("{red}Error: Unknown command or option '%s'.{reset}\n", argv[i]);
379+
exit(EXIT_FAILURE);
368380
}
369381
}
370382

code/logic/fossil/pizza/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ typedef struct {
325325
// exported flags
326326
// *****************************************************************************
327327

328+
extern uint64_t G_PIZZA_TIMEOUT;
328329
extern int G_PIZZA_DRY_RUN;
329330
extern int G_PIZZA_FAIL_FAST;
330331
extern int G_PIZZA_SKIP;

code/logic/test.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
107107
if (!test_case) return;
108108

109109
const char* result_str =
110+
(test_case->result == FOSSIL_PIZZA_CASE_EMPTY) ? "EMPTY" :
110111
(test_case->result == FOSSIL_PIZZA_CASE_PASS) ? "PASS" :
111112
(test_case->result == FOSSIL_PIZZA_CASE_FAIL) ? "FAIL" :
112-
(test_case->result == FOSSIL_PIZZA_CASE_EMPTY) ? "EMPTY" : "UNKNOWN";
113+
(test_case->result == FOSSIL_PIZZA_CASE_TIMEOUT) ? "TIMEOUT" :
114+
(test_case->result == FOSSIL_PIZZA_CASE_SKIPPED) ? "SKIPPED" :
115+
(test_case->result == FOSSIL_PIZZA_CASE_UNEXPECTED) ? "UNEXPECTED" : "UNKNOWN";
113116

114117
switch (G_PIZZA_THEME) {
115118
case PIZZA_THEME_FOSSIL:
@@ -118,8 +121,10 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
118121
test_case->name, result_str, test_case->elapsed_ns);
119122
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
120123
pizza_io_printf("{blue}========================================{reset}\n");
121-
pizza_io_printf("{cyan}Test Case:{reset} %s\n", test_case->name);
122-
pizza_io_printf("{cyan}Given Result:{reset} %s\n", result_str);
124+
pizza_io_printf("{cyan}Test Case :{reset} %s\n", test_case->name);
125+
pizza_io_printf("{cyan}Given Tags :{reset} %s\n", test_case->tags);
126+
pizza_io_printf("{cyan}Given Criteria:{reset} %s\n", test_case->criteria);
127+
pizza_io_printf("{cyan}Given Result :{reset} %s\n", result_str);
123128
pizza_io_printf("{cyan}With Timestamp:{reset} %llu ns\n", test_case->elapsed_ns);
124129
pizza_io_printf("{blue}========================================{reset}\n");
125130
}
@@ -133,6 +138,8 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
133138
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
134139
pizza_io_printf("{cyan}========================================{reset}\n");
135140
pizza_io_printf("{cyan}Test Case:{reset} %s\n", test_case->name);
141+
pizza_io_printf("{cyan}Given Tags:{reset} %s\n", test_case->tags);
142+
pizza_io_printf("{cyan}Given Criteria:{reset} %s\n", test_case->criteria);
136143
pizza_io_printf("{cyan}Given Result:{reset} %s\n", result_str);
137144
pizza_io_printf("{cyan}With Timestamp:{reset} %llu ns\n", test_case->elapsed_ns);
138145
pizza_io_printf("{cyan}========================================{reset}\n");
@@ -146,6 +153,8 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
146153
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
147154
pizza_io_printf("{blue}[========================================]{reset}\n");
148155
pizza_io_printf("{blue}[Test Case]:{reset} %s\n", test_case->name);
156+
pizza_io_printf("{blue}[Given Tags]:{reset} %s\n", test_case->tags);
157+
pizza_io_printf("{blue}[Given Criteria]:{reset} %s\n", test_case->criteria);
149158
pizza_io_printf("{blue}[Given Result]:{reset} %s\n", result_str);
150159
pizza_io_printf("{blue}[With Timestamp]:{reset} %llu ns\n", test_case->elapsed_ns);
151160
pizza_io_printf("{blue}[========================================]{reset}\n");
@@ -159,6 +168,8 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
159168
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
160169
pizza_io_printf("{green}# ========================================{reset}\n");
161170
pizza_io_printf("{green}# Test Case:{reset} %s\n", test_case->name);
171+
pizza_io_printf("{green}# Given Tags:{reset} %s\n", test_case->tags);
172+
pizza_io_printf("{green}# Given Criteria:{reset} %s\n", test_case->criteria);
162173
pizza_io_printf("{green}# Given Result:{reset} %s\n", result_str);
163174
pizza_io_printf("{green}# With Timestamp:{reset} %llu ns\n", test_case->elapsed_ns);
164175
pizza_io_printf("{green}# ========================================{reset}\n");
@@ -172,6 +183,8 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
172183
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
173184
pizza_io_printf("{blue}[========================================]{reset}\n");
174185
pizza_io_printf("{blue}[Test Case]:{reset} %s\n", test_case->name);
186+
pizza_io_printf("{blue}[Given Tags]:{reset} %s\n", test_case->tags);
187+
pizza_io_printf("{blue}[Given Criteria]:{reset} %s\n", test_case->criteria);
175188
pizza_io_printf("{blue}[Given Result]:{reset} %s\n", result_str);
176189
pizza_io_printf("{blue}[With Timestamp]:{reset} %llu ns\n", test_case->elapsed_ns);
177190
pizza_io_printf("{blue}[========================================]{reset}\n");
@@ -185,6 +198,8 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
185198
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
186199
pizza_io_printf("{magenta}========================================{reset}\n");
187200
pizza_io_printf("{magenta}Test Case:{reset} %s\n", test_case->name);
201+
pizza_io_printf("{magenta}Given Tags:{reset} %s\n", test_case->tags);
202+
pizza_io_printf("{magenta}Given Criteria:{reset} %s\n", test_case->criteria);
188203
pizza_io_printf("{magenta}Given Result:{reset} %s\n", result_str);
189204
pizza_io_printf("{magenta}With Timestamp:{reset} %llu ns\n", test_case->elapsed_ns);
190205
pizza_io_printf("{magenta}========================================{reset}\n");
@@ -198,13 +213,19 @@ void fossil_pizza_test_output(const fossil_pizza_case_t* test_case) {
198213
} else if (G_PIZZA_VERBOSE == PIZZA_VERBOSE_DOGE) {
199214
pizza_io_printf("{cyan}========================================{reset}\n");
200215
pizza_io_printf("{cyan}Test Case:{reset} %s\n", test_case->name);
216+
pizza_io_printf("{cyan}Given Tags:{reset} %s\n", test_case->tags);
217+
pizza_io_printf("{cyan}Given Criteria:{reset} %s\n", test_case->criteria);
201218
pizza_io_printf("{cyan}Given Result:{reset} %s\n", result_str);
202219
pizza_io_printf("{cyan}With Timestamp:{reset} %llu ns\n", test_case->elapsed_ns);
203220
pizza_io_printf("{cyan}========================================{reset}\n");
204221
}
205222
break;
206223
}
207224
}
225+
// Utility function to convert seconds to nanoseconds
226+
static uint64_t seconds_to_nanoseconds(uint64_t seconds) {
227+
return seconds * 1000000000ULL;
228+
}
208229

209230
void fossil_pizza_run_test(const fossil_pizza_engine_t* engine, fossil_pizza_case_t* test_case, fossil_pizza_suite_t* suite) {
210231
if (!test_case || !suite) return;
@@ -225,7 +246,7 @@ void fossil_pizza_run_test(const fossil_pizza_engine_t* engine, fossil_pizza_cas
225246
test_case->run();
226247
uint64_t elapsed_time = fossil_pizza_now_ns() - start_time;
227248

228-
if (1000000ULL > 0 && elapsed_time > 1000000ULL) {
249+
if (elapsed_time > seconds_to_nanoseconds(G_PIZZA_TIMEOUT)) { // 1 minute in nanoseconds
229250
test_case->result = FOSSIL_PIZZA_CASE_TIMEOUT;
230251
} else {
231252
test_case->result = FOSSIL_PIZZA_CASE_PASS;
@@ -247,6 +268,10 @@ void fossil_pizza_run_test(const fossil_pizza_engine_t* engine, fossil_pizza_cas
247268

248269
// Output test case result
249270
fossil_pizza_test_output(test_case);
271+
} else if (test_case->result == FOSSIL_PIZZA_CASE_SKIPPED) {
272+
// Output skipped test case result
273+
test_case->elapsed_ns = 0; // No time elapsed for skipped tests
274+
fossil_pizza_test_output(test_case);
250275
} else {
251276
// Handle unexpected cases
252277
test_case->result = FOSSIL_PIZZA_CASE_UNEXPECTED;

0 commit comments

Comments
 (0)