authorgravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-03-01 21:37:47+01:00
committergravatar for igor.anic@gmail.comIgor Anić <igor.anic@gmail.com> 2024-03-11 12:22:00+01:00
logaf0502f6c4202df0223bcefa5121361c07680108
treeea147f5211994af35c84fc57bdd25c10a0e2c359
parentd0c06ca7127110a8afeb0ef524a197049892db21

std.tar: add tests for file and symlink create

Should do that before I broke package manager!

2 files changed, 26 insertions(+), 4 deletions(-)

lib/std/tar.zig+23
......@@ -1,3 +1,4 @@
1<<<<<<< HEAD
12//! Tar archive is single ordinary file which can contain many files (or
23//! directories, symlinks, ...). It's build by series of blocks each size of 512
34//! bytes. First block of each entry is header which defines type, name, size
......@@ -127,6 +128,8 @@ pub const Header = struct {
127128 return buffer[0 .. p.len + 1 + n.len];
128129 }
129130
131 /// When kind is symbolic_link linked-to name (target_path) is specified in
132 /// the linkname field.
130133 pub fn linkName(header: Header, buffer: *[LINK_NAME_SIZE]u8) []const u8 {
131134 const link_name = header.str(157, 100);
132135 if (link_name.len == 0) {
......@@ -619,6 +622,7 @@ fn createDirAndFile(dir: std.fs.Dir, file_name: []const u8) !std.fs.File {
619622 return fs_file;
620623}
621624
625// Creates a symbolic link at path `file_name` which points to `link_name`.
622626fn createDirAndSymlink(dir: std.fs.Dir, link_name: []const u8, file_name: []const u8) !void {
623627 dir.symLink(link_name, file_name, .{}) catch |err| {
624628 if (err == error.FileNotFound) {
......@@ -841,3 +845,22 @@ test "tar header parse mode" {
841845 }
842846 }
843847}
848
849test "create file and symlink" {
850 var root = std.testing.tmpDir(.{});
851 defer root.cleanup();
852
853 _ = try createDirAndFile(root.dir, "file1");
854 _ = try createDirAndFile(root.dir, "a/b/c/file2");
855
856 _ = createDirAndSymlink(root.dir, "a/b/c/file2", "symlink1") catch |err| {
857 // On Windows when developer mode is not enabled
858 if (err == error.AccessDenied) return error.SkipZigTest;
859 return err;
860 };
861 _ = try createDirAndSymlink(root.dir, "../../../file1", "d/e/f/symlink2");
862
863 // Danglink symlnik, file created later
864 _ = try createDirAndSymlink(root.dir, "../../../g/h/i/file4", "j/k/l/symlink3");
865 _ = try createDirAndFile(root.dir, "g/h/i/file4");
866}
lib/std/tar/test.zig+3-4
......@@ -1,5 +1,5 @@
1const std = @import("../std.zig");
2const tar = std.tar;
1const std = @import("std");
2const tar = @import("../tar.zig");
33const testing = std.testing;
44
55test "tar run Go test cases" {
......@@ -489,8 +489,7 @@ test "tar pipeToFileSystem" {
489489
490490 try testing.expectError(error.FileNotFound, root.dir.statFile("empty"));
491491 try testing.expect((try root.dir.statFile("a/file")).kind == .file);
492 // TODO is there better way to test symlink
493492 try testing.expect((try root.dir.statFile("b/symlink")).kind == .file); // statFile follows symlink
494493 var buf: [32]u8 = undefined;
495 _ = try root.dir.readLink("b/symlink", &buf);
494 try testing.expectEqualSlices(u8, "../a/file", try root.dir.readLink("b/symlink", &buf));
496495}