authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-19 22:23:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-10-29 06:20:50-07:00
log97bde94e360cc95206edca478a9dbbc5a0e75264
tree8220ed44c33ff7db9f6d480df2f979fa102252e7
parent6d1b2c7f64fd1a1c671f357cff96b3dd39612857

compiler: upgrade unit tests to new API


2 files changed, 50 insertions(+), 37 deletions(-)

src/Package/Fetch.zig+13-5
......@@ -2069,6 +2069,7 @@ test "tarball with duplicate paths" {
20692069 //
20702070
20712071 const gpa = std.testing.allocator;
2072 const io = std.testing.io;
20722073 var tmp = std.testing.tmpDir(.{});
20732074 defer tmp.cleanup();
20742075
......@@ -2079,7 +2080,7 @@ test "tarball with duplicate paths" {
20792080
20802081 // Run tarball fetch, expect to fail
20812082 var fb: TestFetchBuilder = undefined;
2082 var fetch = try fb.build(gpa, tmp.dir, tarball_path);
2083 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
20832084 defer fb.deinit();
20842085 try std.testing.expectError(error.FetchFailed, fetch.run());
20852086
......@@ -2101,6 +2102,7 @@ test "tarball with excluded duplicate paths" {
21012102 //
21022103
21032104 const gpa = std.testing.allocator;
2105 const io = std.testing.io;
21042106 var tmp = std.testing.tmpDir(.{});
21052107 defer tmp.cleanup();
21062108
......@@ -2111,7 +2113,7 @@ test "tarball with excluded duplicate paths" {
21112113
21122114 // Run tarball fetch, should succeed
21132115 var fb: TestFetchBuilder = undefined;
2114 var fetch = try fb.build(gpa, tmp.dir, tarball_path);
2116 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
21152117 defer fb.deinit();
21162118 try fetch.run();
21172119
......@@ -2145,6 +2147,8 @@ test "tarball without root folder" {
21452147 //
21462148
21472149 const gpa = std.testing.allocator;
2150 const io = std.testing.io;
2151
21482152 var tmp = std.testing.tmpDir(.{});
21492153 defer tmp.cleanup();
21502154
......@@ -2155,7 +2159,7 @@ test "tarball without root folder" {
21552159
21562160 // Run tarball fetch, should succeed
21572161 var fb: TestFetchBuilder = undefined;
2158 var fetch = try fb.build(gpa, tmp.dir, tarball_path);
2162 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
21592163 defer fb.deinit();
21602164 try fetch.run();
21612165
......@@ -2176,6 +2180,8 @@ test "tarball without root folder" {
21762180test "set executable bit based on file content" {
21772181 if (!std.fs.has_executable_bit) return error.SkipZigTest;
21782182 const gpa = std.testing.allocator;
2183 const io = std.testing.io;
2184
21792185 var tmp = std.testing.tmpDir(.{});
21802186 defer tmp.cleanup();
21812187
......@@ -2194,7 +2200,7 @@ test "set executable bit based on file content" {
21942200 // -rwxrwxr-x 17 executables/script
21952201
21962202 var fb: TestFetchBuilder = undefined;
2197 var fetch = try fb.build(gpa, tmp.dir, tarball_path);
2203 var fetch = try fb.build(gpa, io, tmp.dir, tarball_path);
21982204 defer fb.deinit();
21992205
22002206 try fetch.run();
......@@ -2244,13 +2250,14 @@ const TestFetchBuilder = struct {
22442250 fn build(
22452251 self: *TestFetchBuilder,
22462252 allocator: std.mem.Allocator,
2253 io: Io,
22472254 cache_parent_dir: std.fs.Dir,
22482255 path_or_url: []const u8,
22492256 ) !*Fetch {
22502257 const cache_dir = try cache_parent_dir.makeOpenPath("zig-global-cache", .{});
22512258
22522259 try self.thread_pool.init(.{ .allocator = allocator });
2253 self.http_client = .{ .allocator = allocator };
2260 self.http_client = .{ .allocator = allocator, .io = io };
22542261 self.global_cache_directory = .{ .handle = cache_dir, .path = null };
22552262
22562263 self.job_queue = .{
......@@ -2266,6 +2273,7 @@ const TestFetchBuilder = struct {
22662273
22672274 self.fetch = .{
22682275 .arena = std.heap.ArenaAllocator.init(allocator),
2276 .io = io,
22692277 .location = .{ .path_or_url = path_or_url },
22702278 .location_tok = 0,
22712279 .hash_tok = .none,
src/Package/Fetch/git.zig+37-32
......@@ -5,6 +5,7 @@
55//! a package.
66
77const std = @import("std");
8const Io = std.Io;
89const mem = std.mem;
910const testing = std.testing;
1011const Allocator = mem.Allocator;
......@@ -67,8 +68,8 @@ pub const Oid = union(Format) {
6768 };
6869
6970 const Hashing = union(Format) {
70 sha1: std.Io.Writer.Hashing(Sha1),
71 sha256: std.Io.Writer.Hashing(Sha256),
71 sha1: Io.Writer.Hashing(Sha1),
72 sha256: Io.Writer.Hashing(Sha256),
7273
7374 fn init(oid_format: Format, buffer: []u8) Hashing {
7475 return switch (oid_format) {
......@@ -77,7 +78,7 @@ pub const Oid = union(Format) {
7778 };
7879 }
7980
80 fn writer(h: *@This()) *std.Io.Writer {
81 fn writer(h: *@This()) *Io.Writer {
8182 return switch (h.*) {
8283 inline else => |*inner| &inner.writer,
8384 };
......@@ -100,7 +101,7 @@ pub const Oid = union(Format) {
100101 };
101102 }
102103
103 pub fn readBytes(oid_format: Format, reader: *std.Io.Reader) !Oid {
104 pub fn readBytes(oid_format: Format, reader: *Io.Reader) !Oid {
104105 return switch (oid_format) {
105106 inline else => |tag| @unionInit(Oid, @tagName(tag), (try reader.takeArray(tag.byteLength())).*),
106107 };
......@@ -146,7 +147,7 @@ pub const Oid = union(Format) {
146147 } else error.InvalidOid;
147148 }
148149
149 pub fn format(oid: Oid, writer: *std.Io.Writer) std.Io.Writer.Error!void {
150 pub fn format(oid: Oid, writer: *Io.Writer) Io.Writer.Error!void {
150151 try writer.print("{x}", .{oid.slice()});
151152 }
152153
......@@ -594,7 +595,7 @@ pub const Packet = union(enum) {
594595 pub const max_data_length = 65516;
595596
596597 /// Reads a packet in pkt-line format.
597 fn read(reader: *std.Io.Reader) !Packet {
598 fn read(reader: *Io.Reader) !Packet {
598599 const packet: Packet = try .peek(reader);
599600 switch (packet) {
600601 .data => |data| reader.toss(data.len),
......@@ -605,7 +606,7 @@ pub const Packet = union(enum) {
605606
606607 /// Consumes the header of a pkt-line packet and reads any associated data
607608 /// into the reader's buffer, but does not consume the data.
608 fn peek(reader: *std.Io.Reader) !Packet {
609 fn peek(reader: *Io.Reader) !Packet {
609610 const length = std.fmt.parseUnsigned(u16, try reader.take(4), 16) catch return error.InvalidPacket;
610611 switch (length) {
611612 0 => return .flush,
......@@ -618,7 +619,7 @@ pub const Packet = union(enum) {
618619 }
619620
620621 /// Writes a packet in pkt-line format.
621 fn write(packet: Packet, writer: *std.Io.Writer) !void {
622 fn write(packet: Packet, writer: *Io.Writer) !void {
622623 switch (packet) {
623624 .flush => try writer.writeAll("0000"),
624625 .delimiter => try writer.writeAll("0001"),
......@@ -812,7 +813,7 @@ pub const Session = struct {
812813
813814 const CapabilityIterator = struct {
814815 request: std.http.Client.Request,
815 reader: *std.Io.Reader,
816 reader: *Io.Reader,
816817 decompress: std.http.Decompress,
817818
818819 const Capability = struct {
......@@ -869,7 +870,7 @@ pub const Session = struct {
869870 upload_pack_uri.query = null;
870871 upload_pack_uri.fragment = null;
871872
872 var body: std.Io.Writer = .fixed(options.buffer);
873 var body: Io.Writer = .fixed(options.buffer);
873874 try Packet.write(.{ .data = "command=ls-refs\n" }, &body);
874875 if (session.supports_agent) {
875876 try Packet.write(.{ .data = agent_capability }, &body);
......@@ -918,7 +919,7 @@ pub const Session = struct {
918919 pub const RefIterator = struct {
919920 format: Oid.Format,
920921 request: std.http.Client.Request,
921 reader: *std.Io.Reader,
922 reader: *Io.Reader,
922923 decompress: std.http.Decompress,
923924
924925 pub const Ref = struct {
......@@ -986,7 +987,7 @@ pub const Session = struct {
986987 upload_pack_uri.query = null;
987988 upload_pack_uri.fragment = null;
988989
989 var body: std.Io.Writer = .fixed(response_buffer);
990 var body: Io.Writer = .fixed(response_buffer);
990991 try Packet.write(.{ .data = "command=fetch\n" }, &body);
991992 if (session.supports_agent) {
992993 try Packet.write(.{ .data = agent_capability }, &body);
......@@ -1068,8 +1069,8 @@ pub const Session = struct {
10681069
10691070 pub const FetchStream = struct {
10701071 request: std.http.Client.Request,
1071 input: *std.Io.Reader,
1072 reader: std.Io.Reader,
1072 input: *Io.Reader,
1073 reader: Io.Reader,
10731074 err: ?Error = null,
10741075 remaining_len: usize,
10751076 decompress: std.http.Decompress,
......@@ -1094,7 +1095,7 @@ pub const Session = struct {
10941095 _,
10951096 };
10961097
1097 pub fn stream(r: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize {
1098 pub fn stream(r: *Io.Reader, w: *Io.Writer, limit: Io.Limit) Io.Reader.StreamError!usize {
10981099 const fs: *FetchStream = @alignCast(@fieldParentPtr("reader", r));
10991100 const input = fs.input;
11001101 if (fs.remaining_len == 0) {
......@@ -1139,7 +1140,7 @@ const PackHeader = struct {
11391140 const signature = "PACK";
11401141 const supported_version = 2;
11411142
1142 fn read(reader: *std.Io.Reader) !PackHeader {
1143 fn read(reader: *Io.Reader) !PackHeader {
11431144 const actual_signature = reader.take(4) catch |e| switch (e) {
11441145 error.EndOfStream => return error.InvalidHeader,
11451146 else => |other| return other,
......@@ -1202,7 +1203,7 @@ const EntryHeader = union(Type) {
12021203 };
12031204 }
12041205
1205 fn read(format: Oid.Format, reader: *std.Io.Reader) !EntryHeader {
1206 fn read(format: Oid.Format, reader: *Io.Reader) !EntryHeader {
12061207 const InitialByte = packed struct { len: u4, type: u3, has_next: bool };
12071208 const initial: InitialByte = @bitCast(reader.takeByte() catch |e| switch (e) {
12081209 error.EndOfStream => return error.InvalidFormat,
......@@ -1231,7 +1232,7 @@ const EntryHeader = union(Type) {
12311232 }
12321233};
12331234
1234fn readOffsetVarInt(r: *std.Io.Reader) !u64 {
1235fn readOffsetVarInt(r: *Io.Reader) !u64 {
12351236 const Byte = packed struct { value: u7, has_next: bool };
12361237 var b: Byte = @bitCast(try r.takeByte());
12371238 var value: u64 = b.value;
......@@ -1250,7 +1251,7 @@ const IndexHeader = struct {
12501251 const supported_version = 2;
12511252 const size = 4 + 4 + @sizeOf([256]u32);
12521253
1253 fn read(index_header: *IndexHeader, reader: *std.Io.Reader) !void {
1254 fn read(index_header: *IndexHeader, reader: *Io.Reader) !void {
12541255 const sig = try reader.take(4);
12551256 if (!mem.eql(u8, sig, signature)) return error.InvalidHeader;
12561257 const version = try reader.takeInt(u32, .big);
......@@ -1324,7 +1325,7 @@ pub fn indexPack(
13241325 }
13251326 @memset(fan_out_table[fan_out_index..], count);
13261327
1327 var index_hashed_writer = std.Io.Writer.hashed(&index_writer.interface, Oid.Hasher.init(format), &.{});
1328 var index_hashed_writer = Io.Writer.hashed(&index_writer.interface, Oid.Hasher.init(format), &.{});
13281329 const writer = &index_hashed_writer.writer;
13291330 try writer.writeAll(IndexHeader.signature);
13301331 try writer.writeInt(u32, IndexHeader.supported_version, .big);
......@@ -1489,14 +1490,14 @@ fn resolveDeltaChain(
14891490 const delta_header = try EntryHeader.read(format, &pack.interface);
14901491 const delta_data = try readObjectRaw(allocator, &pack.interface, delta_header.uncompressedLength());
14911492 defer allocator.free(delta_data);
1492 var delta_reader: std.Io.Reader = .fixed(delta_data);
1493 var delta_reader: Io.Reader = .fixed(delta_data);
14931494 _ = try delta_reader.takeLeb128(u64); // base object size
14941495 const expanded_size = try delta_reader.takeLeb128(u64);
14951496
14961497 const expanded_alloc_size = std.math.cast(usize, expanded_size) orelse return error.ObjectTooLarge;
14971498 const expanded_data = try allocator.alloc(u8, expanded_alloc_size);
14981499 errdefer allocator.free(expanded_data);
1499 var expanded_delta_stream: std.Io.Writer = .fixed(expanded_data);
1500 var expanded_delta_stream: Io.Writer = .fixed(expanded_data);
15001501 try expandDelta(base_data, &delta_reader, &expanded_delta_stream);
15011502 if (expanded_delta_stream.end != expanded_size) return error.InvalidObject;
15021503
......@@ -1509,9 +1510,9 @@ fn resolveDeltaChain(
15091510/// Reads the complete contents of an object from `reader`. This function may
15101511/// read more bytes than required from `reader`, so the reader position after
15111512/// returning is not reliable.
1512fn readObjectRaw(allocator: Allocator, reader: *std.Io.Reader, size: u64) ![]u8 {
1513fn readObjectRaw(allocator: Allocator, reader: *Io.Reader, size: u64) ![]u8 {
15131514 const alloc_size = std.math.cast(usize, size) orelse return error.ObjectTooLarge;
1514 var aw: std.Io.Writer.Allocating = .init(allocator);
1515 var aw: Io.Writer.Allocating = .init(allocator);
15151516 try aw.ensureTotalCapacity(alloc_size + std.compress.flate.max_window_len);
15161517 defer aw.deinit();
15171518 var decompress: std.compress.flate.Decompress = .init(reader, .zlib, &.{});
......@@ -1523,7 +1524,7 @@ fn readObjectRaw(allocator: Allocator, reader: *std.Io.Reader, size: u64) ![]u8
15231524///
15241525/// The format of the delta data is documented in
15251526/// [pack-format](https://git-scm.com/docs/pack-format).
1526fn expandDelta(base_object: []const u8, delta_reader: *std.Io.Reader, writer: *std.Io.Writer) !void {
1527fn expandDelta(base_object: []const u8, delta_reader: *Io.Reader, writer: *Io.Writer) !void {
15271528 while (true) {
15281529 const inst: packed struct { value: u7, copy: bool } = @bitCast(delta_reader.takeByte() catch |e| switch (e) {
15291530 error.EndOfStream => return,
......@@ -1576,7 +1577,7 @@ fn expandDelta(base_object: []const u8, delta_reader: *std.Io.Reader, writer: *s
15761577/// - SHA-1: `dd582c0720819ab7130b103635bd7271b9fd4feb`
15771578/// - SHA-256: `7f444a92bd4572ee4a28b2c63059924a9ca1829138553ef3e7c41ee159afae7a`
15781579/// 4. `git checkout $commit`
1579fn runRepositoryTest(comptime format: Oid.Format, head_commit: []const u8) !void {
1580fn runRepositoryTest(io: Io, comptime format: Oid.Format, head_commit: []const u8) !void {
15801581 const testrepo_pack = @embedFile("git/testdata/testrepo-" ++ @tagName(format) ++ ".pack");
15811582
15821583 var git_dir = testing.tmpDir(.{});
......@@ -1586,7 +1587,7 @@ fn runRepositoryTest(comptime format: Oid.Format, head_commit: []const u8) !void
15861587 try pack_file.writeAll(testrepo_pack);
15871588
15881589 var pack_file_buffer: [2000]u8 = undefined;
1589 var pack_file_reader = pack_file.reader(&pack_file_buffer);
1590 var pack_file_reader = pack_file.reader(io, &pack_file_buffer);
15901591
15911592 var index_file = try git_dir.dir.createFile("testrepo.idx", .{ .read = true });
15921593 defer index_file.close();
......@@ -1608,7 +1609,7 @@ fn runRepositoryTest(comptime format: Oid.Format, head_commit: []const u8) !void
16081609 try testing.expectEqualSlices(u8, testrepo_idx, index_file_data);
16091610 }
16101611
1611 var index_file_reader = index_file.reader(&index_file_buffer);
1612 var index_file_reader = index_file.reader(io, &index_file_buffer);
16121613 var repository: Repository = undefined;
16131614 try repository.init(testing.allocator, format, &pack_file_reader, &index_file_reader);
16141615 defer repository.deinit();
......@@ -1687,11 +1688,11 @@ fn runRepositoryTest(comptime format: Oid.Format, head_commit: []const u8) !void
16871688const skip_checksums = true;
16881689
16891690test "SHA-1 packfile indexing and checkout" {
1690 try runRepositoryTest(.sha1, "dd582c0720819ab7130b103635bd7271b9fd4feb");
1691 try runRepositoryTest(std.testing.io, .sha1, "dd582c0720819ab7130b103635bd7271b9fd4feb");
16911692}
16921693
16931694test "SHA-256 packfile indexing and checkout" {
1694 try runRepositoryTest(.sha256, "7f444a92bd4572ee4a28b2c63059924a9ca1829138553ef3e7c41ee159afae7a");
1695 try runRepositoryTest(std.testing.io, .sha256, "7f444a92bd4572ee4a28b2c63059924a9ca1829138553ef3e7c41ee159afae7a");
16951696}
16961697
16971698/// Checks out a commit of a packfile. Intended for experimenting with and
......@@ -1699,6 +1700,10 @@ test "SHA-256 packfile indexing and checkout" {
16991700pub fn main() !void {
17001701 const allocator = std.heap.smp_allocator;
17011702
1703 var threaded: Io.Threaded = .init(allocator);
1704 defer threaded.deinit();
1705 const io = threaded.io();
1706
17021707 const args = try std.process.argsAlloc(allocator);
17031708 defer std.process.argsFree(allocator, args);
17041709 if (args.len != 5) {
......@@ -1710,7 +1715,7 @@ pub fn main() !void {
17101715 var pack_file = try std.fs.cwd().openFile(args[2], .{});
17111716 defer pack_file.close();
17121717 var pack_file_buffer: [4096]u8 = undefined;
1713 var pack_file_reader = pack_file.reader(&pack_file_buffer);
1718 var pack_file_reader = pack_file.reader(io, &pack_file_buffer);
17141719
17151720 const commit = try Oid.parse(format, args[3]);
17161721 var worktree = try std.fs.cwd().makeOpenPath(args[4], .{});
......@@ -1727,7 +1732,7 @@ pub fn main() !void {
17271732 try indexPack(allocator, format, &pack_file_reader, &index_file_writer);
17281733
17291734 std.debug.print("Starting checkout...\n", .{});
1730 var index_file_reader = index_file.reader(&index_file_buffer);
1735 var index_file_reader = index_file.reader(io, &index_file_buffer);
17311736 var repository: Repository = undefined;
17321737 try repository.init(allocator, format, &pack_file_reader, &index_file_reader);
17331738 defer repository.deinit();