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 {...@@ -398,6 +398,8 @@ const TestIterator = struct {
398 start: usize = 0,398 start: usize = 0,
399 end: usize = 0,399 end: usize = 0,
400 filenames: []const []const u8,400 filenames: []const []const u8,
401 /// reset on each call to `next`
402 index: usize = 0,
401403
402 const Error = error{InvalidIncrementalTestIndex};404 const Error = error{InvalidIncrementalTestIndex};
403405
...@@ -416,12 +418,12 @@ const TestIterator = struct {...@@ -416,12 +418,12 @@ const TestIterator = struct {
416 }418 }
417419
418 const remaining = it.filenames[it.end..];420 const remaining = it.filenames[it.end..];
419 var i: usize = 0;421 it.index = 0;
420 while (i < remaining.len - 1) : (i += 1) {422 while (it.index < remaining.len - 1) : (it.index += 1) {
421 // First, check if this file is part of an incremental update sequence423 // First, check if this file is part of an incremental update sequence
422 // Split filename into "<base_name>.<index>.<file_ext>"424 // Split filename into "<base_name>.<index>.<file_ext>"
423 const prev_parts = getTestFileNameParts(remaining[i]);425 const prev_parts = getTestFileNameParts(remaining[it.index]);
424 const new_parts = getTestFileNameParts(remaining[i + 1]);426 const new_parts = getTestFileNameParts(remaining[it.index + 1]);
425427
426 // If base_name and file_ext match, these files are in the same test sequence428 // If base_name and file_ext match, these files are in the same test sequence
427 // and the new one should be the incremented version of the previous test429 // and the new one should be the incremented version of the previous test
...@@ -441,13 +443,22 @@ const TestIterator = struct {...@@ -441,13 +443,22 @@ const TestIterator = struct {
441 if (new_parts.test_index != null and new_parts.test_index.? != 0)443 if (new_parts.test_index != null and new_parts.test_index.? != 0)
442 return error.InvalidIncrementalTestIndex;444 return error.InvalidIncrementalTestIndex;
443445
444 it.end += i + 1;446 it.end += it.index + 1;
445 break;447 break;
446 }448 }
447 } else {449 } else {
448 it.end += remaining.len;450 it.end += remaining.len;
449 }451 }
450 }452 }
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 }
451};462};
452463
453/// For a filename in the format "<filename>.X.<ext>" or "<filename>.<ext>", returns464/// For a filename in the format "<filename>.X.<ext>" or "<filename>.<ext>", returns
...@@ -1051,7 +1062,8 @@ pub const TestContext = struct {...@@ -1051,7 +1062,8 @@ pub const TestContext = struct {
1051 sortTestFilenames(filenames.items);1062 sortTestFilenames(filenames.items);
10521063
1053 var test_it = TestIterator{ .filenames = filenames.items };1064 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;
1055 const strategy: TestStrategy = if (batch.len > 1) .incremental else .independent;1067 const strategy: TestStrategy = if (batch.len > 1) .incremental else .independent;
1056 var cases = std.ArrayList(usize).init(ctx.arena);1068 var cases = std.ArrayList(usize).init(ctx.arena);
10571069
...@@ -1133,6 +1145,10 @@ pub const TestContext = struct {...@@ -1133,6 +1145,10 @@ pub const TestContext = struct {
1133 }1145 }
1134 }1146 }
1135 }1147 }
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;
1136 }1152 }
1137 }1153 }
11381154