authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-07 01:05:29+02:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-09 15:00:21+02:00
logdc61c2e9043f89fcfe540431131563a7269167b5
tree2b6ec139e9e6edbbb029f6ffbfc3ba1e82900535
parent373d48212f91ab1fd345da49f8a3c737075274d4

add comments


1 files changed, 24 insertions(+), 6 deletions(-)

src/Package/Fetch.zig+24-6
......@@ -1744,6 +1744,9 @@ test FileHeader {
17441744 try std.testing.expect(h.isExecutable());
17451745}
17461746
1747// Result of the `unpackResource` operation. Enables collecting errors from
1748// tar/git diagnostic, filtering that errors by manifest inclusion rules and
1749// emitting remaining errors to an `ErrorBundle`.
17471750const UnpackResult = struct {
17481751 allocator: std.mem.Allocator,
17491752 errors: std.ArrayListUnmanaged(Error) = .{},
......@@ -1833,6 +1836,7 @@ const UnpackResult = struct {
18331836 } });
18341837 }
18351838
1839 // Filter errors by manifest inclusion rules.
18361840 fn filterErrors(self: *UnpackResult, filter: Filter) !void {
18371841 var i = self.errors.items.len;
18381842 const root_dir: []const u8 = if (self.root_dir) |root_dir| root_dir else "";
......@@ -1850,11 +1854,15 @@ const UnpackResult = struct {
18501854 self.root_error_message = try self.allocator.dupe(u8, msg);
18511855 }
18521856
1857 // Emmit errors to an `ErrorBundle`.
18531858 fn bundleErrors(
18541859 self: *UnpackResult,
18551860 eb: *ErrorBundle.Wip,
18561861 src_loc: ErrorBundle.SourceLocationIndex,
18571862 ) !void {
1863 if (self.errors.items.len == 0 and self.root_error_message.len == 0)
1864 return;
1865
18581866 const notes_len: u32 = @intCast(self.errors.items.len);
18591867 try eb.addRootErrorMessage(.{
18601868 .msg = try eb.addString(self.root_error_message),
......@@ -1941,16 +1949,21 @@ test "tarball with error paths excluded" {
19411949 try fb.expectPackageFiles(expected_files);
19421950}
19431951
1952// Builds Fetch with required dependencies, clears dependencies on deinit().
19441953const TestFetchBuilder = struct {
19451954 thread_pool: ThreadPool,
19461955 http_client: std.http.Client,
19471956 global_cache_directory: Cache.Directory,
19481957 progress: std.Progress,
1949 root_prog_node: *std.Progress.Node,
19501958 job_queue: Fetch.JobQueue,
19511959 fetch: Fetch,
19521960
1953 fn build(self: *TestFetchBuilder, allocator: std.mem.Allocator, cache_parent_dir: std.fs.Dir, path_or_url: []const u8) !*Fetch {
1961 fn build(
1962 self: *TestFetchBuilder,
1963 allocator: std.mem.Allocator,
1964 cache_parent_dir: std.fs.Dir,
1965 path_or_url: []const u8,
1966 ) !*Fetch {
19541967 const cache_dir = try cache_parent_dir.makeOpenPath("zig-global-cache", .{});
19551968
19561969 try self.thread_pool.init(.{ .allocator = allocator });
......@@ -1958,7 +1971,6 @@ const TestFetchBuilder = struct {
19581971 self.global_cache_directory = .{ .handle = cache_dir, .path = null };
19591972
19601973 self.progress = .{ .dont_print_on_dumb = true };
1961 self.root_prog_node = self.progress.start("Fetch", 0);
19621974
19631975 self.job_queue = .{
19641976 .http_client = &self.http_client,
......@@ -1979,7 +1991,7 @@ const TestFetchBuilder = struct {
19791991 .lazy_status = .eager,
19801992 .parent_package_root = Cache.Path{ .root_dir = undefined },
19811993 .parent_manifest_ast = null,
1982 .prog_node = self.root_prog_node,
1994 .prog_node = self.progress.start("Fetch", 0),
19831995 .job_queue = &self.job_queue,
19841996 .omit_missing_hash_error = true,
19851997 .allow_missing_paths_field = false,
......@@ -1991,7 +2003,6 @@ const TestFetchBuilder = struct {
19912003 .actual_hash = undefined,
19922004 .has_build_zig = false,
19932005 .oom_flag = false,
1994
19952006 .module = null,
19962007 };
19972008 return &self.fetch;
......@@ -2000,7 +2011,7 @@ const TestFetchBuilder = struct {
20002011 fn deinit(self: *TestFetchBuilder) void {
20012012 self.fetch.deinit();
20022013 self.job_queue.deinit();
2003 self.root_prog_node.end();
2014 self.fetch.prog_node.end();
20042015 self.global_cache_directory.handle.close();
20052016 self.http_client.deinit();
20062017 self.thread_pool.deinit();
......@@ -2011,6 +2022,8 @@ const TestFetchBuilder = struct {
20112022 return try root.root_dir.handle.openDir(root.sub_path, .{ .iterate = true });
20122023 }
20132024
2025 // Test helper, asserts thet package dir constains expected_files.
2026 // expected_files must be sorted.
20142027 fn expectPackageFiles(self: *TestFetchBuilder, expected_files: []const []const u8) !void {
20152028 var package_dir = try self.packageDir();
20162029 defer package_dir.close();
......@@ -2041,6 +2054,7 @@ const TestFetchBuilder = struct {
20412054 try std.testing.expectEqualDeep(expected_files, actual_files.items);
20422055 }
20432056
2057 // Test helper, asserts that fetch has failed with `msg` error message.
20442058 fn expectFetchErrors(self: *TestFetchBuilder, notes_len: usize, msg: []const u8) !void {
20452059 var errors = try self.fetch.error_bundle.toOwnedBundle("");
20462060 defer errors.deinit(std.testing.allocator);
......@@ -2057,6 +2071,10 @@ const TestFetchBuilder = struct {
20572071 }
20582072};
20592073
2074// Creates tarball with duplicate files names. Simulating case collisions on
2075// case insensitive file system without use of that kind of the file system.
2076// Manifest will exclude those files, so adding manifest should remove duplicate
2077// files problem.
20602078fn createTestTarball(dir: fs.Dir, tarball_name: []const u8, with_manifest: bool) !void {
20612079 const file = try dir.createFile(tarball_name, .{});
20622080 defer file.close();