authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-07 01:02:37+02:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-04-09 15:00:21+02:00
log9e47857ae9340ecd77f73c3c22ae04c938d28ca7
treee31ff2ffda2720b9f700d9c5958e6e55061071ee
parentac21ade667f0f42b8b1aec5831cbc99cbaed8565

fetch: make failing test


1 files changed, 131 insertions(+), 1 deletions(-)

src/Package/Fetch.zig+131-1
...@@ -1735,7 +1735,7 @@ const ThreadPool = std.Thread.Pool;...@@ -1735,7 +1735,7 @@ const ThreadPool = std.Thread.Pool;
1735const WaitGroup = std.Thread.WaitGroup;1735const WaitGroup = std.Thread.WaitGroup;
1736const Fetch = @This();1736const Fetch = @This();
1737const git = @import("Fetch/git.zig");1737const git = @import("Fetch/git.zig");
1738const Package = @import("../Package.zig");1738//const Package = @import("../Package.zig");
1739const Manifest = Package.Manifest;1739const Manifest = Package.Manifest;
1740const ErrorBundle = std.zig.ErrorBundle;1740const ErrorBundle = std.zig.ErrorBundle;
1741const native_os = builtin.os.tag;1741const native_os = builtin.os.tag;
...@@ -1778,3 +1778,133 @@ test FileHeader {...@@ -1778,3 +1778,133 @@ test FileHeader {
1778 h.update(FileHeader.elf_magic[2..4]);1778 h.update(FileHeader.elf_magic[2..4]);
1779 try std.testing.expect(h.isExecutable());1779 try std.testing.expect(h.isExecutable());
1780}1780}
1781
1782// Removing dependencies
1783const Package = struct {
1784 const build_zig_basename = "build.zig";
1785 const Module = struct {};
1786 const Manifest = @import("Manifest.zig");
1787};
1788
1789test "fetch tarball" {
1790 const testing = std.testing;
1791 const gpa = testing.allocator;
1792
1793 var cache_tmp = std.testing.tmpDir(.{});
1794 defer cache_tmp.cleanup();
1795 const global_cache_directory_path = try cache_tmp.dir.realpathAlloc(gpa, ".");
1796 defer gpa.free(global_cache_directory_path);
1797
1798 const paths: []const []const u8 = &.{
1799 "main.zig",
1800 "src/root.zig",
1801 // duplicate file paths
1802 "dir/file",
1803 "dir1/file1",
1804 "dir/file",
1805 "dir1/file1",
1806 };
1807 var tmp = std.testing.tmpDir(.{});
1808 defer tmp.cleanup();
1809 const file = try tmp.dir.createFile("package.tar", .{});
1810 try createTarball(file, "package", paths);
1811 file.close();
1812
1813 const tmp_path = try tmp.dir.realpathAlloc(gpa, ".");
1814 const path_or_url = try std.fmt.allocPrint(
1815 gpa,
1816 "file://{s}/package.tar",
1817 .{tmp_path},
1818 );
1819 gpa.free(tmp_path);
1820 defer gpa.free(path_or_url);
1821
1822 var thread_pool: ThreadPool = undefined;
1823 try thread_pool.init(.{ .allocator = gpa });
1824 defer thread_pool.deinit();
1825
1826 var http_client: std.http.Client = .{ .allocator = gpa };
1827 defer http_client.deinit();
1828
1829 var global_cache_directory: Cache.Directory = .{
1830 .handle = try fs.cwd().makeOpenPath(global_cache_directory_path, .{}),
1831 .path = global_cache_directory_path,
1832 };
1833 defer global_cache_directory.handle.close();
1834
1835 var progress: std.Progress = .{ .dont_print_on_dumb = true };
1836 const root_prog_node = progress.start("Fetch", 0);
1837 defer root_prog_node.end();
1838
1839 var job_queue: Fetch.JobQueue = .{
1840 .http_client = &http_client,
1841 .thread_pool = &thread_pool,
1842 .global_cache = global_cache_directory,
1843 .recursive = false,
1844 .read_only = false,
1845 .debug_hash = true,
1846 .work_around_btrfs_bug = false,
1847 };
1848 defer job_queue.deinit();
1849
1850 var fetch: Fetch = .{
1851 .arena = std.heap.ArenaAllocator.init(gpa),
1852 .location = .{ .path_or_url = path_or_url },
1853 .location_tok = 0,
1854 .hash_tok = 0,
1855 .name_tok = 0,
1856 .lazy_status = .eager,
1857 .parent_package_root = Cache.Path{ .root_dir = undefined },
1858 .parent_manifest_ast = null,
1859 .prog_node = root_prog_node,
1860 .job_queue = &job_queue,
1861 .omit_missing_hash_error = true,
1862 .allow_missing_paths_field = false,
1863
1864 .package_root = undefined,
1865 .error_bundle = undefined,
1866 .manifest = null,
1867 .manifest_ast = undefined,
1868 .actual_hash = undefined,
1869 .has_build_zig = false,
1870 .oom_flag = false,
1871
1872 .module = null,
1873 };
1874 defer fetch.deinit();
1875
1876 try testing.expectError(error.FetchFailed, fetch.run());
1877
1878 try testing.expectEqual(1, fetch.error_bundle.root_list.items.len);
1879 var errors = try fetch.error_bundle.toOwnedBundle("");
1880 defer errors.deinit(gpa);
1881
1882 const em = errors.getErrorMessage(errors.getMessages()[0]);
1883 try testing.expectEqual(2, em.notes_len);
1884
1885 var al = std.ArrayList(u8).init(gpa);
1886 defer al.deinit();
1887 try errors.renderToWriter(.{ .ttyconf = .no_color }, al.writer());
1888 try testing.expectEqualStrings(
1889 \\error: unable to unpack tarball
1890 \\ note: unable to create file 'dir/file': PathAlreadyExists
1891 \\ note: unable to create file 'dir1/file1': PathAlreadyExists
1892 \\
1893 , al.items);
1894}
1895
1896const TarHeader = std.tar.output.Header;
1897
1898fn createTarball(file: fs.File, prefix: []const u8, paths: []const []const u8) !void {
1899 for (paths) |path| {
1900 var hdr = TarHeader.init();
1901 hdr.typeflag = .regular;
1902 //if (prefix.len > 0) {
1903 try hdr.setPath(prefix, path);
1904 // } else {
1905 // hdr.setName(path);
1906 // }
1907 try hdr.updateChecksum();
1908 try file.writeAll(std.mem.asBytes(&hdr));
1909 }
1910}