test-standalone: update the rest of the cases to new API

This commit is contained in:
Andrew Kelley
2026-01-02 18:46:25 -08:00
parent e149c0e2aa
commit 4afed3e9ef
8 changed files with 52 additions and 65 deletions
+4 -14
View File
@@ -17,20 +17,10 @@ var stdout_buffer: [1024]u8 = undefined;
var input_buffer: [1024]u8 = undefined;
var output_buffer: [1024]u8 = undefined;
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = arena_instance.allocator();
var general_purpose_allocator: std.heap.GeneralPurposeAllocator(.{}) = .init;
const gpa = general_purpose_allocator.allocator();
var threaded: std.Io.Threaded = .init(gpa, .{});
defer threaded.deinit();
const io = threaded.io();
const args = try std.process.argsAlloc(arena);
return cmdObjCopy(arena, io, args[1..]);
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);
return cmdObjCopy(arena, init.io, args[1..]);
}
fn cmdObjCopy(arena: Allocator, io: Io, args: []const []const u8) !void {
+8 -2
View File
@@ -9,6 +9,12 @@ pub fn main(init: std.process.Init.Minimal) !void {
};
const gpa = gpa_state.allocator();
const process_cwd_path = try std.process.getCwdAlloc(gpa);
defer gpa.free(process_cwd_path);
var env_map = try init.environ.createMap(gpa);
defer env_map.deinit();
var it = try init.args.iterateAllocator(gpa);
defer it.deinit();
_ = it.next() orelse unreachable; // skip binary name
@@ -17,7 +23,7 @@ pub fn main(init: std.process.Init.Minimal) !void {
const cwd_path = it.next() orelse break :child_path .{ child_path, false };
// If there is a third argument, it is the current CWD somewhere within the cache directory.
// In that case, modify the child path in order to test spawning a path with a leading `..` component.
break :child_path .{ try std.fs.path.relative(gpa, cwd_path, child_path), true };
break :child_path .{ try std.fs.path.relative(gpa, process_cwd_path, &env_map, cwd_path, child_path), true };
};
defer if (needs_free) gpa.free(child_path);
@@ -28,7 +34,7 @@ pub fn main(init: std.process.Init.Minimal) !void {
defer threaded.deinit();
const io = threaded.io();
var child = try std.process.spawn(.{
var child = try std.process.spawn(io, .{
.argv = &.{ child_path, "hello arg" },
.stdin = .pipe,
.stdout = .pipe,
+1 -1
View File
@@ -1,5 +1,5 @@
const std = @import("std");
pub fn main(init: std.process.Init) !void {
try std.testing.expect(init.env_map.count() == 0);
try std.testing.expectEqual(0, init.env_map.count());
}
+5 -5
View File
@@ -104,20 +104,20 @@ pub fn main(init: std.process.Init) !void {
// getAlloc
{
try std.testing.expectEqualSlices(u8, "123", try environ.getAlloc(arena, "FOO"));
try std.testing.expectError(error.EnvironmentVariableNotFound, environ.getAlloc(arena, "FOO="));
try std.testing.expectError(error.EnvironmentVariableNotFound, environ.getAlloc(arena, "FO"));
try std.testing.expectError(error.EnvironmentVariableNotFound, environ.getAlloc(arena, "FOOO"));
try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FOO="));
try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FO"));
try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "FOOO"));
if (builtin.os.tag == .windows) {
try std.testing.expectEqualSlices(u8, "123", try environ.getAlloc(arena, "foo"));
}
try std.testing.expectEqualSlices(u8, "ABC=123", try environ.getAlloc(arena, "EQUALS"));
try std.testing.expectError(error.EnvironmentVariableNotFound, environ.getAlloc(arena, "EQUALS=ABC"));
try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "EQUALS=ABC"));
try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try environ.getAlloc(arena, "КИРиллИЦА"));
if (builtin.os.tag == .windows) {
try std.testing.expectEqualSlices(u8, "non-ascii አማርኛ \u{10FFFF}", try environ.getAlloc(arena, "кирИЛЛица"));
}
try std.testing.expectEqualSlices(u8, "", try environ.getAlloc(arena, "NO_VALUE"));
try std.testing.expectError(error.EnvironmentVariableNotFound, environ.getAlloc(arena, "NOT_SET"));
try std.testing.expectError(error.EnvironmentVariableMissing, environ.getAlloc(arena, "NOT_SET"));
if (builtin.os.tag == .windows) {
try std.testing.expectEqualSlices(u8, "hi", try environ.getAlloc(arena, "=HIDDEN"));
try std.testing.expectEqualSlices(u8, "\xed\xa0\x80", try environ.getAlloc(arena, "INVALID_UTF16_\xed\xa0\x80"));
@@ -9,7 +9,7 @@ pub fn main(init: std.process.Init) !void {
const exe_path = it.next() orelse unreachable;
const symlink_path = it.next() orelse unreachable;
const cwd = std.process.getCwdAlloc(init.arena.allocator());
const cwd = try std.process.getCwdAlloc(init.arena.allocator());
// If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`.
const exe_rel_path = try std.fs.path.relative(gpa, cwd, init.env_map, std.fs.path.dirname(symlink_path) orelse ".", exe_path);
+9 -16
View File
@@ -6,13 +6,9 @@ const process = std.process;
const assert = std.debug.assert;
const fatal = std.process.fatal;
const info = std.log.info;
const Allocator = mem.Allocator;
const Allocator = std.mem.Allocator;
const OsTag = std.Target.Os.Tag;
var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = general_purpose_allocator.allocator();
const Arch = enum {
aarch64,
x86_64,
@@ -67,10 +63,11 @@ const usage =
;
pub fn main(init: std.process.Init) !void {
const allocator = init.arena;
const args = try init.minimal.args.toSlice(allocator);
const io = init.io;
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);
var argv = std.array_list.Managed([]const u8).init(allocator);
var argv = std.array_list.Managed([]const u8).init(arena);
var sysroot: ?[]const u8 = null;
var args_iter = ArgsIterator{ .args = args[1..] };
@@ -82,23 +79,19 @@ pub fn main(init: std.process.Init) !void {
} else try argv.append(arg);
}
var threaded: Io.Threaded = .init(gpa, .{});
defer threaded.deinit();
const io = threaded.io();
const sysroot_path = sysroot orelse blk: {
const target = try std.zig.system.resolveTargetQuery(io, .{});
break :blk std.zig.system.darwin.getSdk(allocator, io, &target) orelse
break :blk std.zig.system.darwin.getSdk(arena, io, &target) orelse
fatal("no SDK found; you can provide one explicitly with '--sysroot' flag", .{});
};
var sdk_dir = try Dir.cwd().openDir(io, sysroot_path, .{});
defer sdk_dir.close(io);
const sdk_info = try sdk_dir.readFileAlloc(io, "SDKSettings.json", allocator, .limited(std.math.maxInt(u32)));
const sdk_info = try sdk_dir.readFileAlloc(io, "SDKSettings.json", arena, .limited(std.math.maxInt(u32)));
const parsed_json = try std.json.parseFromSlice(struct {
DefaultProperties: struct { MACOSX_DEPLOYMENT_TARGET: []const u8 },
}, allocator, sdk_info, .{ .ignore_unknown_fields = true });
}, arena, sdk_info, .{ .ignore_unknown_fields = true });
const version = Version.parse(parsed_json.value.DefaultProperties.MACOSX_DEPLOYMENT_TARGET) orelse
fatal("don't know how to parse SDK version: {s}", .{
@@ -114,7 +107,7 @@ pub fn main(init: std.process.Init) !void {
.arch = arch,
.os_ver = os_ver,
};
try fetchTarget(allocator, io, argv.items, sysroot_path, target, version, tmp_dir);
try fetchTarget(arena, io, argv.items, sysroot_path, target, version, tmp_dir);
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ const usage =
;
pub fn main(init: std.process.Init) !void {
const arena = init.arena;
const arena = init.arena.allocator();
const io = init.io;
const args = try init.minimal.args.toSlice(arena);
+23 -25
View File
@@ -127,16 +127,14 @@ const LibCVendor = enum {
netbsd,
};
pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const allocator = arena.allocator();
pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const args = try init.minimal.args.toSlice(arena);
const cwd_path = try std.process.getCwdAlloc(arena);
const env_map = init.env_map;
var threaded: Io.Threaded = .init(allocator, .{});
defer threaded.deinit();
const io = threaded.io();
const args = try std.process.argsAlloc(allocator);
var search_paths = std.array_list.Managed([]const u8).init(allocator);
var search_paths = std.array_list.Managed([]const u8).init(arena);
var opt_out_dir: ?[]const u8 = null;
var opt_abi: ?[]const u8 = null;
@@ -172,7 +170,7 @@ pub fn main() !void {
usageAndExit(args[0]);
};
const generic_name = try std.fmt.allocPrint(allocator, "generic-{s}", .{abi_name});
const generic_name = try std.fmt.allocPrint(arena, "generic-{s}", .{abi_name});
const libc_targets = switch (vendor) {
.glibc => &glibc_targets,
.musl => &musl_targets,
@@ -180,8 +178,8 @@ pub fn main() !void {
.netbsd => &netbsd_targets,
};
var path_table = PathTable.init(allocator);
var hash_to_contents = HashToContents.init(allocator);
var path_table = PathTable.init(arena);
var hash_to_contents = HashToContents.init(arena);
var max_bytes_saved: usize = 0;
var total_bytes: usize = 0;
@@ -189,7 +187,7 @@ pub fn main() !void {
for (libc_targets) |libc_target| {
const libc_dir = switch (vendor) {
.glibc => try std.zig.target.glibcRuntimeTriple(allocator, libc_target.arch, .linux, libc_target.abi),
.glibc => try std.zig.target.glibcRuntimeTriple(arena, libc_target.arch, .linux, libc_target.abi),
.musl => std.zig.target.muslArchName(libc_target.arch, libc_target.abi),
.freebsd => switch (libc_target.arch) {
.arm => "armv7",
@@ -221,7 +219,7 @@ pub fn main() !void {
},
};
const dest_target = if (libc_target.dest) |dest| dest else try std.fmt.allocPrint(allocator, "{s}-{s}-{s}", .{
const dest_target = if (libc_target.dest) |dest| dest else try std.fmt.allocPrint(arena, "{s}-{s}-{s}", .{
@tagName(libc_target.arch),
switch (vendor) {
.musl, .glibc => "linux",
@@ -239,8 +237,8 @@ pub fn main() !void {
=> &[_][]const u8{ search_path, libc_dir, "usr", "include" },
.musl => &[_][]const u8{ search_path, libc_dir, "usr", "local", "musl", "include" },
};
const target_include_dir = try Dir.path.join(allocator, sub_path);
var dir_stack = std.array_list.Managed([]const u8).init(allocator);
const target_include_dir = try Dir.path.join(arena, sub_path);
var dir_stack = std.array_list.Managed([]const u8).init(arena);
try dir_stack.append(target_include_dir);
while (dir_stack.pop()) |full_dir_name| {
@@ -254,16 +252,16 @@ pub fn main() !void {
var dir_it = dir.iterate();
while (try dir_it.next(io)) |entry| {
const full_path = try Dir.path.join(allocator, &[_][]const u8{ full_dir_name, entry.name });
const full_path = try Dir.path.join(arena, &[_][]const u8{ full_dir_name, entry.name });
switch (entry.kind) {
.directory => try dir_stack.append(full_path),
.file, .sym_link => {
const rel_path = try Dir.path.relative(allocator, target_include_dir, full_path);
const rel_path = try Dir.path.relative(arena, cwd_path, env_map, target_include_dir, full_path);
const max_size = 2 * 1024 * 1024 * 1024;
const raw_bytes = try Dir.cwd().readFileAlloc(io, full_path, allocator, .limited(max_size));
const raw_bytes = try Dir.cwd().readFileAlloc(io, full_path, arena, .limited(max_size));
const trimmed = std.mem.trim(u8, raw_bytes, " \r\n\t");
total_bytes += raw_bytes.len;
const hash = try allocator.alloc(u8, 32);
const hash = try arena.alloc(u8, 32);
hasher = Blake3.init(.{});
hasher.update(rel_path);
hasher.update(trimmed);
@@ -285,8 +283,8 @@ pub fn main() !void {
}
const path_gop = try path_table.getOrPut(rel_path);
const target_to_hash = if (path_gop.found_existing) path_gop.value_ptr.* else blk: {
const ptr = try allocator.create(TargetToHash);
ptr.* = TargetToHash.init(allocator);
const ptr = try arena.create(TargetToHash);
ptr.* = TargetToHash.init(arena);
path_gop.value_ptr.* = ptr;
break :blk ptr;
};
@@ -327,7 +325,7 @@ pub fn main() !void {
// gets their header in a separate arch directory.
var path_it = path_table.iterator();
while (path_it.next()) |path_kv| {
var contents_list = std.array_list.Managed(*Contents).init(allocator);
var contents_list = std.array_list.Managed(*Contents).init(arena);
{
var hash_it = path_kv.value_ptr.*.iterator();
while (hash_it.next()) |hash_kv| {
@@ -339,7 +337,7 @@ pub fn main() !void {
const best_contents = contents_list.pop().?;
if (best_contents.hit_count > 1) {
// worth it to make it generic
const full_path = try Dir.path.join(allocator, &[_][]const u8{ out_dir, generic_name, path_kv.key_ptr.* });
const full_path = try Dir.path.join(arena, &[_][]const u8{ out_dir, generic_name, path_kv.key_ptr.* });
try Dir.cwd().createDirPath(io, Dir.path.dirname(full_path).?);
try Dir.cwd().writeFile(io, .{ .sub_path = full_path, .data = best_contents.bytes });
best_contents.is_generic = true;
@@ -360,7 +358,7 @@ pub fn main() !void {
if (contents.is_generic) continue;
const dest_target = hash_kv.key_ptr.*;
const full_path = try Dir.path.join(allocator, &[_][]const u8{ out_dir, dest_target, path_kv.key_ptr.* });
const full_path = try Dir.path.join(arena, &[_][]const u8{ out_dir, dest_target, path_kv.key_ptr.* });
try Dir.cwd().createDirPath(io, Dir.path.dirname(full_path).?);
try Dir.cwd().writeFile(io, .{ .sub_path = full_path, .data = contents.bytes });
}