fix: normalize_file_path_dot_prefix on windows

This commit is contained in:
CJ van den Berg 2025-10-30 15:14:34 +01:00
parent ec783d68a6
commit 18a7190545
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1,19 +1,42 @@
const std = @import("std");
const pm = @import("project_manager");
const builtin = @import("builtin");
test "normalize_file_path_dot_prefix" {
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix("example.txt"));
try std.testing.expectEqualStrings("/example.txt", pm.normalize_file_path_dot_prefix("/example.txt"));
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix("./example.txt"));
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix("././example.txt"));
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix(".//example.txt"));
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix(".//./example.txt"));
try std.testing.expectEqualStrings("example.txt", pm.normalize_file_path_dot_prefix(".//.//example.txt"));
try std.testing.expectEqualStrings("../example.txt", pm.normalize_file_path_dot_prefix("./../example.txt"));
try std.testing.expectEqualStrings("../example.txt", pm.normalize_file_path_dot_prefix(".//../example.txt"));
try std.testing.expectEqualStrings("../example.txt", pm.normalize_file_path_dot_prefix("././../example.txt"));
try std.testing.expectEqualStrings("../example.txt", pm.normalize_file_path_dot_prefix("././/../example.txt"));
try std.testing.expectEqualStrings("../example.txt", pm.normalize_file_path_dot_prefix(".//.//../example.txt"));
try std.testing.expectEqualStrings("./", pm.normalize_file_path_dot_prefix("./"));
try std.testing.expectEqualStrings(".", pm.normalize_file_path_dot_prefix("."));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2("example.txt")));
try std.testing.expectEqualStrings(P1("/example.txt"), pm.normalize_file_path_dot_prefix(P2("/example.txt")));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2("./example.txt")));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2("././example.txt")));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2(".//example.txt")));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2(".//./example.txt")));
try std.testing.expectEqualStrings(P1("example.txt"), pm.normalize_file_path_dot_prefix(P2(".//.//example.txt")));
try std.testing.expectEqualStrings(P1("../example.txt"), pm.normalize_file_path_dot_prefix(P2("./../example.txt")));
try std.testing.expectEqualStrings(P1("../example.txt"), pm.normalize_file_path_dot_prefix(P2(".//../example.txt")));
try std.testing.expectEqualStrings(P1("../example.txt"), pm.normalize_file_path_dot_prefix(P2("././../example.txt")));
try std.testing.expectEqualStrings(P1("../example.txt"), pm.normalize_file_path_dot_prefix(P2("././/../example.txt")));
try std.testing.expectEqualStrings(P1("../example.txt"), pm.normalize_file_path_dot_prefix(P2(".//.//../example.txt")));
try std.testing.expectEqualStrings(P1("./"), pm.normalize_file_path_dot_prefix(P2("./")));
try std.testing.expectEqualStrings(P1("."), pm.normalize_file_path_dot_prefix(P2(".")));
}
fn P1(file_path: []const u8) []const u8 {
const local = struct {
var fixed_file_path: [256]u8 = undefined;
};
return fix_path(&local.fixed_file_path, file_path);
}
fn P2(file_path: []const u8) []const u8 {
const local = struct {
var fixed_file_path: [256]u8 = undefined;
};
return fix_path(&local.fixed_file_path, file_path);
}
fn fix_path(dest: []u8, src: []const u8) []const u8 {
if (builtin.os.tag == .windows) {
for (src, 0..) |c, i| switch (c) {
std.fs.path.sep_posix => dest[i] = std.fs.path.sep_windows,
else => dest[i] = c,
};
return dest[0..src.len];
} else return src;
}