Skip to content

Commit 8e72a25

Browse files
committed
doctest: handle relative paths correctly
Evaluate all child processes in the temporary directory, and use `std.fs.path.relative` to make every other path relative to that child cwd instead of our cwd. Resolves: ziglang#22119
1 parent 9064907 commit 8e72a25

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

tools/doctest.zig

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,18 @@ pub fn main() !void {
8989
const out = bw.writer();
9090

9191
try printSourceBlock(arena, out, source, fs.path.basename(input_path));
92-
try printOutput(arena, out, code, input_path, zig_path, opt_zig_lib_dir, tmp_dir_path);
92+
try printOutput(
93+
arena,
94+
out,
95+
code,
96+
tmp_dir_path,
97+
try std.fs.path.relative(arena, tmp_dir_path, zig_path),
98+
try std.fs.path.relative(arena, tmp_dir_path, input_path),
99+
if (opt_zig_lib_dir) |zig_lib_dir|
100+
try std.fs.path.relative(arena, tmp_dir_path, zig_lib_dir)
101+
else
102+
null,
103+
);
93104

94105
try bw.flush();
95106
}
@@ -98,10 +109,14 @@ fn printOutput(
98109
arena: Allocator,
99110
out: anytype,
100111
code: Code,
101-
input_path: []const u8,
112+
/// Relative to this process' cwd.
113+
tmp_dir_path: []const u8,
114+
/// Relative to `tmp_dir_path`.
102115
zig_exe: []const u8,
116+
/// Relative to `tmp_dir_path`.
117+
input_path: []const u8,
118+
/// Relative to `tmp_dir_path`.
103119
opt_zig_lib_dir: ?[]const u8,
104-
tmp_dir_path: []const u8,
105120
) !void {
106121
var env_map = try process.getEnvMap(arena);
107122
try env_map.put("CLICOLOR_FORCE", "1");
@@ -304,7 +319,7 @@ fn printOutput(
304319
},
305320
}
306321
}
307-
const result = run(arena, &env_map, null, test_args.items) catch
322+
const result = run(arena, &env_map, tmp_dir_path, test_args.items) catch
308323
fatal("test failed", .{});
309324
const escaped_stderr = try escapeHtml(arena, result.stderr);
310325
const escaped_stdout = try escapeHtml(arena, result.stdout);
@@ -339,6 +354,7 @@ fn printOutput(
339354
.allocator = arena,
340355
.argv = test_args.items,
341356
.env_map = &env_map,
357+
.cwd = tmp_dir_path,
342358
.max_output_bytes = max_doc_file_size,
343359
});
344360
switch (result.term) {
@@ -395,6 +411,7 @@ fn printOutput(
395411
.allocator = arena,
396412
.argv = test_args.items,
397413
.env_map = &env_map,
414+
.cwd = tmp_dir_path,
398415
.max_output_bytes = max_doc_file_size,
399416
});
400417
switch (result.term) {
@@ -432,10 +449,7 @@ fn printOutput(
432449
zig_exe, "build-obj",
433450
"--color", "on",
434451
"--name", code_name,
435-
input_path,
436-
try std.fmt.allocPrint(arena, "-femit-bin={s}{c}{s}", .{
437-
tmp_dir_path, fs.path.sep, name_plus_obj_ext,
438-
}),
452+
input_path, try std.fmt.allocPrint(arena, "-femit-bin={s}", .{name_plus_obj_ext}),
439453
});
440454
if (opt_zig_lib_dir) |zig_lib_dir| {
441455
try build_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
@@ -465,6 +479,7 @@ fn printOutput(
465479
.allocator = arena,
466480
.argv = build_args.items,
467481
.env_map = &env_map,
482+
.cwd = tmp_dir_path,
468483
.max_output_bytes = max_doc_file_size,
469484
});
470485
switch (result.term) {
@@ -489,7 +504,7 @@ fn printOutput(
489504
const colored_stderr = try termColor(arena, escaped_stderr);
490505
try shell_out.print("\n{s} ", .{colored_stderr});
491506
} else {
492-
_ = run(arena, &env_map, null, build_args.items) catch fatal("example failed to compile", .{});
507+
_ = run(arena, &env_map, tmp_dir_path, build_args.items) catch fatal("example failed to compile", .{});
493508
}
494509
try shell_out.writeAll("\n");
495510
},
@@ -505,10 +520,7 @@ fn printOutput(
505520

506521
try test_args.appendSlice(&[_][]const u8{
507522
zig_exe, "build-lib",
508-
input_path,
509-
try std.fmt.allocPrint(arena, "-femit-bin={s}{s}{s}", .{
510-
tmp_dir_path, fs.path.sep_str, bin_basename,
511-
}),
523+
input_path, try std.fmt.allocPrint(arena, "-femit-bin={s}", .{bin_basename}),
512524
});
513525
if (opt_zig_lib_dir) |zig_lib_dir| {
514526
try test_args.appendSlice(&.{ "--zig-lib-dir", zig_lib_dir });
@@ -542,7 +554,7 @@ fn printOutput(
542554
try test_args.append(option);
543555
try shell_out.print("{s} ", .{option});
544556
}
545-
const result = run(arena, &env_map, null, test_args.items) catch fatal("test failed", .{});
557+
const result = run(arena, &env_map, tmp_dir_path, test_args.items) catch fatal("test failed", .{});
546558
const escaped_stderr = try escapeHtml(arena, result.stderr);
547559
const escaped_stdout = try escapeHtml(arena, result.stdout);
548560
try shell_out.print("\n{s}{s}\n", .{ escaped_stderr, escaped_stdout });
@@ -1076,7 +1088,7 @@ fn in(slice: []const u8, number: u8) bool {
10761088
fn run(
10771089
allocator: Allocator,
10781090
env_map: *process.EnvMap,
1079-
cwd: ?[]const u8,
1091+
cwd: []const u8,
10801092
args: []const []const u8,
10811093
) !process.Child.RunResult {
10821094
const result = try process.Child.run(.{

0 commit comments

Comments
 (0)