From 18a719054596d8387e6c4d509a73dc20d58aafc6 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 30 Oct 2025 15:14:34 +0100 Subject: [PATCH] fix: normalize_file_path_dot_prefix on windows --- test/tests_project_manager.zig | 51 ++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/test/tests_project_manager.zig b/test/tests_project_manager.zig index a5901b1..ded91e0 100644 --- a/test/tests_project_manager.zig +++ b/test/tests_project_manager.zig @@ -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; }