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 {...@@ -1744,6 +1744,9 @@ test FileHeader {
1744 try std.testing.expect(h.isExecutable());1744 try std.testing.expect(h.isExecutable());
1745}1745}
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`.
1747const UnpackResult = struct {1750const UnpackResult = struct {
1748 allocator: std.mem.Allocator,1751 allocator: std.mem.Allocator,
1749 errors: std.ArrayListUnmanaged(Error) = .{},1752 errors: std.ArrayListUnmanaged(Error) = .{},
...@@ -1833,6 +1836,7 @@ const UnpackResult = struct {...@@ -1833,6 +1836,7 @@ const UnpackResult = struct {
1833 } });1836 } });
1834 }1837 }
18351838
1839 // Filter errors by manifest inclusion rules.
1836 fn filterErrors(self: *UnpackResult, filter: Filter) !void {1840 fn filterErrors(self: *UnpackResult, filter: Filter) !void {
1837 var i = self.errors.items.len;1841 var i = self.errors.items.len;
1838 const root_dir: []const u8 = if (self.root_dir) |root_dir| root_dir else "";1842 const root_dir: []const u8 = if (self.root_dir) |root_dir| root_dir else "";
...@@ -1850,11 +1854,15 @@ const UnpackResult = struct {...@@ -1850,11 +1854,15 @@ const UnpackResult = struct {
1850 self.root_error_message = try self.allocator.dupe(u8, msg);1854 self.root_error_message = try self.allocator.dupe(u8, msg);
1851 }1855 }
18521856
1857 // Emmit errors to an `ErrorBundle`.
1853 fn bundleErrors(1858 fn bundleErrors(
1854 self: *UnpackResult,1859 self: *UnpackResult,
1855 eb: *ErrorBundle.Wip,1860 eb: *ErrorBundle.Wip,
1856 src_loc: ErrorBundle.SourceLocationIndex,1861 src_loc: ErrorBundle.SourceLocationIndex,
1857 ) !void {1862 ) !void {
1863 if (self.errors.items.len == 0 and self.root_error_message.len == 0)
1864 return;
1865
1858 const notes_len: u32 = @intCast(self.errors.items.len);1866 const notes_len: u32 = @intCast(self.errors.items.len);
1859 try eb.addRootErrorMessage(.{1867 try eb.addRootErrorMessage(.{
1860 .msg = try eb.addString(self.root_error_message),1868 .msg = try eb.addString(self.root_error_message),
...@@ -1941,16 +1949,21 @@ test "tarball with error paths excluded" {...@@ -1941,16 +1949,21 @@ test "tarball with error paths excluded" {
1941 try fb.expectPackageFiles(expected_files);1949 try fb.expectPackageFiles(expected_files);
1942}1950}
19431951
1952// Builds Fetch with required dependencies, clears dependencies on deinit().
1944const TestFetchBuilder = struct {1953const TestFetchBuilder = struct {
1945 thread_pool: ThreadPool,1954 thread_pool: ThreadPool,
1946 http_client: std.http.Client,1955 http_client: std.http.Client,
1947 global_cache_directory: Cache.Directory,1956 global_cache_directory: Cache.Directory,
1948 progress: std.Progress,1957 progress: std.Progress,
1949 root_prog_node: *std.Progress.Node,
1950 job_queue: Fetch.JobQueue,1958 job_queue: Fetch.JobQueue,
1951 fetch: Fetch,1959 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 {
1954 const cache_dir = try cache_parent_dir.makeOpenPath("zig-global-cache", .{});1967 const cache_dir = try cache_parent_dir.makeOpenPath("zig-global-cache", .{});
19551968
1956 try self.thread_pool.init(.{ .allocator = allocator });1969 try self.thread_pool.init(.{ .allocator = allocator });
...@@ -1958,7 +1971,6 @@ const TestFetchBuilder = struct {...@@ -1958,7 +1971,6 @@ const TestFetchBuilder = struct {
1958 self.global_cache_directory = .{ .handle = cache_dir, .path = null };1971 self.global_cache_directory = .{ .handle = cache_dir, .path = null };
19591972
1960 self.progress = .{ .dont_print_on_dumb = true };1973 self.progress = .{ .dont_print_on_dumb = true };
1961 self.root_prog_node = self.progress.start("Fetch", 0);
19621974
1963 self.job_queue = .{1975 self.job_queue = .{
1964 .http_client = &self.http_client,1976 .http_client = &self.http_client,
...@@ -1979,7 +1991,7 @@ const TestFetchBuilder = struct {...@@ -1979,7 +1991,7 @@ const TestFetchBuilder = struct {
1979 .lazy_status = .eager,1991 .lazy_status = .eager,
1980 .parent_package_root = Cache.Path{ .root_dir = undefined },1992 .parent_package_root = Cache.Path{ .root_dir = undefined },
1981 .parent_manifest_ast = null,1993 .parent_manifest_ast = null,
1982 .prog_node = self.root_prog_node,1994 .prog_node = self.progress.start("Fetch", 0),
1983 .job_queue = &self.job_queue,1995 .job_queue = &self.job_queue,
1984 .omit_missing_hash_error = true,1996 .omit_missing_hash_error = true,
1985 .allow_missing_paths_field = false,1997 .allow_missing_paths_field = false,
...@@ -1991,7 +2003,6 @@ const TestFetchBuilder = struct {...@@ -1991,7 +2003,6 @@ const TestFetchBuilder = struct {
1991 .actual_hash = undefined,2003 .actual_hash = undefined,
1992 .has_build_zig = false,2004 .has_build_zig = false,
1993 .oom_flag = false,2005 .oom_flag = false,
1994
1995 .module = null,2006 .module = null,
1996 };2007 };
1997 return &self.fetch;2008 return &self.fetch;
...@@ -2000,7 +2011,7 @@ const TestFetchBuilder = struct {...@@ -2000,7 +2011,7 @@ const TestFetchBuilder = struct {
2000 fn deinit(self: *TestFetchBuilder) void {2011 fn deinit(self: *TestFetchBuilder) void {
2001 self.fetch.deinit();2012 self.fetch.deinit();
2002 self.job_queue.deinit();2013 self.job_queue.deinit();
2003 self.root_prog_node.end();2014 self.fetch.prog_node.end();
2004 self.global_cache_directory.handle.close();2015 self.global_cache_directory.handle.close();
2005 self.http_client.deinit();2016 self.http_client.deinit();
2006 self.thread_pool.deinit();2017 self.thread_pool.deinit();
...@@ -2011,6 +2022,8 @@ const TestFetchBuilder = struct {...@@ -2011,6 +2022,8 @@ const TestFetchBuilder = struct {
2011 return try root.root_dir.handle.openDir(root.sub_path, .{ .iterate = true });2022 return try root.root_dir.handle.openDir(root.sub_path, .{ .iterate = true });
2012 }2023 }
20132024
2025 // Test helper, asserts thet package dir constains expected_files.
2026 // expected_files must be sorted.
2014 fn expectPackageFiles(self: *TestFetchBuilder, expected_files: []const []const u8) !void {2027 fn expectPackageFiles(self: *TestFetchBuilder, expected_files: []const []const u8) !void {
2015 var package_dir = try self.packageDir();2028 var package_dir = try self.packageDir();
2016 defer package_dir.close();2029 defer package_dir.close();
...@@ -2041,6 +2054,7 @@ const TestFetchBuilder = struct {...@@ -2041,6 +2054,7 @@ const TestFetchBuilder = struct {
2041 try std.testing.expectEqualDeep(expected_files, actual_files.items);2054 try std.testing.expectEqualDeep(expected_files, actual_files.items);
2042 }2055 }
20432056
2057 // Test helper, asserts that fetch has failed with `msg` error message.
2044 fn expectFetchErrors(self: *TestFetchBuilder, notes_len: usize, msg: []const u8) !void {2058 fn expectFetchErrors(self: *TestFetchBuilder, notes_len: usize, msg: []const u8) !void {
2045 var errors = try self.fetch.error_bundle.toOwnedBundle("");2059 var errors = try self.fetch.error_bundle.toOwnedBundle("");
2046 defer errors.deinit(std.testing.allocator);2060 defer errors.deinit(std.testing.allocator);
...@@ -2057,6 +2071,10 @@ const TestFetchBuilder = struct {...@@ -2057,6 +2071,10 @@ const TestFetchBuilder = struct {
2057 }2071 }
2058};2072};
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.
2060fn createTestTarball(dir: fs.Dir, tarball_name: []const u8, with_manifest: bool) !void {2078fn createTestTarball(dir: fs.Dir, tarball_name: []const u8, with_manifest: bool) !void {
2061 const file = try dir.createFile(tarball_name, .{});2079 const file = try dir.createFile(tarball_name, .{});
2062 defer file.close();2080 defer file.close();