authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-14 21:39:09+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-15 09:30:59+02:00
log5138856a72fc009ec799cb935d9117e3bd72f16a
treefb4713ece697eebf87c57acde6c4bc50f6627471
parent1bdcbd18ae1f312748c9909db98532a8dfd006eb

test harness: Set filename on error return

While calling `next` an error can occur while parsing the file. However, we don't set the filename that is currently being processed, until `next` completed successfully. This means that for invalid test names, the wrong filename was being displayed in the panic message. The fix is to retrieve the correct filename when an error occurs and then setting the filename appropriately.

1 files changed, 22 insertions(+), 6 deletions(-)

src/test.zig+22-6
......@@ -398,6 +398,8 @@ const TestIterator = struct {
398398 start: usize = 0,
399399 end: usize = 0,
400400 filenames: []const []const u8,
401 /// reset on each call to `next`
402 index: usize = 0,
401403
402404 const Error = error{InvalidIncrementalTestIndex};
403405
......@@ -416,12 +418,12 @@ const TestIterator = struct {
416418 }
417419
418420 const remaining = it.filenames[it.end..];
419 var i: usize = 0;
420 while (i < remaining.len - 1) : (i += 1) {
421 it.index = 0;
422 while (it.index < remaining.len - 1) : (it.index += 1) {
421423 // First, check if this file is part of an incremental update sequence
422424 // Split filename into "<base_name>.<index>.<file_ext>"
423 const prev_parts = getTestFileNameParts(remaining[i]);
424 const new_parts = getTestFileNameParts(remaining[i + 1]);
425 const prev_parts = getTestFileNameParts(remaining[it.index]);
426 const new_parts = getTestFileNameParts(remaining[it.index + 1]);
425427
426428 // If base_name and file_ext match, these files are in the same test sequence
427429 // and the new one should be the incremented version of the previous test
......@@ -441,13 +443,22 @@ const TestIterator = struct {
441443 if (new_parts.test_index != null and new_parts.test_index.? != 0)
442444 return error.InvalidIncrementalTestIndex;
443445
444 it.end += i + 1;
446 it.end += it.index + 1;
445447 break;
446448 }
447449 } else {
448450 it.end += remaining.len;
449451 }
450452 }
453
454 /// In the event of an `error.InvalidIncrementalTestIndex`, this function can
455 /// be used to find the current filename that was being processed.
456 /// Asserts the iterator hasn't reached the end.
457 fn currentFilename(it: TestIterator) []const u8 {
458 assert(it.end != it.filenames.len);
459 const remaining = it.filenames[it.end..];
460 return remaining[it.index + 1];
461 }
451462};
452463
453464/// For a filename in the format "<filename>.X.<ext>" or "<filename>.<ext>", returns
......@@ -1051,7 +1062,8 @@ pub const TestContext = struct {
10511062 sortTestFilenames(filenames.items);
10521063
10531064 var test_it = TestIterator{ .filenames = filenames.items };
1054 while (try test_it.next()) |batch| {
1065 while (test_it.next()) |maybe_batch| {
1066 const batch = maybe_batch orelse break;
10551067 const strategy: TestStrategy = if (batch.len > 1) .incremental else .independent;
10561068 var cases = std.ArrayList(usize).init(ctx.arena);
10571069
......@@ -1133,6 +1145,10 @@ pub const TestContext = struct {
11331145 }
11341146 }
11351147 }
1148 } else |err| {
1149 // make sure the current file is set to the file that produced an error
1150 current_file.* = test_it.currentFilename();
1151 return err;
11361152 }
11371153 }
11381154