-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmakTest.pm
More file actions
515 lines (407 loc) · 11.6 KB
/
SmakTest.pm
File metadata and controls
515 lines (407 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package SmakTest;
# Testing framework for smak - interactive and automated testing
use strict;
use warnings;
use Exporter 'import';
use IO::Select;
use Time::HiRes qw(time sleep);
use File::Path qw(remove_tree);
use File::Copy;
use Cwd;
our @EXPORT_OK = qw(
run_test_suite
run_interactive_tests
test_ctrl_c
test_file_dirty
test_file_remove
test_rebuild_detection
test_clean_rebuild
);
our $VERBOSE = $ENV{TEST_VERBOSE} || 0;
# Test state
my $tests_run = 0;
my $tests_passed = 0;
my $tests_failed = 0;
my @failures;
my @test_log;
sub vprint {
print @_ if $VERBOSE;
}
sub log_test {
push @test_log, @_;
}
sub test_header {
my ($name) = @_;
$tests_run++;
my $header = "\n" . "=" x 60 . "\n" . "TEST $tests_run: $name\n" . "=" x 60;
print "$header\n";
log_test($header);
}
sub pass {
my ($name) = @_;
$tests_passed++;
my $msg = "✓ $name";
print "$msg\n";
log_test($msg);
}
sub fail {
my ($name, $reason) = @_;
$tests_failed++;
push @failures, "$name: $reason";
my $msg = "✗ $name";
$msg .= "\n Reason: $reason" if $reason;
print "$msg\n";
log_test($msg);
}
sub reset_stats {
$tests_run = 0;
$tests_passed = 0;
$tests_failed = 0;
@failures = ();
@test_log = ();
}
sub print_summary {
print "\n";
print "=" x 60 . "\n";
print "TEST SUMMARY\n";
print "=" x 60 . "\n";
print "Total tests: $tests_run\n";
print "Passed: $tests_passed\n";
print "Failed: $tests_failed\n";
if (@failures) {
print "\nFailures:\n";
for my $failure (@failures) {
print " - $failure\n";
}
}
print "\nPass rate: ";
if ($tests_run > 0) {
printf "%.1f%%\n", ($tests_passed / $tests_run) * 100;
} else {
print "N/A\n";
}
print "=" x 60 . "\n";
}
# Send command to smak and wait for response
sub send_command {
my ($socket, $command, $timeout) = @_;
$timeout ||= 10;
vprint ">>> Sending: $command\n";
print $socket "$command\n";
my $output = '';
my $sel = IO::Select->new($socket);
my $start = time();
my $last_output = time();
while (time() - $start < $timeout) {
my @ready = $sel->can_read(0.1);
for my $fh (@ready) {
my $line = <$fh>;
if (defined $line) {
$output .= $line;
vprint $line;
$last_output = time();
# Stop reading when we see a prompt
return $output if $line =~ /smak>|smak-attach>/;
}
}
# If no output for 2 seconds, assume done
last if time() - $last_output > 2;
}
return $output;
}
# Wait for output with pattern
sub wait_for_output {
my ($socket, $pattern, $timeout) = @_;
$timeout ||= 30;
my $output = '';
my $sel = IO::Select->new($socket);
my $start = time();
while (time() - $start < $timeout) {
my @ready = $sel->can_read(0.1);
for my $fh (@ready) {
my $line = <$fh>;
if (defined $line) {
$output .= $line;
vprint $line;
return $output if $output =~ /$pattern/;
}
}
}
return $output;
}
# Test: Build current project
sub test_build_all {
my ($socket) = @_;
test_header("Build all targets");
my $output = send_command($socket, "b all", 60);
if ($output =~ /Build succeeded|✓/) {
pass("Build all targets");
return 1;
} elsif ($output =~ /No default target/) {
# Try just 'b' for default target
$output = send_command($socket, "b", 60);
if ($output =~ /Build succeeded|✓/) {
pass("Build default target");
return 1;
}
}
fail("Build all targets", "Build did not succeed");
return 0;
}
# Test: Clean and rebuild
sub test_clean_rebuild {
my ($socket) = @_;
test_header("Clean and rebuild");
# Clean
my $output1 = send_command($socket, "clean", 10);
# Rebuild
my $output2 = send_command($socket, "b all", 60);
if ($output2 =~ /Build succeeded|✓/) {
pass("Clean and rebuild");
return 1;
} else {
fail("Clean and rebuild", "Rebuild failed after clean");
return 0;
}
}
# Test: File removal and rebuild detection
sub test_file_remove {
my ($socket, $file) = @_;
test_header("File removal and rebuild detection");
# Build first
my $output1 = send_command($socket, "b all", 60);
if ($output1 !~ /Build succeeded|✓|up to date/) {
fail("File removal test", "Initial build failed");
return 0;
}
# Find a file to remove if not specified
unless ($file) {
my @objs = glob("*.o");
if (@objs) {
$file = $objs[0];
} else {
fail("File removal test", "No object files found");
return 0;
}
}
unless (-f $file) {
fail("File removal test", "File '$file' does not exist");
return 0;
}
vprint "Removing $file\n";
unlink($file);
# Rebuild
my $output2 = send_command($socket, "b all", 60);
if (-f $file && $output2 =~ /Build succeeded|✓/) {
pass("File removal and rebuild");
return 1;
} else {
fail("File removal and rebuild", "File not recreated or build failed");
return 0;
}
}
# Test: File modification and rebuild detection
sub test_file_dirty {
my ($socket, $file) = @_;
test_header("File modification and rebuild detection");
# Build first
my $output1 = send_command($socket, "b all", 60);
if ($output1 !~ /Build succeeded|✓|up to date/) {
fail("File dirty test", "Initial build failed");
return 0;
}
# Find a file to modify if not specified
unless ($file) {
my @srcs = glob("*.c");
push @srcs, glob("*.cpp");
push @srcs, glob("*.cc");
if (@srcs) {
$file = $srcs[0];
} else {
fail("File dirty test", "No source files found");
return 0;
}
}
unless (-f $file) {
fail("File dirty test", "File '$file' does not exist");
return 0;
}
vprint "Modifying $file\n";
sleep(1); # Ensure mtime changes
my $backup = "$file.testbackup";
copy($file, $backup) or do {
fail("File dirty test", "Cannot backup file: $!");
return 0;
};
# Append a comment
open(my $fh, '>>', $file) or do {
fail("File dirty test", "Cannot open file: $!");
return 0;
};
print $fh "// Test modification\n";
close($fh);
# Rebuild
my $output2 = send_command($socket, "b all", 60);
# Restore original file
move($backup, $file);
if ($output2 =~ /Build succeeded|✓/) {
pass("File modification and rebuild");
return 1;
} else {
fail("File modification and rebuild", "Rebuild failed");
return 0;
}
}
# Test: Status command
sub test_status {
my ($socket) = @_;
test_header("Status command");
my $output = send_command($socket, "status", 5);
if ($output =~ /workers|jobs|ready/i || length($output) > 10) {
pass("Status command");
return 1;
} else {
fail("Status command", "No status information received");
return 0;
}
}
# Test: Help command
sub test_help {
my ($socket) = @_;
test_header("Help command");
my $output = send_command($socket, "help", 5);
if ($output =~ /commands|help|build/i) {
pass("Help command");
return 1;
} else {
fail("Help command", "No help text received");
return 0;
}
}
# Test: Rebuild detection (should be up to date)
sub test_rebuild_detection {
my ($socket) = @_;
test_header("Rebuild detection (up-to-date check)");
# Build twice
my $output1 = send_command($socket, "b all", 60);
my $output2 = send_command($socket, "b all", 60);
if ($output2 =~ /up to date|nothing to do|0 jobs/i || $output2 =~ /Build succeeded/) {
pass("Rebuild detection");
return 1;
} else {
fail("Rebuild detection", "Did not detect up-to-date state");
return 0;
}
}
# Interactive test menu
sub run_interactive_tests {
my ($socket) = @_;
reset_stats();
print "\n";
print "=" x 60 . "\n";
print "SMAK INTERACTIVE TEST MENU\n";
print "=" x 60 . "\n";
print "Working directory: " . getcwd() . "\n";
print "\n";
print "Available tests:\n";
print " 1. Build all targets\n";
print " 2. Clean and rebuild\n";
print " 3. File removal and rebuild\n";
print " 4. File modification and rebuild\n";
print " 5. Rebuild detection (up-to-date)\n";
print " 6. Status command\n";
print " 7. Help command\n";
print " a. Run all tests\n";
print " q. Quit test mode\n";
print "\n";
while (1) {
print "Test> ";
my $choice = <STDIN>;
chomp $choice;
$choice =~ s/^\s+|\s+$//g;
last if $choice eq 'q' || $choice eq 'quit';
if ($choice eq '1') {
test_build_all($socket);
} elsif ($choice eq '2') {
test_clean_rebuild($socket);
} elsif ($choice eq '3') {
test_file_remove($socket, undef);
} elsif ($choice eq '4') {
test_file_dirty($socket, undef);
} elsif ($choice eq '5') {
test_rebuild_detection($socket);
} elsif ($choice eq '6') {
test_status($socket);
} elsif ($choice eq '7') {
test_help($socket);
} elsif ($choice eq 'a') {
print "\nRunning all tests...\n";
test_build_all($socket);
test_rebuild_detection($socket);
test_status($socket);
test_help($socket);
test_file_dirty($socket, undef);
test_file_remove($socket, undef);
test_clean_rebuild($socket);
} else {
print "Invalid choice. Enter 1-7, 'a' for all, or 'q' to quit.\n";
next;
}
print "\n";
}
print_summary();
}
# Automated test suite
sub run_test_suite {
my ($socket) = @_;
reset_stats();
print "\n";
print "=" x 60 . "\n";
print "SMAK AUTOMATED TEST SUITE\n";
print "=" x 60 . "\n";
print "Working directory: " . getcwd() . "\n";
print "\n";
# Run all tests
test_build_all($socket);
test_rebuild_detection($socket);
test_status($socket);
test_help($socket);
test_file_dirty($socket, undef);
test_file_remove($socket, undef);
test_clean_rebuild($socket);
print_summary();
return $tests_failed == 0;
}
1;
__END__
=head1 NAME
SmakTest - Testing framework for smak build system
=head1 SYNOPSIS
use SmakTest qw(run_interactive_tests run_test_suite);
# Interactive testing
run_interactive_tests($socket);
# Automated testing
my $success = run_test_suite($socket);
=head1 DESCRIPTION
SmakTest provides interactive and automated testing capabilities for smak.
Tests can be run against a live job server through a socket connection.
=head1 FUNCTIONS
=over 4
=item run_interactive_tests($socket)
Presents an interactive menu for running individual tests.
=item run_test_suite($socket)
Runs all tests automatically and returns true if all pass.
=item test_build_all($socket)
Tests building all targets.
=item test_clean_rebuild($socket)
Tests clean and rebuild cycle.
=item test_file_remove($socket, $file)
Tests file removal and rebuild detection.
=item test_file_dirty($socket, $file)
Tests file modification and rebuild detection.
=item test_rebuild_detection($socket)
Tests that rebuild is skipped when up-to-date.
=back
=head1 AUTHOR
Claude Code
=cut