Skip to content

Commit 493e37f

Browse files
committed
cases: include dirname in case names
For instance, the file 'cases/compile_errors/undeclared_identifier.zig' now corresponds to test name 'compile_errors.undeclared_identifier'. This is useful because you can now filter based on the case dirname using `-Dtest-filter`.
1 parent c1a5caa commit 493e37f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

test/src/Cases.zig

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ fn addFromDirInner(
400400
for (targets) |target_query| {
401401
const output = try manifest.trailingLinesSplit(ctx.arena);
402402
try ctx.translate.append(.{
403-
.name = std.fs.path.stem(filename),
403+
.name = try caseNameFromPath(ctx.arena, filename),
404404
.c_frontend = c_frontend,
405405
.target = b.resolveTargetQuery(target_query),
406406
.link_libc = link_libc,
@@ -416,7 +416,7 @@ fn addFromDirInner(
416416
for (targets) |target_query| {
417417
const output = try manifest.trailingSplit(ctx.arena);
418418
try ctx.translate.append(.{
419-
.name = std.fs.path.stem(filename),
419+
.name = try caseNameFromPath(ctx.arena, filename),
420420
.c_frontend = c_frontend,
421421
.target = b.resolveTargetQuery(target_query),
422422
.link_libc = link_libc,
@@ -454,7 +454,7 @@ fn addFromDirInner(
454454

455455
const next = ctx.cases.items.len;
456456
try ctx.cases.append(.{
457-
.name = std.fs.path.stem(filename),
457+
.name = try caseNameFromPath(ctx.arena, filename),
458458
.import_path = std.fs.path.dirname(filename),
459459
.backend = backend,
460460
.files = .init(ctx.arena),
@@ -1138,3 +1138,17 @@ fn knownFileExtension(filename: []const u8) bool {
11381138
if (it.next() != null) return false;
11391139
return false;
11401140
}
1141+
1142+
/// `path` is a path relative to the root case directory.
1143+
/// e.g. `compile_errors/undeclared_identifier.zig`
1144+
/// The case name is computed by removing the extension and substituting path separators for dots.
1145+
/// e.g. `compile_errors.undeclared_identifier`
1146+
/// Including the directory components makes `-Dtest-filter` more useful, because you can filter
1147+
/// based on subdirectory; e.g. `-Dtest-filter=compile_errors` to run the compile error tets.
1148+
fn caseNameFromPath(arena: Allocator, path: []const u8) Allocator.Error![]const u8 {
1149+
const ext_len = std.fs.path.extension(path).len;
1150+
const path_sans_ext = path[0 .. path.len - ext_len];
1151+
const result = try arena.dupe(u8, path_sans_ext);
1152+
std.mem.replaceScalar(u8, result, std.fs.path.sep, '.');
1153+
return result;
1154+
}

0 commit comments

Comments
 (0)