fix: normalize_file_path_dot_prefix on windows
This commit is contained in:
		
							parent
							
								
									ec783d68a6
								
							
						
					
					
						commit
						18a7190545
					
				
					 1 changed files with 37 additions and 14 deletions
				
			
		|  | @ -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; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue